问题描述
下面是配置,求解答如public Student querySutdent(id){ Student stu = XXDao.getStudent(id); stu.setSex("男"); return stu;}会将 stu的 sex 更新数据库里面 <!-- Enable @Transactional support --> <tx:annotation-driven/> <!-- Enable @AspectJ support --> <aop:aspectj-autoproxy/> <!-- Activates scanning of @Autowired --> <context:annotation-config/> <!-- Activates scanning of @service --> <context:component-scan base-package="com.chinadrtv.erp.tc;com.chinadrtv.erp.shipment;com.chinadrtv.erp.ic"/> <tx:advice id="txAdvice"> <tx:attributes> <!-- Read-only commented out to make things easier for end-users --> <tx:method name="build*" read-only="true" propagation="REQUIRED" rollback-for="Throwable"/> <tx:method name="get*" read-only="true" propagation="REQUIRED" rollback-for="Throwable"/> <tx:method name="calculate*" read-only="true" propagation="REQUIRED" rollback-for="Throwable"/> <tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/> </tx:attributes> </tx:advice> <!-- =================================================================== --> <!-- AOP: Configuration and Aspects --> <!-- =================================================================== --> <aop:config> <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Service.*(..))" order="0"/> <aop:advisor id="auditLogTx" advice-ref="txAdvice" pointcut="execution(* *..aop.*Service.*(..))" order="1"/> </aop:config>
解决方案
把查询方法设置为只读 即可
解决方案二:
stu.setSex("男"); 】你删除这段代码不就可以了?
解决方案三:
read-only="true"
解决方案四:
Hibernate的基础问题。因为你的hibernate的对象有4种状态你通过hibernate的get拿到的 stu对象。那么这个stu是被纳入session的管理的,当你修改了其属性以后。hibernate在commit的时候会自动对比对象的属性是否变化。如果变化了就会update如果不想纳入session的管理。调用session.evict(object) 将stu逐出session的管理即可