异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlu

场景:

在使用spring整合hibernate调用的HibernateTemplate时报错
解决:

在spring配置文件中添加事务的配置

    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <!-- 注入sessionFactory,配置sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
           <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
           <aop:pointcut id="personServiceOperation" expression="execution(* user.service.UserServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="personServiceOperation" />
    </aop:config>

猜你喜欢

转载自www.cnblogs.com/hujingnb/p/10246177.html