Unable to fix java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName
Solution 1
The /META-INF/persistence.xml
must end up in runtime classpath, not in web resources. So, you need to put it in Java source folder, not in Web content folder. The persistence.xml
is not related to web resources, but to Java beans.
Move it up:
MyProject
|-- java (src folder)
| |-- controller
| | `--ControllerServlet.java
| |-- entity
| |-- session
| `-- META-INF
| `-- persistence.xml <-- Here.
`--WebContent
`-- WEB-INF
|-- view
| |-- category.jsp
| `-- etc..
`-- index.jsp
This way the build will produce the proper WAR structure with /META-INF/persistence.xml
inside /WEB-INF/classes
. You seem to be using Eclipse, you can also achieve this by selecting the JPA facet in project's properties.
This way Eclipse will generate a persistence.xml
at the right place.
Solution 2
You mention that your persistence.xml
is present in META-INF
. If you're deploying this application as a WAR
file, your persistence.xml
needs to be packaged as
WEB-INF
`- classes
`- META-INF
`- persistence.xml`
According to the JPA 2.0 specification, section 8.2 Persistence Unit Packaging:
A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE environments, the root of a persistence unit must be one of the following:
• an EJB-JAR file
• the WEB-INF/classes directory of a WAR file
• a jar file in the WEB-INF/lib directory of a WAR file
• a jar file in the EAR library directory
• an application client jar fileIt is not required that an EJB-JAR or WAR file containing a persistence unit be packaged in an EAR unless the persistence unit contains persistence classes in addition to those contained within the EJB-JAR or WAR.
Solution 3
Because your project has incorrect structure. Create two separate parts for ejb and web application then pack them into EAR or use CDI. JPA (which EntityManagerFactory is part of) simply not activated by container for your application.
For Enterprise Application:
http://www.youtube.com/watch?v=32wpTh1TmY4
For CDI:
Solution 4
I had a similar issue with Embedded Glassfish server, caused by:
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName cis_ejbPU
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:132)
TO RESOLVE IT I DID THE FOLLOWING
1) In the pom.xml add the following dependencies
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.2-b06</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2) Set up the HashMap properties for the embedded container
File target = prepareModuleDirectory();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(EJBContainer.MODULES, target);
properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", "./src/test/glassfish/");
properties.put("org.glassfish.ejb.embedded.glassfish.configuration.file", "./src/test/glassfish/domains/domain1/config/domain.xml");
container = javax.ejb.embeddable.EJBContainer.createEJBContainer(properties);
3)
private static final String MODULE_NAME = "embedded";
private static final String TARGET_DIR = "target/" + MODULE_NAME;
private static File prepareModuleDirectory() throws IOException {
File result = new File(TARGET_DIR);
FileUtils.copyDirectory(new File("target/classes"), result);
FileUtils.copyFile(new File("target/test-classes/META-INF/persistence.xml"),
new File(TARGET_DIR + "/META-INF/persistence.xml"));
return result;
}
4) create the folder structure under
src\test\glassfish\domains\domain1\config
and copy the glassfish domain.xml which is present under
<glassfish server install> glassfish\domains\domain1\config to src\test\glassfish\domains\domain1\config.
I followed the above steps to resolve the issue.
james
Updated on June 28, 2022Comments
-
james almost 2 years
I am trying to learn JSP and servlets by making a project. My goal is NOT to focus on JPA, ORM and persistence or even EJB for now. So, please do not tell me to read a tutorial or book on that. Unfortunately, I am not supposed to use SQL queries to interact with the database. Instead, I used pre-written JPA code to manage the persistence while I focus on jsp and servlets.
When I run my project, I get the error -
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName AffableBeanPU.
My project is located at - https://github.com/double-whammy/affablebean.git Download as zip button is on bottom right corner.Note - My persistence.xml file IS inside the META-INF folder. I checked and built my project again. So that reason can be ruled out.
I googled and none of the solutions were of help. How do I fix this error ?
MyProject | |__java (src folder) | | | |__controller (package) | | |__ControllerServlet.java | | | |__entity (entity classes here) | |__session (facade classes for each entity class) | | |__WebContent | |__WEB-INF | |__view | |__category.jsp | | | |etc... | |__index.jsp
Exception:
Time|Info: Redirecting to /index.jsf Time|Info: Admin Console: Initializing Session Attributes... Time|Warning: EJB5184:A system exception occurred during an invocation on EJB CategoryFacade, method: public java.util.List session.AbstractFacade.findAll() Time|Warning: javax.ejb.EJBException at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748) at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698) at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4475) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2009) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) at com.sun.proxy.$Proxy193.findAll(Unknown Source) at session.__EJB31_Generated__CategoryFacade__Intf____Bean__.findAll(Unknown Source) at controller.ControllerServlet.init(ControllerServlet.java:31) at javax.servlet.GenericServlet.init(GenericServlet.java:244) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1212) etc ................................................... etc... at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName AffableBeanPU at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138) at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:171) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.getCriteriaBuilder(EntityManagerWrapper.java:834) at session.AbstractFacade.findAll(AbstractFacade.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) etc ................................................... etc... at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) ... 33 more Time|Severe: WebModule[/AffableBean]StandardWrapper.Throwable javax.ejb.EJBException at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748) at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698) at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503) etc ................................................... etc... at session.__EJB31_Generated__CategoryFacade__Intf____Bean__.findAll(Unknown Source) at controller.ControllerServlet.init(ControllerServlet.java:31) at javax.servlet.GenericServlet.init(GenericServlet.java:244) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1212) etc ................................................... etc... at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName AffableBeanPU at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138) at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:171) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.getCriteriaBuilder(EntityManagerWrapper.java:834) at session.AbstractFacade.findAll(AbstractFacade.java:41) etc ................................................... etc... at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) ... 33 more Time|Warning: StandardWrapperValve[ControllerServlet]: Allocate exception for servlet ControllerServlet java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName AffableBeanPU at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138) at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:171) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.getCriteriaBuilder(EntityManagerWrapper.java:834) at session.AbstractFacade.findAll(AbstractFacade.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) etc ................................................... etc...
Persistence.xml :
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="AffableBeanPU" transaction-type="JTA"> <jta-data-source>jdbc/affablebean</jta-data-source> <properties> <property name="eclipselink.logging.level" value="FINEST" /> </properties> </persistence-unit> </persistence>
-
james almost 10 yearsWhat do you mean by ` Create two separate parts for ejb and web application then pack them into EAR or use CDI.` ? Please tell me what are Enterprise app and CDI and what are the differences. These things are new to me. The tutorial from which I made my code mentions nothing about this. My project structure is according to the tutorial. Not sure if that is causing the problem. Thanks.
-
Alex Chernyshev almost 10 yearsTo be simple - just create new enterprise project in Netbeans/Eclipse and see what will be generated. docs.oracle.com/javaee/5/tutorial/doc/figures/…
-
james almost 10 yearsThanks for your answer Balus. I did not face this issue again because I started developing on a new system. In the new sys, I googled like crazy and found that you need to use JPA facets. Not sure if that was the reason my project worked. Did not reproduce the error. I got past the error in this question and got many other nasty errors. I am now developing the same code using netbeans, the original IDE for the tutorial. So far, its working perfectly.
-
BalusC almost 10 yearsAs answered, the
/META-INF/persistence.xml
has to end up in runtime classpath, not in web resources. Your problem is caused because you had it in the web resources. When using the JPA facet (or manually creating the folder and filter at the right place), then it ends up in the Java source folder, which ultimately ends up in runtime classpath. -
crakama about 6 yearsI'm using Intellij 2018 and GlassFish 5(Paraya)in my project, I had created another folder by the name
resources
and placed it on same hierarchy level assrc
and I got this same error. I moved/META-INF/persistence.xml
from resources folder and placed it insidesrc
as @BalusC and it worked like a charm. But of course this was after making necessary configurations at the paraya server(that is, setting up the JDBC resources and Connection Pool).