Java Hibernate 5.2.2:EntityManager 没有持久化提供程序

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

Hibernate 5.2.2: No Persistence provider for EntityManager

javahibernate

提问by tok

What has changed between Hibernate 5.1.1 and 5.2.2? If I use 5.2.2 I'll get an error message "No Persistence provider for EntityManager named pu". Exactly the same configuration works with 5.1.1. How should I change my code to get 5.2.2 to work?

Hibernate 5.1.1 和 5.2.2 之间有什么变化?如果我使用 5.2.2,我将收到一条错误消息“名为 pu 的 EntityManager 没有持久性提供程序”。完全相同的配置适用于 5.1.1。我应该如何更改我的代码以使 5.2.2 工作?

pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>jpatest</groupId>
    <artifactId>jpatest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <hibernate.version>5.2.2.Final</hibernate.version>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1209.jre7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
</project>

persistence.xml in src/main/resources/META-INF

src/main/resources/META-INF 中的persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="pu" >
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
            <property name="hibernate.default_schema" value="myschema" />
            <property name="hibernate.connection.username" value="xxx" />
            <property name="hibernate.connection.password" value="zzz" />
            <!-- <property name="hibernate.show_sql" value="true"/> -->
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>
</persistence>

creating EntityManager

创建实体管理器

factory = Persistence.createEntityManagerFactory("pu");
em = factory.createEntityManager();
tx = em.getTransaction();

采纳答案by Daniel Pinheiro

The class org.hibernate.ejb.HibernatePersistencedoes not exist in the hibernate-release-5.2.2.Final.zipbundle file. That's why the provider can't be found, because the class can't (at the project library jars). Instead, I used the class org.hibernate.jpa.HibernatePersistenceProvider, which CANbe found at hibernate-core-5.2.2.Final.jar (that comes with hibernate-release-5.2.2.Final.zip bundle), by changing the provider at persistence.xml to <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>. Doing so, it worked fine! Hope the problem is only this.

org.hibernate.ejb.HibernatePersistencehibernate-release-5.2.2.Final.zip包文件中不存在。这就是找不到提供者的原因,因为该类不能(在项目库 jar 中)。相反,我使用的类org.hibernate.jpa.HibernatePersistenceProvider,其中CAN在休眠核-5.2.2.Final.jar(附带hibernate-release-5.2.2.Final.zip束)可以发现,通过改变persistence.xml 中的提供程序到<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>. 这样做,效果很好!希望问题仅此而已。

回答by Bijay Raj Ghimire

Sometimes simply forgetting to add the persistence.xmlfile to build path causes this problem. Follow the following steps:

有时只是忘记将persistence.xml文件添加到构建路径会导致此问题。请按照以下步骤操作:

  1. Right click on persistence.xmlfile.
  2. Click on Build Path
  3. Add to build path
  1. 右键单击persistence.xml文件。
  2. 点击构建路径
  3. 添加到构建路径

Then it should work.

那么它应该工作。

回答by Spasoje Petronijevi?

I had the same error. I changed version of

我有同样的错误。我改了版本

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

from 5.3.1.Final to 5.3.6.Final and error disappeared.

从 5.3.1.Final 到 5.3.6.Final 并且错误消失了。