我正在尝试通过制作项目来学习JSP和servlet。我的目标不是专注于JPA,ORM和持久性 或者甚至是EJB。所以,请不要告诉我阅读教程或书籍。不幸的是,我不应该 使用SQL查询与数据库交互。相反,我使用预先编写的JPA代码来管理持久性 而我专注于jsp和servlets。
当我运行我的项目时,我收到错误 - java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName AffableBeanPU.
我的项目位于 - https://github.com/double-whammy/affablebean.git
下载拉链按钮位于右下角。
注意 - 我的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
例外:
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>
答案 0 :(得分:15)
/META-INF/persistence.xml
必须以运行时类路径结束,而不是在Web资源中。因此,您需要将其放在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
这样构建将在/META-INF/persistence.xml
内生成/WEB-INF/classes
的正确WAR结构。您似乎正在使用Eclipse,您也可以通过在项目属性中选择JPA facet来实现此目的。
这样Eclipse就会在正确的位置生成persistence.xml
。
答案 1 :(得分:4)
您提到persistence.xml
中存在META-INF
。如果您要将此应用程序部署为WAR
文件,则需要将persistence.xml
打包为
WEB-INF
`- classes
`- META-INF
`- persistence.xml`
根据JPA 2.0 specification, 8.2持久单元包装部分:
持久性单元由persistence.xml文件定义。 jar文件 或其META-INF目录包含persistence.xml的目录 file被称为持久性单元的根。在Java EE中 在环境中,持久性单元的根必须是其中之一 以下:
•EJB-JAR文件
•WAR文件的WEB-INF / classes目录
•WAR文件的WEB-INF / lib目录中的jar文件
•EAR库目录中的jar文件
•应用程序客户端jar文件不需要包含的EJB-JAR或WAR文件 除非持久性单元,否则持久性单元必须打包在EAR中 包含持久性类以及包含在其中的持久性类 EJB-JAR或WAR。
答案 2 :(得分:2)
因为您的项目结构不正确。为ejb和Web应用程序创建两个单独的部分,然后将它们打包到EAR或使用CDI。 JPA(EntityManagerFactory是其中的一部分)根本没有被应用程序的容器激活。
对于企业应用程序:
http://www.youtube.com/watch?v=32wpTh1TmY4
对于CDI:
答案 3 :(得分:1)
我遇到了与Embedded Glassfish服务器类似的问题,原因是:
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName cis_ejbPU
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:132)
要解决以下问题
1)在pom.xml中添加以下依赖项
<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)设置嵌入式容器的HashMap属性
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)在
下创建文件夹结构src\test\glassfish\domains\domain1\config
并复制
下的glassfish domain.xml<glassfish server install> glassfish\domains\domain1\config to src\test\glassfish\domains\domain1\config.
我按照上述步骤解决了问题。