Hibernate cascade inverse example (none, save-update, delete and delete-orphan)

Hibernate cascade example with inverse

We use parent child relation in our most of application. To solve this we use hibernate cascade in application. It is very important to understand how hibernate cascade and inverse works. In this example I use One to Many relation where a parent table as Parent and child table as Child. We need to understand why and when 3 queries will be fired by hibernate for all cases

Query 1: it is for parent itself

Query 2: need to update,insert/update/delete, child based on cascade value. It will fire all values of cascade except none. It also fire if manually we try to save like session.save(child).

Query 3: need to update child only if relation owner is parent that means inverse = false and when a new parent is inserting or deleting a parent or a new child is adding to parent. It wont fire when inverse = true.

I am trying to show all possible examples on hibernate cascade that will help you to know hibernate cascade, inverse and difference between them.

cascade

This keyword use in relation and let hibernate know what should do for child. Based on this value hibernate fires query to update child and intermediate table. This keyword help you reduce additional work for relation. For example if you need to do anything for child just use this keyword hibernate will take care of everything.

inverse

In fact inverse =  owner of relation.  when inverse = true then relation owner = child otherwise parent. By default inverse = false that means parent is relation owner.

Example of hibernate cascade with inverse


When value of hibernate cascade =  “none” : Hibernate ignores the association.

Cascade = none inverse= false; relation owner = Parent

Here cascade = none and inverse = true so parent is relation owner. Q1 is fired for parent itself. Since cascade=none and child is not manually saved so no query (Q2 query) will be fired for child. Since parent is owner of relation it needs to update (Q3 query) child record. Since child is not save manually and child object is in hibernate memory, transient, but it is not going to database as a result hibernate throw unsaved instance

Cascade = none inverse = true; relation owner = Child

Here cascade = none and inverse = true so Child is relation owner and Parent does not need to update Child (Q3). Since cascade=none and child is not manually saved so no query (Q2) will be fired for child.

Cascade = none inverse = false; relation owner=Parent

Q1: for parent itself. Q2: cascade = none but manually session.save() exists. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = none inverse = true; relation owner = Child

Q1: for parent itself. Q2: cascade = none but manually session.save() exists. Q3:wont fire because inverse = true; relation owner = Child. Child does not need to update Parent.


when value of hibernate cascade =  “save-update” : Let hibernate to save a new instance or changes persistence object of parent and also association object(s).

Cascade = save-update inverse = false; relation owner = Parent

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: fired because inverse = false; relation owner = Parent. Parent needs to update Parent.

Cascade = save-update inverse = true; relation owner =Child

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: wont fire because inverse = ture;

Cascade = save-update inverse = true; relation owner =Child

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: wont fire because inverse = true;

Cascade = save-update inverse = false; relation owner=Parent

Q1: for parent itself. Q2: cascade = save-update and manually session.update() exists. Q3: wont fire when parent is updating.

Cascade = save-update inverse = false; relation owner=Parent

Q1: wont fire because nothing change for parent.Q2: cascade = save-update. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = save-update inverse = true; relation owner=Child

Q1: wont fire because nothing change for parent.Q2: cascade = save-update. Q3: inverse = true; relation owner = Child.


when value of hibernate cascade= “delete-orphan”: Let hibernate deletes persistent object that has been removed from the association.

Cascade = delete-orphan inverse = false; relation owner=Parent

Q1: for parent itself. Q2: cascade = delete-orphan. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = delete-orphan inverse = true; relation owner=Child

Q1: for parent itself. Q2: cascade = delete-orphan. Q3: wont fire because inverse = true.

Cascade = delete-orphan inverse = false; relation owner=Parent

Q1: nothing update for parent. Q2: cascade = delete-orphan or manually delete. Q3: no insert or delete for parent.

Cascade = delete-orphan inverse = true; relation owner=Child

Q1: nothing update for parent. Q2: cascade = delete-orphan or manually delete. Q3: wont fire because there is no insert or delete for parent.

Download of Hibernate Cascade project

To get complete project of Hibernate cascade please click here.

Leave a Reply

Your email address will not be published. Required fields are marked *