12

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

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

starsplusplus
  • 1,232
  • 3
  • 19
  • 32
user3411221
  • 121
  • 1
  • 1
  • 3
  • I think that you have to be the same with Persistence.createEntityManagerFactory("jobs"); it works? – Makoton Mar 27 '14 at 15:12
  • Makoton, I corrected but still not working. – user3411221 Mar 27 '14 at 16:26
  • add this dependency with your dependency manager org.hibernate hibernate-entitymanager ${hibernate-core-version} with your current version – Makoton Mar 28 '14 at 14:18
  • possible duplicate of [JPA 2.1/Hibernate 4.3 deprecation warning](http://stackoverflow.com/questions/23041964/jpa-2-1-hibernate-4-3-deprecation-warning) – Stephan Jan 05 '15 at 19:53
  • Possible duplicate of [Encountered a deprecated javax.persistence.spi.PersistenceProvider](http://stackoverflow.com/questions/21693139/encountered-a-deprecated-javax-persistence-spi-persistenceprovider) – Stephan Oct 18 '16 at 14:02

2 Answers2

28

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

<!-- 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)

aurelije
  • 768
  • 1
  • 6
  • 14
  • When I change my provider to "org.hibernate.jpa.HibernatePersistenceProvider", I get a "javax.ejb.EJBException: java.lang.NullPointerException". When using the old provider everything runs fine... (JBoss EAP 6.1). Does anyone else have that problem? – Sverro2 Apr 02 '15 at 11:19
  • 1
    For people like me who didn't spot it by the eye: .ejb. -> .jpa. – Pieter De Bie Nov 12 '18 at 13:26
16

The solution by aurelije is 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-9141 and exist in Hibernate EntityManager version 4.3.5.Final.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
deucalion
  • 314
  • 2
  • 9