Java 没有名为 X 的 EntityManager 的持久性提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19322827/
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-12 16:05:43  来源:igfitidea点击:

No Persistence provider for EntityManager named X

javajakarta-eejpaentitymanagerpersistence.xml

提问by joshi737

I am developing a JavaSE application using JPA. Unfortunately, I get nullafter calling: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

我正在使用 JPA 开发 JavaSE 应用程序。不幸的是,我null打电话后得到: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

Below you will find:

下面你会发现:

  • A snippet of my code that invokes EntityManagerFactoryand unexpectedly returns null
  • My persistence.xmlfile
  • My project structure
  • 我调用EntityManagerFactory并意外返回的代码片段null
  • 我的persistence.xml档案
  • 我的项目结构

Snippet of my code:

我的代码片段:

public class Main {
    private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        // I get null on this line!!!
       factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

       EntityManager em = factory.createEntityManager();
       // do stuff with entity manager
       ...
    }
}

My persistence.xml:

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>path.to.package.server.Todo</class>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
     <properties>       
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>       
            <property name="javax.persistence.jdbc.url"  value="jdbc:postgresql://localhost:5432/test"/>       
            <property name="javax.persistence.jdbc.user" value="postgres"/>       
            <property name="javax.persistence.jdbc.password" value="postgres"/>       
        </properties>
  </persistence-unit>
</persistence>

My project structure:

我的项目结构:

This is the structure of my project:

这是我的项目结构:

采纳答案by cmd

You must move persistence.xmlfile to an appropriate location.

您必须将persistence.xml文件移动到适当的位置。

More specifically, add META-INF/persistence.xmlfile to the root of a source folder.

更具体地说,将META-INF/persistence.xml文件添加到源文件夹的根目录。

In this case, the following is an appropriate location: src\main\java\META-INF\persistence.xml

在这种情况下,以下是合适的位置: src\main\java\META-INF\persistence.xml

Here are the details: (taken from the JPA spec)

以下是详细信息:( 取自JPA 规范

A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.

persistence.xml 文件定义了一个持久性单元。persistence.xml 文件位于持久性单元根目录的 META-INF 目录中。

The root of the persistence unit is the key here.

持久化单元的根是这里的关键。

If you are a non-Java EE app

如果您是非 Java EE 应用程序

The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.

META-INF 目录包含persistence.xml 文件的jar 文件或目录称为持久性单元的根。

If you are in a Java EE app, the following are valid

如果您在Java EE 应用程序中,则以下内容有效

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[80]
  • 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

在 Java EE 环境中,持久性单元的根必须是以下之一:

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

回答by OndroMih

Quick advice:

快速建议:

  • check if persistence.xml is in your classpath
  • check if hibernate provider is in your classpath
  • 检查persistence.xml 是否在您的类路径中
  • 检查休眠提供程序是否在您的类路径中

With using JPA in standalone application (outside of JavaEE), a persistence provider needs to be specified somewhere. This can be done in two ways that I know of:

在独立应用程序(JavaEE 之外)中使用 JPA 时,需要在某处指定持久性提供程序。这可以通过我所知道的两种方式来完成:

In my case, I found out that due to maven misconfiguration, hibernate-entitymanager.jarwas not included as a dependency, even if it was a transient dependency of other module.

就我而言,我发现由于 maven 配置错误,hibernate-entitymanager.jar没有作为依赖项包含在内,即使它是其他模块的临时依赖项。

See also answers here: No Persistence provider for EntityManager named

另请参阅此处的答案:No Persistence provider for EntityManager named

回答by RLZaleski

So in my case, everything was in the class path, but I had to add

所以就我而言,一切都在类路径中,但我必须添加

Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");

Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");

I think this caused the PersistenceProvider to register itself with the javax classes. I have had to do something similar for JDBC drivers in the past as well.

我认为这导致 PersistenceProvider 向 javax 类注册自己。过去我也不得不为 JDBC 驱动程序做类似的事情。

回答by esfandia

If you don't want to move your META-INF folder (maybe because that's where your IDE created it and you don't want to confuse it), and if you are using Maven, you can tell Maven where to look for META-INF using the <resources>tag. See: http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

如果您不想移动您的 META-INF 文件夹(可能是因为那是您的 IDE 创建它的地方并且您不想混淆它),并且如果您使用的是 Maven,您可以告诉 Maven 在哪里寻找 META- INF 使用<resources>标签。请参阅:http: //maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

In your case:

在你的情况下:

<build>
  <resources>
    <resource>
      <directory>src</directory>
    </resource>
  </resources>
</build>

回答by Prerak Sheth

I recently upgraded NB 8.1 to 8.2 and suddenly faced this problem and spent 2 days breaking my head to resolve. Up to 8.1, removing processorpath (mentioned above by others) worked. With 8.2, the problem persisted.

我最近将NB 8.1升级到8.2,突然遇到这个问题,花了2天时间才解决。在 8.1 之前,删除处理器路径(其他人在上面提到过)有效。使用 8.2,问题仍然存在。

Finally, I found that the eclipselink.jar is missing in the default library of EclipseLink (JPA 2.1). I added the file to the definition of the library and voila - it started working!

最后,我发现EclipseLink(JPA 2.1)的默认库中缺少eclipselink.jar。我将文件添加到库的定义中,瞧——它开始工作了!

回答by rahulnikhare

Dont give JPA dependency explicity

不要明确给出 JPA 依赖

        <dependency>
                    <groupId>javax.persistence</groupId>
                    <artifactId>persistence-api</artifactId>
                    <version>1.0.2</version>    
      </dependency>

Thanks,
Rahul

谢谢,
拉胡尔