Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate Hibernate 5.2 with Spring framework 4.x

Tags:

I am using latest stable Spring version (4.2.6.RELEASE).

After upgrading Hibernate from 5.1 to 5.2 and changing hibernate-entitymanager dependency to hibernate-core due hibernate documentation: https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2

I dont receive any compile error but all my tests fails with this stack trace:

Jun 06, 2016 12:00:38 PM org.springframework.test.context.TestContextManager beforeTestMethod WARNING: Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@548b7f67] to process 'before' execution of test method [public void com.rh.cores.architecture.tests.units.person.PersonTest.add_iranianNaturalPersonDateIsInvalid_preventSaving()] for test instance [com.rh.cores.architecture.tests.units.person.PersonTest@7f9e8421] org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;     at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)     at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)     at org.springframework.test.context.transaction.TransactionContext.startTransaction(TransactionContext.java:98)     at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:200)     at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:265)     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.prepareFlushMode(HibernateJpaDialect.java:187)     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:173)     at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)     ... 30 more  Jun 06, 2016 12:00:38 PM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript INFO: Executing SQL script from class path resource [test/downs/architecture/down.sql] Jun 06, 2016 12:00:38 PM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript INFO: Executed SQL script from class path resource [test/downs/architecture/down.sql] in 5 ms.  org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;      at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)     at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)     at org.springframework.test.context.transaction.TransactionContext.startTransaction(TransactionContext.java:98)     at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:200)     at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:265)     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.prepareFlushMode(HibernateJpaDialect.java:187)     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:173)     at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)     ... 30 more 
like image 411
Mohsen Kashi Avatar asked Jun 06 '16 07:06

Mohsen Kashi


People also ask

Does spring 4 support Hibernate 5?

3 with Spring Boot 2.2. 4. That being said only HIbernate 5 is supported by Spring 5, so no you cannot use hibernate4 with Spring Boot 2.2 or Spring 5.

Can Spring framework be integrated with Hibernate framework?

We can simply integrate hibernate application with spring application. In hibernate framework, we provide all the database information hibernate.

Which version of Hibernate is compatible with spring 4?

Note that Hibernate 4.3 is a JPA 2.1 provider and therefore only supported as of Spring Framework 4.0.

How do you integrate spring with Hibernate?

Spring Bean Configuration File for Hibernate 3 Integration There are two ways we can provide database connection details to Hibernate, first by passing everything in hibernateProperties and second by creating a DataSource and then passing it to hibernate.


2 Answers

Hibernate 5.2 supports added in Spring framework 4.3 , that its stable version will be available in next week. Spring 4.2 only supports Hibernate up to 5.1.

see this:

https://jira.spring.io/browse/SPR-14334

like image 114
Mohsen Kashi Avatar answered Sep 19 '22 07:09

Mohsen Kashi


The issue is related to the versions only. Use Hibernate version 5.1.0 with Spring 4.3.7. It will solve this issue.
Add the below dependency along with Spring framework dependencies of version 4.3.x .

<dependency>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-entitymanager</artifactId>     <version>5.1.0.Final</version> </dependency>` 
like image 22
Piyush Verma Avatar answered Sep 18 '22 07:09

Piyush Verma