如错误所述,您必须使用事务,因此在spring配置文件中声明事务管理器:
<bean id="txManager" > <property name="sessionFactory" ref="sessionFactory" /></bean><tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="*" /> </tx:attributes></tx:advice><aop:config> <aop:pointcut id="personServiceOperation" expression="execution(* com.examples.service.PersonService.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="personServiceOperation" /></aop:config>
在这里,我们还需要使用
aopand
tx命名空间,因此更新命名空间声明
beans如下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
最后删除配置文件中的以下行:
<prop key="hibernate.current_session_context_class">thread</prop>



