【发布时间】:2010-01-18 20:06:03
【问题描述】:
我想知道在 Java 上使用 HB 更新分离对象的某些字段的最佳方法是什么。特别是当对象具有子对象属性时。例如(删除注释并减少字段数以减少噪音):
public class Parent {
int id;
String field2;
...
Child child;
}
public class Child {
int id;
String field3;
}
在 MVC webapp 中更新 Parent 时,我可以使用 Session.get(Parent.class,123) 调用父实例,使用它来填充表单并显示它。没有 DTO,只是将分离的父级传递给视图并绑定到表单。现在,我只想允许用户更新父级的 field2 属性。因此,当用户发布表单时,我会得到一个填充了 id 和 field2 的 Parent 实例(我认为 mvc 框架在这里无关紧要,绑定时的行为基本相同)。
现在,哪种策略最适合执行实体的更新?我可以考虑几种选择,但我想听专家的意见:)(请记住,我不想放松父实例和子实例之间的关系)
A) 再次从 Session 中检索父实例并手动替换更新的字段
Parent pojoParent; //binded with the data of the Form.
Parent entity = Session.get(Parent.class,pojoParent.getId());
entity.setField2(pojoParent.getField2()).
我经常使用这个。但是 pojoParent 似乎被用作卧底 DTO。如果要更新的字段数量变大,也会变得很糟糕。
B) 将 Child 存储在某处(httpSession?)并在以后关联它。
Parent parent = Session.get(Parent.class,123);
//bind the retrieved parent to the form
// store the Child from parent.getChild() on the httpSession
...
//when the users submits the form...
pojoParent.setChild(someHttpSessionContext.getAttribute('Child'))
Session.save(pojoParent);
我认为这是废话,但我在一些项目中看到了它......
C) 将 Parent 和 Child 之间的关系设置为不可变。在关系上使用 updatable=false 我可以更新任何父字段,而不必担心失去孩子。无论如何,这是相当严格的,并且关系永远不会更新。
那么,您认为解决这种情况的最佳方法是什么?
提前谢谢你!
【问题讨论】: