users@jersey.java.net

Does JPA-injection in Jersey really work?

From: saturon <netbeans_at_vtxfree.ch>
Date: Wed, 24 Dec 2008 06:08:31 +0100

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