Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 400
james Avatar asked Jul 22 '14 16:07

james


1 Answers

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

like image 122
BalusC Avatar answered Oct 21 '22 17:10

BalusC