users@jersey.java.net

Re: [Jersey] Does JPA-injection in Jersey really work?

From: RoanBrasil <roanbrasil_at_yahoo.com.br>
Date: Fri, 9 Oct 2009 07:34:08 -0700 (PDT)

I am having the same issue....

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
        xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="citespace-jpa" transaction-type="JTA">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:/citespace-jpaDS</jta-data-source>
                <properties>
         <property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.format_sql" value="true"/>
         <property name="hibernate.connection.characterEncoding"
value="UTF-8"/>
         <!-- <property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>-->
                </properties>
        </persistence-unit>
</persistence>



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
       
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>example.jersey.spring</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

I am getting NullPointerException when I am using it:


       @PersistenceUnit(name = "citespace-jpa")
       protected EntityManagerFactory emf;

       @GET
       @Produces("text/plain")
       public String getIt() {
                       
               EntityManager manager = emf.createEntityManager();
               Query q = manager.createQuery("SELECT c from Country c where c.id=
:id ");
               q.setParameter("id", 1);
               List<Country> c = q.getResultList();


Have someone some solution to fix it?



Sam Pullara wrote:
>
> In my JPA setup I am using Tomcat, not Glassfish, so I am not trying
> to get automatic injection of the persistence unit. Instead I am
> using a Filter.init() to create the initial EntityManagerFactory and
> to handle the EntityManager and Transaction management (including
> Injection into my services through an InjectionProvider). Presumably
> this isn't what you want to do since you are using full JEE.
>
> Sam
>
> On Dec 23, 2008, at 9:08 PM, saturon wrote:
>
>> Hello,
>>
>> Well I just cannot get jersey to inject entitymanagerfactory (JNDI
>> lookup works though...).
>>
>> - I use glassfishv2ur2, jersey 1.0.1, toplinkessentials
>> - I have the exact same configuration as in the bookmark example
>> EXCEPT:
>>
>> Using:com.sun.jersey.server.impl.container.servlet.ServletAdaptor in
>> web.xml gives me:
>> ...
>> Weaver processing class [foo.model.ent.NlUser].
>> end predeploying Persistence Unit NightlifeServer2PU; state
>> Predeployed;
>> factoryCount 1
>> PWC1412: WebModule[/NightlifeServer2]-ServletContext.log():PWC1409:
>> Servlet ServletAdaptor wird als nicht verfügbar markiert
>> WebModule[/MyServer2]Error loading WebappClassLoader
>> delegate: true
>> repositories:
>> /WEB-INF/classes/
>> ----------> Parent Classloader:
>> EJBClassLoader :
>> urlSet = []
>> doneCalled = false
>> Parent -> java.net.URLClassLoader_at_e5f46e
>> com.sun.jersey.server.impl.container.servlet.ServletAdaptor
>> java.lang.ClassNotFoundException:
>> com.sun.jersey.server.impl.container.servlet.ServletAdaptor
>>
>> Using the com.sun.jersey.spi.container.servlet.ServletContainer in
>> web.xml and my EntityManagerFactory is null. I use
>>
>> @PersistenceUnit(unitName = "MyPU")
>> EntityManagerFactory emf;
>>
>> And then im my get of the resource (exactyl like in the bookmark
>> example):
>> final EntityManager em = emf.createEntityManager();
>> TransactionManager.manage(new Transactional(em) { public void
>> transact() {
>> em.merge(userConfig);
>> }});
>>
>> So I am not seeing what im doing wrong here.
>>
>> -----Original Message-----
>> From: Florian Hars [mailto:hars_at_bik-gmbh.de]
>> Sent: Montag, 22. Dezember 2008 12:13
>> To: users_at_jersey.dev.java.net
>> Subject: Re: [Jersey] Jersey and JPA best practices?
>>
>>
>> Paul Sandoz schrieb:
>>> Complete injection support equivalent to that on Servlet is not
>>> currently implemented.
>>
>> That might explain why it didn't work :-). Anyway, what are the thread
>> safety properties of jersey resources? Would it even be useful to
>> inject
>> an EntityManager or would you run into concurrency issues?
>>
>>> What servlet class are you using for your web application?
>>
>> Ah, that is the secret sauce, you have to use
>> com.sun.jersey.server.impl.container.servlet.ServletAdaptor
>> instead of com.sun.jersey.spi.container.servlet.ServletContainer
>> if you want it to give you an EntityManagerFactory.
>>
>> Maybe somebody should add an item to the FAQ that explains the
>> following
>> points:
>>
>> 1. You must use the mentioned ServletAdaptor class.
>> 2. You must specify the persistence unit as an init-param
>> to the servlet.
>> 3. You can only inject via @PersistenceUnit EntityManagerFactory emf =
>> ... 4. How to use EntityManagers created by that Factory to avoid
>> threading problems in your jersey resources
>> 5. How to use transactions in jersey resouces.
>> (have I missed anything?)
>>
>> The answers are all implicit in the bookmarks example, but it might
>> help
>> people if they are written down with some short explanations.
>>
>> - Florian.
>>
>> PS: Koan for today:
>> After making the change, the glassfish first gave me:
>>
>> java.lang.ClassCastException: com.example.jpa.User cannot be cast to
>> com.example.jpa.User
>>
>> After playing around and finally redeploying the same code, everything
>> works. Classloader madness?
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
>

-- 
View this message in context: http://n2.nabble.com/Jersey-and-JPA-best-practices-tp1672268p3794969.html
Sent from the Jersey mailing list archive at Nabble.com.