I have a problem with injecting a EntityManagerFactory with @PersistenceUnit in a third part jar library that contains my DAO's.
Here is my multi module maven project structure.
-EAR
\-WAR ==> the webapp (maven type war)
\-EJB ==> the service facade (maven type EJB (provided to War)
\CORE ==> the legacy DAO's library (maven type JAR)
I use CDI to wire all together ==> there is a beans.xml file in all maven project... and the CDI works fine.
But the @PersistenceUnit annotation doesn't work when I put it in the CORE project. (the field is null)
When I use @PersistenceUnit in the EJB project, everything works fine.
My persistence.xml file is under META-INF folder of the EJB project (but I tried to put it in the core project too..without success).
Here is the injection point :
public class HibernateSupport {
@PersistenceUnit
private EntityManagerFactory emf;
...
}
This class is injected in the EJB project with CDI (and it works...but the emf field is null...)
Could someone help me?
Edit :
If I turn my core project into an EJB project (maven type ejb). Then it works... But I would like my core project stays a POJO project...
Edit 2 :
The producer code :
@PersistenceUnit
private EntityManagerFactory emf;
@Produces
public SessionFactory getSessionFactory() {
return ((HibernateEntityManagerFactory)emf).getSessionFactory();
}
With annotation @PersistenceUnit
you can inject only to EJB. Your HibernateSupport
is not EJB.
So in order to fix issue, you need:
HibernateSupport
an EJB;or
EntityManager
using @Inject
, read here how to do it;