错误:不推荐使用PersistenceProvider,使用HibernatePersistenceProvider而不是HibernatePersistence

时间:2014-03-27 14:50:52

标签: java hibernate

我认为对于像我这样的所有初学者来说,这是一个非常普遍的问题。但我找不到解决方案。爱好。

文件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>

当我运行代码时

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());
}

我收到了消息

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

我做错了什么?

2 个答案:

答案 0 :(得分:28)

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

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

应该改为

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

即使persistence.xml中缺少provider标记,由于Hibernate错误,Hibernate将使用旧的ejb提供程序实现,然后它会抱怨(精神分裂症)

答案 1 :(得分:16)

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