I have a doubt a long time and never find the answer, so I appreciate any king of help or explanation… maybe it´s is just a conceptual mistake…
I have an environment where I use CMT and Hibernate. I can inject the EntityManager as a resource on my EJB without any problem. But I couldn´t make it inject direct in my DAOs, since I instantiate it with a traditional factory pattern….
But my question is: I need to use the EntityManager, but don´t want to pass it all the way down through my layers! Must have another way and I´m probably not seeing the solution…
Can I do some king of context lookup for this EntityManager? Or any other more elegant solution?
I made my own solution, but its kind of crap lol… don´t worth a discution…
Thanks for any help and sorry for any english mistake, it´s not my native language.
Even following the JBNizet sugestion, I still curious to know how to get the EntityManager and I found this answer in how to lookup for the EntityManager on J2EE/EJB environment.
First, you need to put a @PersistentContext on the class scope of your EJB to set a def for later lookup and define the “name” attribute. Like:
Then you can lookup for your EntityManager with a code like this:
That is the way I found to get the EntityManager without injections.