Spring + Hibernate 项目中,查询POJO的时候调用 set方法 会将对应字段修改到数据库里面

下面是配置,求解答


public Student querySutdent(id){
Student stu = XXDao.getStudent(id);
stu.setSex("男");
return stu;
}

会将 stu的 sex 更新数据库里面

[code="java"]
<!-- 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>

[/code]

把查询方法设置为只读 即可

Hibernate的基础问题。因为你的hibernate的对象有4种状态

你通过hibernate的get拿到的 stu对象。
那么这个stu是被纳入session的管理的,当你修改了其属性以后。
hibernate在commit的时候会自动对比对象的属性是否变化。如果变化了就会update

如果不想纳入session的管理。调用session.evict(object) 将stu逐出session的管理即可

read-only="true"

stu.setSex("男"); 】

你删除这段代码不就可以了?