Different between cascade and inverse

Many Hibernate developers are confuse about the cascade option and inverse keyword. In some ways..they really look quite similar at the beginning, both are related with relationship.

Cascade vs inverse

However, there is no relationship between cascade and inverse, both are totally different notions.

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).

Example

In this example, the relationship owner is belong to stockDailyRecords (inverse=true).


<!-- Stock.hbm.xml -->
<hibernate-mapping>
    <class name="com.mkyong.common.Stock" table="stock" ...>
    ...
    <set name="stockDailyRecords" table="stock_daily_record" inverse="true">
        <key>
            <column name="STOCK_ID" not-null="true" />
        </key>
        <one-to-many class="com.mkyong.common.StockDailyRecord" />
    </set>
    ...

When you save or update the stock object


session.save(stock);
session.update(stock);

Hibernate will only insert or update the STOCK table, no update on the foreign key column. More detail example here…

2. cascade

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

Example

In this example, the cascade=”save-update” is declare on stockDailyRecords.


<!-- Stock.hbm.xml -->
<hibernate-mapping>
    <class name="com.mkyong.common.Stock" table="stock" ...>
    ...
    <set name="stockDailyRecords" table="stock_daily_record" 
        cascade="save-update" inverse="true">
        <key>
            <column name="STOCK_ID" not-null="true" />
        </key>
        <one-to-many class="com.mkyong.common.StockDailyRecord" />
    </set>
    ...

When you save or update the stock object


session.save(stock);
session.update(stock);

It will inserted or updated the record into STOCK table and call another insert or update statement (cascade=”save-update”) on StockDailyRecord. More detail example here…

Conclusion

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

Reference

1. inverse=”true” example and explanation
2. cascade example – save, update and delete

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
anilAsmit BasuBinh Thanh NguyenSandyjaivasanth Recent comment authors
newest oldest most voted
Binh Thanh Nguyen
Guest
Binh Thanh Nguyen

Thanks, nice explanation.

Jagadeesh
Guest
Jagadeesh

Hello Yong

Thank you so much, now I could able to understand the difference clearly.

Thank you

anil
Guest
anil

thanks nice explanations

Asmit Basu
Guest
Asmit Basu

Mr. yong, great explanation. In addition to this, I think the below mentioned link is one of the best explanations about the difference between cascade and inverse in hibernate.

http://stackoverflow.com/questions/3667387/what-is-the-difference-between-cascade-inverse-in-hibernate-what-are-they-use

jaivasanth
Guest
jaivasanth

Really a good one…

Sunny shah
Guest
Sunny shah

I always confused between these 2 thing bu t explain IT with very simplicity thanks for ur help

reddy
Guest
reddy

Hey yong,

Really I appreciate you. You are helping so many people.

trackback
inverse = “true” example and explanation

[…] Since “Stock” is not the owner of the relationship, it will not update the “stock_daily_record.STOCK_ID” in stockDailyRecord table. inverse vs cascade Many people like to compare between inverse and cascade, but both are totally different notions, see the differential here. […]

trackback
Hibernate Tutorials | Tutorials

[…] Different between cascade and inverse Many Hibernate developers are confuse about the different between the cascade and inverse, here’s the explanation. […]

Sandy
Guest
Sandy

Great tutorials but very poor choice of words. English is also a language like Hibernate. Kindly spend some time with it.