我想知道在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,只是将脱离的父级传递给视图并绑定到表单。现在,我只想让用户更新 域2 父母的属性。因此,当用户发布表单时,我得到一个父实例,其中id和field2已填充(我认为mvc框架在这里不重要,绑定时所有行为大致相同)。
现在,哪种策略最适合执行实体更新?我可以想一些替代方案,但我想听听专家:)(请记住,我不想放松父实例和子实例之间的关系)
一个) 再次从Session中重新获取Parent实例,并手动替换更新的字段
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之间的关系设置为不可变。运用 更新=假 在关系上,我可以更新任何父字段,而不必担心失去孩子。无论如何,这是非常严格的,关系永远不会更新。
那么,您认为解决这种情况的最佳方法是什么?
先谢谢你!