aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate :  cascade=all behaves like cascade=all,delete-orphan Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate :  cascade=all behaves like cascade=all,delete-orphan" Watch "Hibernate :  cascade=all behaves like cascade=all,delete-orphan" New topic
Author

Hibernate : cascade=all behaves like cascade=all,delete-orphan

Anirban Chowdhury
Ranch Hand

Joined: Aug 05, 2008
Posts: 36
Hibernate version:3.6.2 final
Personal Oracle Database 11g Release 11.2.0.1.0 - Production

The relationship is like 1 organization can have multiple volunteers. However 1 volunteer may or may not be linked to an organization.
Organization.java

Volunteer.java


organization.hbm.xml

volunteer.hbm.xml




Ok, lets say there is 1 volunteer linked with this organization.
Then if we delink this volunteer from the organization and then call the delete on the organization, shouldn't it just leave an orphan volunteer?
It instead deletes the volunteer as well.



This generates the following SQL:



I think that's incorrect.Could you please suggest?


To living life on the edge! I blog my experiences @ http://anirbanchowdhury.wordpress.com.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

As far as I know cascade all includes cascade orphan-delete so you might want do remove that.
I know what you want to do and the possible explanation i have might be wrong!
Explanation I have is:
Hibernate is a state machine and it just checks the state of the managed entities when you tell it to. Transaction.commit, session.flush, etc.
If you do all those things in one transaction hibernates just sees.
Before state check:
Relationship there!
After/in state check:
Relationship not there! Check if cascade orphan delete is set and remove all orphans.

This is my understanding of hibernate so far. Might be right or totally wrong so please feel free to correct me!


Please correct my English.
Anirban Chowdhury
Ranch Hand

Joined: Aug 05, 2008
Posts: 36
Manuel, can you point me to some document according to which cascade = all included delete-orphan? AFAIK, it doesn't.
Thanks Anirban.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

I don't think an orphan is being deleted here.

An Organisation is being deleted and when this operation is called, Hibernate still thinks it is linked to the Volunteer instance. As cascade is set to ALL, this volunteer is removed when the organisation is deleted.

Try updating the organisation (to break the link with the volunteer) before deleting it.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

The reference manual explains that: "You can even use cascade="all" to specify that all operations should be cascaded along the association. The default cascade="none" specifies that no operations are to be cascaded."

Its a little vague though what all means in this context because the delete-orphan cascade is explained later on in the text. I would suggest it really means all cascades.
Found this: "Mapping an association (either a single valued association, or a collection) with cascade="all" marks the association as a parent/child style relationship where save/update/delete of the parent results in save/update/delete of the child or children."
Which is totally ok because in your case you don't want to cascade the delete at all as far as i understand.

Just try it and let us know.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate : cascade=all behaves like cascade=all,delete-orphan