spring整合hibernate时报错:org.hibernte.engine.transaction.spi.transactioncontext

错误提示:Caused by:java.lang.ClassNotFoundException: org.hibernte.engine.transaction.spi.transactioncontext

本人用的hibernate版本为5.2.5,spring为4.6.1.

在applicationContext.xml中错误配置:

<!--3. 配置hibernate的sessionfactory实例 ,通过spring提供的LocalSessionFactoryBean类进行配置-->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 引入数据源 -->  
        <property name="dataSource" ref="dataSource"></property>
        <!-- 引入hibernate的配置文件 -->           
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        
        <!--  hibernate中用xml配置的整合
             引入bean的映射
        <property name="mappingLocations" value="classpath:*.hbm.xml"></property>
        -->
        
        <!-- hibernate注解配置的整合
            此处要用annotatedClasses属性才能生效,而不能用packageToscan属性。
         -->
        <property name="annotatedClasses">
            <list>
                <value>com.entity.Account</value>
                <value>com.entity.Book</value>
            </list>
        </property>
    </bean>
    
    <!--4. 配置spring的声明事务管理 -->
      <!-- 4.1 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <!-- 加入hibernate的sessionFactory实例 -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
      <!-- 4.2 配置事务属性,需要事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <!-- 其他的方法使用默认值 -->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 4.3 配置事务切点,并把切点和事务属性关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.service.*.*(..))"
            id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

解决办法:hibernate是5.0版本,而spring是4.0版本。5.0版本的hibernate中的相应包中把那个类给取消了。而在spring中配置时,我们最多只能配置到hibernate4,所以就出现了上述问题。

解决很简单,去网上下载hibernate4.0版本的hibernate-core-4.3.8.Final.jar,用这个文件替换中5.0中的那个。现在再运行,就能正常工作了

原文地址:https://www.cnblogs.com/xiangxinhouse/p/6685104.html