Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between cascade & inverse in hibernate, what are they used for?

How to use cascade and inverse in hibernate? What is the procedure/tag to define them? Are they related to each other and how are they useful?

like image 745
Chandra Sekhar Avatar asked Sep 08 '10 11:09

Chandra Sekhar


People also ask

Which is better Cascade Complete or Cascade Platinum?

What's the Difference Between Cascade Complete and Platinum? The Short Answer. Cascade Platinum is more expensive than Complete, but it is a stronger, more effective formula containing 50% more active cleaning ingredients.

Which is better for dishwasher Cascade or finish?

Both brands are recognized for their excellent cleaning ability, but Cascade won Best Overall Detergent by Good Housekeeping and The Spruce. Only Cascade offers a Free & Clear detergent formulation that is scent and dye-free. Finish has more options for dishwasher cleaning and additives like rinse aids.

What's the difference between Cascade Platinum and Platinum Plus?

Cascade Platinum Plus Dishwasher Pods The main difference between the Platinum Plus and Cascade's Compete line is the addition of a presoak aid. It's part powder and part gel with a water softener and rinse aid for a complete cleaning solution that will work on virtually any dish or pan, no matter how dirty.


2 Answers

In case of many-to-many relation through intermediary table; "Cascade" says whether a record will be created/updated in the child table. Whereas "Inverse" says whether a record will be created/updated in the intermediary table

e.g. Assume below scenario 1 student can have multiple phones. So Student class has property for Set of phones. Also 1 Phone can be owned by multiple students. So Phone class has property for Set of Students. This mapping is mentioned in stud_phone table.

So there are three tables viz. Student, Phone and stud_phone (intermediary) table. Mapping might look like:

<set name="phoneset" table="stud_phone" cascade="save-update" inverse="true">   <key column="mapping_stud_id">< /key>   <many-to-many class="com.domain.Phone" column="mapping_phon_id"/> </set>  

A new student object is created and 2 new phone objects are added to its set. And session.save(student_obj) is called. Depending upon "cascade" and "inverse" settings different queries will be fired.

Below are different combinations of cascade and inverse and their impact.

1) CASCADE IS NONE and INVERSE is false

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?) Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?) Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?) 

2) CASCADE is NONE and INVERSE is true

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?) 

3) CASCADE is save-update and INVERSE is false

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?) Hibernate: insert into phone (phone_num, phone_id) values (?, ?) Hibernate: insert into phone (phone_num, phone_id) values (?, ?) Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?) Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?) 

4) CASCADE is save-update and INVERSE true

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?) Hibernate: insert into phone (phone_num, phone_id) values (?, ?) Hibernate: insert into phone (phone_num, phone_id) values (?, ?) 

As it can be seen, only when CASCADE was save-update the records were created in PHONE table also. Otherwise not.

When INVERSE was false ( i.e. Student was the owner of relationship ) the intermediary table STUD_PHONE was updated. When inverse is true, Phone is owner of relationship, so even though a new student was created, the intermediary table was not updated.

So in case of relation of two entities, "cascade" affects other entity table and "inverse" affects intermediary table. So their effect is independent.

like image 158
Kaushik Lele Avatar answered Oct 07 '22 15:10

Kaushik Lele


Information referenced from Different between cascade and inverse link:

1. inverse: This is used to decide which side is the relationship owner to manage the relationship (insert or update of the foreign key column).

2. cascade: In cascade, after one operation (save, update and delete) is done, it will decide whether it need to call other operations (save, update and delete) on another entities which has relationship with each other.

Conclusion: In short, the “inverse” decides which side will update the foreign key, while “cascade” decides what’s the follow by operation should execute. Both look quite similar in relationship, but it’s totally two different things. Hibernate developers are worth to spend time to research on it, because misunderstanding the concept or misusing it will bring serious performance or data integrity issue in their application.

Also check this forum topic: https://forum.hibernate.org/viewtopic.php?f=1&t=949041

like image 35
YoK Avatar answered Oct 07 '22 14:10

YoK