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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24892866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:24:32  来源:igfitidea点击:

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

javaeclipsejspjakarta-eeservlets

提问by james

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.

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

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.gitDownload as zip button is on bottom right corner.

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

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.

- 我的 persistence.xml 文件位于 META-INF 文件夹中。我再次检查并构建了我的项目。所以可以排除这个原因。

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:

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

回答by Alex Chernyshev

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.

因为你的项目结构不正确。为 ejb 和 web 应用程序创建两个单独的部分,然后将它们打包到 EAR 中或使用 CDI。JPA(EntityManagerFactory 是其中的一部分)根本没有被应用程序的容器激活。

For Enterprise Application:

对于企业应用:

http://www.youtube.com/watch?v=32wpTh1TmY4

http://www.youtube.com/watch?v=32wpTh1TmY4

For CDI:

对于 CDI:

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

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

回答by Robby Cornelissen

You mention that your persistence.xmlis present in META-INF. If you're deploying this application as a WARfile, your persistence.xmlneeds to be packaged as

你提到你persistence.xml的存在于META-INF. 如果您将此应用程序部署为WAR文件,则persistence.xml需要打包为

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

According to the JPA 2.0 specification, section 8.2 Persistence Unit Packaging:

根据JPA 2.0 规范,第8.2Persistence 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.

持久性单元由persistence.xml 文件定义。META-INF 目录包含persistence.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 中的持久性类之外还包含持久性类。

回答by BalusC

The /META-INF/persistence.xmlmust 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.xmlis not related to web resources, but to Java beans.

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

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.xmlinside /WEB-INF/classes. You seem to be using Eclipse, you can also achieve this by selecting the JPA facet in project's properties.

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

enter image description here

在此处输入图片说明

This way Eclipse will generate a persistence.xmlat the right place.

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

enter image description here

在此处输入图片说明

回答by Madhu Pisipati

I had a similar issue with Embedded Glassfish server, caused by:

我在嵌入式 Glassfish 服务器上遇到了类似的问题,原因是:

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

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) Set up the HashMap properties for the embedded container

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)

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

4)在下面创建文件夹结构

src\test\glassfish\domains\domain1\config 

and copy the glassfish domain.xml which is present under

并复制存在于下的 glassfish domain.xml

<glassfish server install> glassfish\domains\domain1\config  to src\test\glassfish\domains\domain1\config.

I followed the above steps to resolve the issue.

我按照上述步骤解决了这个问题。