Java 错误:已弃用 PersistenceProvider,使用 HibernatePersistenceProvider 而不是 HibernatePersistence

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

Error: Deprecated PersistenceProvider, use HibernatePersistenceProvider instead of HibernatePersistence

javahibernate

提问by user3411221

I think this is a very common problem for all beginners like me. But I could not find a solution. Yet.

我认为这对于像我这样的初学者来说是一个非常普遍的问题。但我找不到解决办法。然而。

File persistence.xml is in src/META-INF/persistence.xml

文件 persistence.xml 位于 src/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
    <persistence 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"
    version="2.0">

    <persistence-unit name="jobs">

    <!-- provedor/implementacao do JPA -->
    <provider>org.hibernate.ejb.HibernatePersistenceProvider</provider>

    <!-- entidade mapeada -->
    <class>
        br.com.caelum.tarefas.modelo.Job
    </class>

    <properties>
        <!-- dados da conexao -->
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/fj21" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root" />

        <!-- propriedades do hibernate -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />

        <!-- atualiza o banco, gera as tabelas se for preciso -->
        <property name="hibernate.hbm2ddl.auto" value="update" />

    </properties>
</persistence-unit>

When I run the code

当我运行代码

try
{
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("tarefas");
    EntityManager manager = factory.createEntityManager();

    manager.close();
    factory.close();
    System.out.println("Execu??o com sucesso!");
}catch(Exception _Ex)
{
    System.out.println("Erro: " + _Ex.getMessage());
}

I get the message

我收到消息

27/03/2014 11:35:18 org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
27/03/2014 11:35:18 org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
27/03/2014 11:35:18 org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Erro: No Persistence provider for EntityManager named jobs

What I'm doing wrong?

我做错了什么?

回答by aurelije

The problem is in this line from persistence.xml file:

问题出在 persistence.xml 文件中的这一行:

<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.ejb.HibernatePersistenceProvider</provider>

It should be changed to

应该改为

<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

Even when provider tag is missing in persistence.xml, because of Hibernate bug, Hibernate will use the old ejb provider implementation, and then it will complain about that (schizophrenia)

即使在persistence.xml中缺少provider标签,因为Hibernate bug,Hibernate会使用旧的ejb provider实现,然后它会抱怨(精神分裂症)

回答by deucalion

The solution by aurelijeis correct, however a bug in Hibernate will incorrectly report an issue even when you specify the correct HibernatePersistenceProvider: all details on the bug can be found in bug report HHH-9141and exist in Hibernate EntityManager version 4.3.5.Final.

aurelije的解决方案是正确的,但是即使您指定了正确的,Hibernate 中的错误也会错误地报告问题HibernatePersistenceProvider:关于该错误的所有详细信息都可以在错误报告 HHH-9141 中找到,并且存在于 Hibernate EntityManager 版本 4.3.5.Final 中。