Hibernate 5.2.2:没有EntityManager的持久性提供程序

时间:2016-09-09 11:07:31

标签: java hibernate

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

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

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

3 个答案:

答案 0 :(得分:63)

hibernate-release-5.2.2.Final.zip 捆绑文件中不存在 org.hibernate.ejb.HibernatePersistence 类。这就是无法找到提供者的原因,因为该类不能(在项目库罐子里)。相反,我使用了类 org.hibernate.jpa.HibernatePersistenceProvider ,在hibernate-core-5.2.2.Final.jar中找到 CAN (与hibernate一起提供) release-5.2.2.Final.zip bundle),将persistence.xml中的提供程序更改为 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 。这样做,它工作正常!希望问题只是这个。

答案 1 :(得分:1)

我有同样的错误。 我更改了

的版本
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

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

答案 2 :(得分:0)

有时只是忘记添加persistence.xml文件来构建路径会导致此问题。请执行以下步骤:

  1. 右键单击persistence.xml文件。
  2. 单击构建路径
  3. 添加到构建路径

然后它应该工作。