【发布时间】:2011-10-11 01:27:58
【问题描述】:
我有一个object A 映射到数据库中的table A
class A {
Integer id;
String field2,field2;field3 ,... fieldN;
//lots of other attribute
}
现在我想编写一个只更新单个字段的 DAO api。一种方法是我可以先加载对象,然后更改我需要的属性,然后使用合并 api
//start transcation
A a = session.load(A.class, id);
A.setfieldP(newValue)
session.merge(A)
//commit transcation
现在如果我使用以下代码
//start transcation
A a = new A();
a.setId(id); //set a id by which object A exists in DB
A.setfieldP(newValue)
session.merge(A)
//commit transaction
现在第二种方法除了 id 和 fieldP 之外的所有字段都设置为 null
1)现在还有其他方法吗?
2)我可以使用更新而不是合并吗?
【问题讨论】: