when you use spring & Hibernate, have you ever met a log warning that says
WARN o.hibernate.ejb.HibernatePersistence - HHH015016: Encountered a
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>
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/
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.
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
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
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.