gpt4 book ai didi

java - 无法修复 java.lang.IllegalStateException : Unable to retrieve EntityManagerFactory for unitName

转载 作者:太空狗 更新时间:2023-10-29 22:45:17 26 4
gpt4 key购买 nike

我正在尝试通过制作项目来学习 JSP 和 servlet。我的目标不是关注 JPA、ORM 和持久性甚至现在是 EJB。所以,请不要告诉我阅读有关它的教程或书籍。不幸的是,我不应该使用 SQL 查询与数据库交互。相反,我使用预先编写的 JPA 代码来管理持久性而我专注于 jsp 和 servlet。

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

注意 - 我的 persistence.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

异常(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...

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

最佳答案

/META-INF/persistence.xml 必须在运行时类路径中结束,而不是在网络资源中。因此,您需要将它放在 Java 源文件夹中,而不是 Web 内容文件夹中。 persistence.xml 与 Web 资源无关,但与 Java bean 相关。

向上移动:

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

这样,构建将在 /WEB-INF/classes 中使用 /META-INF/persistence.xml 生成正确的 WAR 结构。您似乎正在使用 Eclipse,您也可以通过在项目属性中选择 JPA facet 来实现。

enter image description here

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

enter image description here

关于java - 无法修复 java.lang.IllegalStateException : Unable to retrieve EntityManagerFactory for unitName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com