I am developing Spring MVC Hibernate
Integration example. In this example I\'m using Spring 4.1.9.RELEASE
and Hibernate 5.1.0.Final
. If I
I wanted to create an application context that works well for both Hibernate 4 and Hibernate 5 at the same time.[why?]
So I replaced Hibernate-specific transaction manager (org.springframework.orm.hibernate4.HibernateTransactionManager
) with Spring JPA transaction manager (org.springframework.orm.jpa.JpaTransactionManager
):
// part of my Java Config
@Bean
@Autowired
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory, DataSource mainDataSource) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
transactionManager.setDataSource(mainDataSource);
return transactionManager;
}
[why?] Because this context is shared between different modules that depend on different Spring and Hibernate versions.
I have solved it. I have removed this part my config class and it's solved.
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
Changing the below import from hibernate4 to hibernate5 in hibenate configuaration file worked for me,
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
to this
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
In your pom.xml
you have Hibernate 5
,try to pass from hibernate5
to hibernate4
) in your pom:
<hibernate.version>4xxxx</hibernate.version>
...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
Change the definition of your transaction manager to:
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory"/>
</bean>
And upgrade your spring framework to 4.2.x
You can use the following setup as well for Spring 4 + Hibernate 5:
spring setup:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
pom.xml setup:
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.3.2.RELEASE</org.springframework-version>
</properties>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.2.Final</version>
</dependency>
Tried and tested.