Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

These are orthogonal concepts.

In associations, one of the side must be marked as inverse by using inverse attribute or mappedBy attribute (many side in one-to-many/ many-to-one association and either side in many-to-many association). This information is needed for Hibernate to correctly determine, how Java classes (object-oriented association) will be mapped to the database tables(relational association).

What about cascading - you can explicitly specify for Hibernate to perform operations on associated entities:

  • CascadeType.PERSIST - When the save() or persist() method is called for the owner, all the associated entities are also saved;
  • CascadeType.REMOVE - When the delete() method is called for the owner, all the associated entities are also deleted;
  • CascadeType.MERGE - When the merge() method is called for the owner, all the associated entities are also merged to managed/ persisted state;
  • CascadeType.REFRESH - When the refresh() method is called for the owner, all the associated entities are also refreshing from their database representation;
  • CascadeType.DETACH - When the session with which this entity was associated is closed, all the related entities would in the detached state;
  • CascadeType.ALL - Includes all the cascade operations;
  • "orphan removal" - removes associated entity from the database, when this entity removed from the relationship.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top