Reputation: 2403
My application is using JPA and is deployed under GlassFish 3.1 (so using EclipseLink). It's working fine, but I'm now trying to use a DAO implementation.
I find that article. But it seems not to be working with injection of persistence context. So schematically I have an entity class, a stateless EJB class and a persistence.xml
file. In my EJB a @PersistenceContext(name=...)
annotation. Like that it's working.
Now I add a DAO interface and a JPA DAO class (implementing the interaface). In my JPA DAO, I'm trying to inject the persistence context, but it's not working. No exception but remains null.
How can I do?
If it's not possible to do like that with GlassFish, my first idea was to pass the entitymanager to the DAO. It's working, but is it 'nice'?
The second idea, implement the JPA DAO like a stateless bean and inject it in my EJB. It's also working but...? Is it a good idea or not?
Upvotes: 0
Views: 726
Reputation: 3684
You cannot inject the PersistenceContext unless the object is managed by the container. It must be a @Stateless
or @Singleton
or things like that. You could also use CDI (JSR-299) to make the bean container-managed and get the injection to work. Or you could use Spring, like that article you linked to talks about.
Upvotes: 1