views:

125

answers:

1

I have 2 Classes, Parent and Child, with a @OneToMany relationship. Every parent has 0 or more children.

@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.REMOVE})

The Parent class has also other fields like the name.

I'm in a Container env, so my UI is made of JSP.

When I want to change the Parent object I have to pass it to a servlet and down to the client browser. In the web page I don't want to put all the children information, I only whant to have a text field for the parent name.

When the control return to me, I'd like to create a new Parent object with the brand new name and to merge with the EntityManger without worrying about the children.

+2  A: 

I think the correct way is actually to load the existing Parent object, set the name property to the new value, and save it. That is actually what you are doing.

So your form needs one field for the name that the user can enter, and one hidden field containing the id of the parent. When the form is posted, you load the Parent object by the id, set the name to the value entered, and save.

David Sykes
Thanks Davis, this is probably the only way. In terms of performance, can I assume the PersistenceContext probably already have an instance of the object?
s.susini
I am not sure about that. It would depend on your caching setup. I would just suggest that in the normal case, a lookup by ID is very fast, unless you have thousands of children. And never forget that "premature optimization is the root of all evil" ;-)
David Sykes