Lots of posts and information on this one. You need to mark the collection as inverse="true" which tells Hibernate that you will manage the relationship from the child side of things, otherwise Hibernate thinks the relationship is managed from the parent side. So, what happens is when you delete the parent Hibernate removes the relationship first by updating the child object to set its relationship field to null. Here's the sequence so you can understand it (Hibernate gurus, please correct me if I'm wrong...)
Inverse="false", cascade="none"
1. User deletes parent
2. Hibernate senses the relationships are being deleted
3. Hibernate updates child objects' relationship field to NULL, effectively deleting the relationship (this may result in a Foreign Key violation, if the column is not nullable in the DB)
4. Hibernate deletes the parent, orphaning the children
Inverse="true", cascade="none"
1. User attempts to delete parent
2. Hibernate senses the relationships are attempting to be deleted
3. Hibernate throws an exception
Please experiment with that and see what happens. Also, read and re-read the documentation and web site articles on inverse="true" relationships. Understanding that is crucial to understanding how hibernate works with relationships.
|