无法修复 java.lang.IllegalStateException: 无法检索 EntityManagerFactory for unitName

2022-09-03 01:13:46

我试图通过做一个项目来学习JSP和servlets。我的目标不是暂时不关注JPA,ORM和持久性,甚至EJB。所以,请不要告诉我要读一本关于它的教程或书。不幸的是,我不应该使用SQL查询来与数据库进行交互。相反,我使用预先编写的JPA代码来管理持久性,同时专注于jsp和servlet。

当我运行我的项目时,我收到错误 - 我的项目位于 - https://github.com/double-whammy/affablebean.git 下载为zip按钮位于右下角。java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName AffableBeanPU.

注意 - 我的持久性.xml文件位于 META-INF 文件夹中。我检查并再次构建了我的项目。因此,可以排除这种原因。

我用谷歌搜索,没有一个解决方案是有帮助的。如何修复此错误?

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

例外:

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

持久性.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>

答案 1

必须最终位于运行时类路径中,而不是 Web 资源中。因此,您需要将其放在Java源文件夹中,而不是放在Web内容文件夹中。与 Web 资源无关,而是与 Java Bean 相关。/META-INF/persistence.xmlpersistence.xml

上移:

MyProject
 |-- java (src folder)
 |    |-- controller
 |    |    `--ControllerServlet.java
 |    |-- entity
 |    |-- session
 |    `-- META-INF
 |         `-- persistence.xml <-- Here.
 `--WebContent
     `-- WEB-INF
          |-- view
          |    |-- category.jsp        
          |    `-- etc..
          `-- index.jsp

这样,构建将产生具有内部的适当WAR结构。您似乎正在使用 Eclipse,您也可以通过在项目属性中选择 JPA 方面来实现此目的。/META-INF/persistence.xml/WEB-INF/classes

enter image description here

这样,Eclipse 将在正确的位置生成一个。persistence.xml

enter image description here


答案 2

您提到您存在于 中。如果要将此应用程序部署为文件,则需要将其打包为persistence.xmlMETA-INFWARpersistence.xml

WEB-INF
 `- classes
     `- META-INF
         `- persistence.xml`

根据 JPA 2.0 规范,第 8.2 节持久性单元打包

持久性单元由持久性.xml文件定义。其 META-INF 目录包含持久性.xml文件的 jar 文件或目录称为持久性单元的根。在 Java EE 环境中,持久性单元的根必须是以下之一:

• EJB-JAR 文件
• WAR 文件的 WEB-INF/classes 目录
• WAR 文件的 WEB-INF/lib 目录中的 jar 文件
• EAR 库目录中
的 jar 文件 • 应用程序客户端 jar 文件

不需要将包含持久性单元的 EJB-JAR 或 WAR 文件打包到 EAR 中,除非持久性单元除了包含 EJB-JAR 或 WAR 中包含的持久性类之外,还包含持久性类。