java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext

微笑、不失礼 提交于 2019-12-04 01:03:39

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.

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>

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!