0
votes

I want to inject an EM into a Servlet, the persistence unit is declared in the EJB JAR. I have an EAR with an EJB JAR and a WAR.
The problem is that I get this error:

Could not resolve a persistence unit corresponding to the persistence-context-ref-name [cvu.JpaServlet/em] in the scope of the module called

If I put the persistence.xml in the WAR it works ok, but I don't want to do that.

Can I get an EntityManager from the PersistenceUnit (persistence.xml) declared in EJB JAR ?

2

2 Answers

0
votes

I think you need to add the ejb jar to your wars classpath.

In general you should inject an EntityManagerFacotry into a Servlet, not an EntityManager, as a Servlet is shared.

You could also wrap your persistence using SessionBeans.

0
votes

I moved all my persistence classes & persistence.xml into a persist.jar, and put it in ear/lib. Now it works from EJB and from WAR. If you put the persistence.xml just in your name.war/WEB-INF/classes/META-INF/persistence.xml you won't be able to use it in EJB. If you put it in EJB you won't be able to use @PersistenceUnit EntityManagerFactory in WAR. For you to access it in WAR you can put it also in WEB-INF/lib/persist.jar/META-INF/persistence.xml. But better is in EAR.