I have a parent-child relationship mapped using a Set, (one-to-many). When I
remove an element from the collection (parent.getChildren().remove(child)) and submit the change to hibernate using session.update(parent) then
2 SQL queries are produced,
one updating the parent (every field of it) and one doing the right update on the child (id=null).
I am supposing the update of every field happens because
I am updating the parent in another session than the one where I retrieved it (web app).
Is there a way to tell hibernate to commit the collection changes only.
Fabien.
PS: this is with hibernate 2.1 and mysql 3.51 transactional
Sample of code:
Code:
public class Parent {
// [...]
/**
* @hibernate.set cascade="delete"
* table="children"
* where="type='place'"
* @hibernate.collection-key column="owner_id"
* @hibernate.collection-one-to-many
* class="ChildImpl"
*/
Collection getChildren() {
return children
}
}
Code:
Session session = sessionFactory.openSession();
Parent parent = (Parent) session.get(Parent.class,"1");
session.close();
// later
parent.getChildren().remove(aChild);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.update(parent);
transaction.commit();
session.close();
// --> 2 SQL statements