Hi All,
I was wondering what's the best way to update some fields of a dettached object using HB on Java. Specially when the object has child objects attributes. For Example (annotations removed and fields number reduced to reduce noise):
public class Parent {
int id;
String field2;
...
Child child;
}
public class Child {
int id;
String field3;
}
When updating a Parent in a MVC webapp I could call for a parent instance using Session.get(Parent.class,123), use it to populate a form and display it. No DTOs, just the dettached parent is passed to the view and binded to a form. Now, I only want to allow the user to update the field2 attribute of the parent. So when the user posts the form I get a Parent instance with the id and field2 filled (I think the mvc framework dont matter here, all behaves mostly the same when binding).
Now, which strategy is the best to perform the update of the entity? I can think in few alternatives but I want to hear the experts :) (Remember that I dont want to loose the relationship between the parent and the child instances)
A) Retrive the Parent instance from the Session again and replace by hand the updated fields
Parent pojoParent; //binded with the data of the Form.
Parent entity = Session.get(Parent.class,pojoParent.getId());
entity.setField2(pojoParent.getField2()).
I'm using this a lot. But the pojoParent seems to be used as an undercover DTO. Also it gets awful if the number of fields to update gets bigger.
B) Store the Child somewhere (httpSession?) and associate it latter.
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);
I think this is crap but I saw it in some projects...
C) Set the relation between Parent and Child as inmutable. Using updatable=false on the relationship I can update any parent field without worrying about loosing the child. Anyway, this is quite restrictive and the relationship never will be updated.
So, what do you think is the best way to solve this situation?
Thank you in advance!