1.在hbm.xml中设置property 标签 update = “false” 例如
<property name=”age”update=”false”></property>
我们在执行 Update方法会发现,age 属性 不会被更改,缺点:不灵活。
Hibernate:
UPDATE
Teacher
SET
birthday=?,
name=?,
title=?
WHERE
id=?
2. 使用hbm.xml中的 dynamic-update="true"
<classname="com.sccin.entity.Student" table="student"dynamic-update="true"/>
3.使用HQL语句(灵活,方便),使用HQL语句修改数据.
Query query =session.createQuery("update A a set a.name = :name where id = :id");
4.
将需要更新的对象加载上来后,利用
org.springframework.beans.BeanUtils
类的 copyProperties方法,将需要 更新的值copy到对象中,最后再调用update方法。
由于本系统使用1和2方法,并未成功,所以采用4方法。
Useru = findUserById(user.getUid());
BeanUtils.copyProperties(user,u,newString[]{"uid","username","password","cr eatetime"});
getSession().update(u);