Hi,
I have read TFM and searched many times, so if I miss something, please kindly point me out, I'll appreciate it. You can be rude if you want, just point me to somewhere I can start--if nobody replies I don't even know it's because I miss a TFM, or my question is too banal that nobody wants to deign to answer it, or what. Thank you.
My question is this:
On the DB I have a Parent object, which has many children. In
Java it's modeled as a Set, with cascade set to all-delete-orphan, and lazy=false:
So I get this from DB, I populate
Struts ActionForm with this, and let the user edit Parent and remove/add/edit children as well. When I want to persist it back to the DB is where I get the problem. I have several alternatives:
1. Populate a new empty Parent instance using the ActionForm's values, then call saveOrUpdate on the populated Parent. It works great for editing, also there's only one Hibernate DB call. However, this doesn't work when the user _deleted_ a child in the UI layer. Hibernate will update children that are updated, but will ignore the fact that some children have been deleted.
2. Load a Parent instance first using the id from the ActionForm. Then populate this Parent instance using the values from the ActionForm. Then call saveOrUpdate(). This one is problematic because I cannot say "setChildren()".
I have to say,
But with this approach I need to load an instance first just to edit it. But deletion of children is successfully committed to the DB.
3. Just like #1, instead of using saveOrUpdate, use merge(). HOWEVER... due to the fact that the setChildren() looks like this:
instead of this:
I get this:
So there is conflicting requirement here! First I need to set the Parent of Child while adding, but second I need to leave the set method very simple.
So that makes me wonder. Surely people have been doing this for years. What is the typical practice of doing this?
Thank you!
Ray