Hibernate 5.2.2:没有EntityManager的持久性提供者 [英] Hibernate 5.2.2: No Persistence provider for EntityManager

查看:109
本文介绍了Hibernate 5.2.2:没有EntityManager的持久性提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate 5.1.1和5.2.2之间有什么变化?如果我使用5.2.2,我会得到一个错误消息没有EntityManager命名为pu的持久性提供者。完全相同的配置适用于5.1.1。

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?

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>

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

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

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

创建EntityManager

creating EntityManager

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


推荐答案

org.hibernate.ejb。 HibernatePersistence hibernate-release-5.2.2.Final.zip 包文件中不存在。这就是为什么无法找到提供者,因为该类不能(在项目库jar)。相反,我使用了 org.hibernate.jpa.HibernatePersistenceProvider 类,它可以在hibernate-core-5.2.2.Final.jar中找到 CAN 通过将persistence.xml中的提供者更改为 < provider> org.hibernate.jpa.HibernatePersistenceProvider< / provider> 即可。这样做,它工作得很好!希望问题只在于此。

The class org.hibernate.ejb.HibernatePersistence does not exist in the hibernate-release-5.2.2.Final.zip bundle 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 CAN be 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.

这篇关于Hibernate 5.2.2:没有EntityManager的持久性提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆