james
james

Reputation: 1677

Unable to fix java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName

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>

Upvotes: 18

Views: 18197

Answers (4)

Madhu Pisipati
Madhu Pisipati

Reputation: 61

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.

Upvotes: 1

BalusC
BalusC

Reputation: 1108577

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.

enter image description here

This way Eclipse will generate a persistence.xml at the right place.

enter image description here

Upvotes: 15

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

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 file

It 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.

Upvotes: 6

Alex Chernyshev
Alex Chernyshev

Reputation: 1745

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:

http://www.hascode.com/2011/02/creating-a-sample-java-ee-6-blog-application-with-jpa-ejb-cdi-jsf-and-primefaces-on-glassfish/

Upvotes: 2

Related Questions