Salut Yves,
What is important here are your Session and Transaction boundaries + where you call session.flush(), if you do so (transaction.commit() can do it)
open session
begin transaction
load parent + all children
create new child
add new child to parent's children collection
save parent, cascading is done to parent.children, assuming you at least have the save-update cascading style.
[Note: if you use an id generator (identity, sequence, native) whose ids are generated by the database itself, the sql insert is issued right away because hibernate needs to know this id! He uses it to cache the retreived object(s) in session.
Using other generators like Increment, Assigned, the insert statement is simply added to the batcher. When calling session.flush() (or transaction.commit(), calling it) entities are dirty checked (you can disable this behavior if you want in HB3) and the batcher is executed. Only at this point hb hits the db.]
remove newly added child from parent's children collection
[Note: here, it depends how you set the inverse attribute of your collection and if this relation is bidirectional, back in the mapping files. If you set inverse="true" from the one-to-many side, removing the entity from the parent's children collection isn't enuff. You must also set the children->parent reference to null (for a bidirectional relation with inverse=true from the one-to-many side]
flush session
collection and database should be synchonized, one less child.
commit transaction
close session
For the entity not removed from the collection itself, .equals() and .hashCode() on your entities is important, you must respect the rule: don't use entity id (if not using an assigned natural key even if not recommended) in hashCode() and equals() ovverrides.
[/list]
_________________ - Frank
|