0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tweetysat
  • 2,187
  • 14
  • 38
  • 75
  • ?? You mean for other questions ? Ok, but it's not possible to accept answers if there is no answers or if the answer is not answering my question. – tweetysat Apr 04 '12 at 05:53

1 Answers1

1

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.

Jim
  • 3,476
  • 4
  • 23
  • 33
  • Also see http://stackoverflow.com/questions/2021370/ejb-3-1-ejb-injection-into-pojo – Jim Apr 03 '12 at 14:59
  • Ok, thanks. I didn't saw the article was using spring ... But is it a good way to make a dao stateless ? I read some people (even on this forum) telling it's not a good idea. – tweetysat Apr 04 '12 at 05:52
  • There is always a debate about how to use DAOs. I tend to agree with Adam Bien that generally you don't need them with EJB 3.x. http://www.adam-bien.com/roller/abien/entry/jpa_ejb3_killed_the_dao http://www.adam-bien.com/roller/abien/entry/you_should_dao_if http://www.adam-bien.com/roller/abien/entry/daos_aren_t_dead_but – Jim Apr 04 '12 at 14:08