hibernate中casecade和inverse的区别
2016-05-20 17:09
288 查看
原文地址:http://www.mkyong.com/hibernate/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.
However, there is no relationship between cascade and inverse, both are totally different notions.
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).
Markup
When you save or update the stock object
Java
Hibernate will only insert or update the STOCK table, no update on the foreign key column. More
detail example here…
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.
Markup
When you save or update the stock object
Java
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…
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.
1. inverse=”true” example and explanation
2. cascade example – save, update and
delete
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).
Markup
<!-- 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
Java
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.
Markup
<!-- 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
Java
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 worthto 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 explanation2. cascade example – save, update and
delete
相关文章推荐
- CascadeView级联组件实现思路详解(分离思想和单链表)
- Hibernate Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- Struts2+Hibernate实现数据分页的方法
- Hibernate环境搭建与配置方法(Hello world配置文件版)
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- 详解Java的Hibernate框架中的注解与缓存
- 浅析Java的Hibernate框架中的继承关系设计
- Hibernate实现批量添加数据的方法
- JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
- 深入理解Hibernate中的flush机制
- 简要分析Java的Hibernate框架中的自定义类型