Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encountered a deprecated javax.persistence.spi.PersistenceProvider

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.

like image 521
eaststrong Avatar asked Feb 11 '14 04:02

eaststrong


4 Answers

It should be

org.hibernate.jpa.HibernatePersistenceProvider

Have a look at this.

Deprecated.

Use HibernatePersistenceProvider instead

like image 108
Andre Hofmeister Avatar answered Oct 31 '22 12:10

Andre Hofmeister


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/

like image 42
isma.imc Avatar answered Oct 31 '22 13:10

isma.imc


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.

like image 9
Hubert Kauker Avatar answered Oct 31 '22 11:10

Hubert Kauker


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

like image 8
cripox Avatar answered Oct 31 '22 11:10

cripox