30

when you use spring & Hibernate, have you ever met a log warning that says

WARN o.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.

How to handle that? Thank you for any answer.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
eaststrong
  • 373
  • 1
  • 4
  • 7

8 Answers8

34

It should be

org.hibernate.jpa.HibernatePersistenceProvider

Have a look at this.

Deprecated.

Use HibernatePersistenceProvider instead

Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74
  • 9
    cause of the problem? how to fix the problem? What files should I change in spring configuration? – hrishikeshp19 May 10 '14 at 23:14
  • 2
    @riship89 You have to change the `persistence.xml`. @AaronHall Thats wrong. I wrote to use `org.hibernate.jpa.HibernatePersistenceProvider` instead of `javax.persistence.spi.PersistenceProvider`. – Andre Hofmeister May 11 '14 at 09:22
15

If you are working with Spring Data JPA and Java Configuration, you will be able to solve it, adding the following code in your Entity Manager Factory:

factory.setPersistenceProvider(new HibernatePersistenceProvider());

@Bean
    public EntityManagerFactory entityManagerFactory() throws SQLException {

      HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      vendorAdapter.setGenerateDdl(true);
      vendorAdapter.setShowSql(true);

      LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
      factory.setJpaVendorAdapter(vendorAdapter);
      **factory.setPersistenceProvider(new HibernatePersistenceProvider());**
      factory.setPackagesToScan("com.company.appname.persistence.domain");
      factory.setDataSource(dataSource());

      factory.setJpaProperties(hibernateProperties());
      factory.afterPropertiesSet();

      return factory.getObject();
    }

You will find a good example of Hibernate configuration with Spring Data JPA here: http://spring.io/guides/tutorials/data/3/

isma.imc
  • 183
  • 1
  • 8
9

For users who are not using SPRING:

We can replace the standard javax.persistence bootstrapping by a Hibernate specific one.

Old:

EntityManagerFactory emf = Persistence.createEntityManagerFactory(
    PERSISTENCE_UNIT, props );

New:

PersistenceProvider provider = new HibernatePersistenceProvider();
EntityManagerFactory emf = provider.createEntityManagerFactory(
   PERSISTENCE_UNIT, props);

The deprecated warnings should now be gone. The problem was still present in 4.3.1.Final. In 5.1.0.Final it should be fixed.

Hubert Kauker
  • 151
  • 1
  • 3
  • :( too bad. Can't go to 5.1, it breaks all my naming strategies, and there's no funding for spending hours and hours messing with naming strategies... – Gus Jun 02 '16 at 19:19
  • I'm using Hibernate 4.3.8.Final and warnings are still there. However I managed to fix the issue with the suggested change in persistence.xml, plus the change recommended by this answer. I don't see the warnings anymore – McCoy Jul 27 '18 at 15:56
8

Had this problem while working with JPA's Entity Manager in Spring context, having transaction-type="RESOURCE_LOCAL" in persistence.xml.

It's not always a bug. I actually had the wrong provider configured.

I just changed the provider in persistence.xml from

<provider>org.hibernate.ejb.HibernatePersistence</provider>

to

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

and it works fine.

Notice that the package changed from EJB to JPA

Robert
  • 5,484
  • 1
  • 22
  • 35
cripox
  • 556
  • 1
  • 7
  • 19
  • 2
    Please stop commenting that it doesn't work for you. I didn't said my comment is always a solution. It is a solution sometimes. My comment was just a heads up for checking this, and I thought it may be handy. As I said : "It's not always a bug. I actually had the wrong provider configured." Maybe it's a common mistake. – cripox Apr 22 '15 at 13:29
2

You get this message because the class org.hibernate.ejb.HibernatePersistence is deprecated. Under my persistence.xml file I found the provider class had org.hibernate.ejb.HibernatePersistence and I changed it to org.hibernate.jpa.HibernatePersistenceProvider as mentioned in the stacktrace warning message.

persistence.xml

<persistence-unit name="personPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>Person</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db_name"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        ....
    </properties>
</persistence-unit>
Lucky
  • 16,787
  • 19
  • 117
  • 151
  • 1
    For those in xml spring configuration see : http://stackoverflow.com/questions/24520602/spring-data-jpa-no-bean-named-entitymanagerfactory-is-defined-injection-of-a . The declaration is – Xavier Bouclet Jul 30 '15 at 18:12
1

After changing your org.hibernate.ejb.HibernatePersistence to org.hibernate.jpa.HibernatePersistenceProvider in the persistence.xml Change also the hibernate-entitymanager dependency version, get the last version 5.2.10.Final that fixed the bug. Here is:

http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager/5.2.10.Final

it's worked for me

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
0

I changed the reference to:

org.hibernate.jpa.HibernatePersistenceProvider

but it didn't work.

Then I removed all the references to Hibernate 4.x jar libs, downloaded last version (5.2.7), then added this jar files and it finally works.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

If you are creating a Has-A relationship and forgot to mention @Embedded then it will throw the same error.

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named TEST
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
Procrastinator
  • 2,526
  • 30
  • 27
  • 36