Inverse和cascade是Hibernate映射中最难掌握的两个属性。两者都在对象的关联操作中发挥作用。
1.inverse属性:inverse所描述的是对象之间关联关系的维护方式。
inverse只存在于集合标记的元素中。Hibernate提供的集合元素包括<set/> <map/> <list/> <array /> <bag />
Inverse属性的作用是:是否将对集合对象的修改反映到数据库中。
inverse属性的默认值为false,表示对集合对象的修改会被反映到数据库中
inverse=”true” 表示对集合对象的修改不会被反映到数据库中。
<one-to-many>中inverse的属性的设置
例:Person中包含一个Child集合,Child包含一个Person对象。
要求:新建Person和Child对象,Child属于此Person对象
分析:在数据库中,就是新增一条Child记录
//one-to-many的inverse的配置
public void testInverse() {
Qx qx = new Qx();
qx.setQx("天山区");
Jd jd = new Jd();
jd.setJd("人民路");
qx.getJds().add(jd);
jd.setQx(qx);
super.add(qx);
super.add(jd);
}
配置Inverse=”false”,Hibernate的操作:
(1)Hibernate: insert into TBL_QX (qx) values (?)
(2)Hibernate: insert into TBL_JD (jd, qxid) values (?, ?)
(3)Hibernate: update TBL_JD set qxid=? where jdid=?
配置Inverse=”true”,Hibernate的操作
(1)Hibernate: insert into TBL_QX (qx) values (?)
(2)Hibernate: insert into TBL_JD (jd, qxid) values (?, ?)
可以看出:Inverse=”true”,SQL少执行一条更新操作(Java代码不会少)
<many-to-many>中inverse的属性的设置
在多对多的关系中,可以在关联的两边都不设置inverse属性(默认的false),那么说明关联的双方都可以维护两个对象之间的关联关系。
注:在设置多对多关系的inverse属性时,不能两个对象都将其设置为true
2.cascade属性
cascade属性的作用是描述关联对象进行操作时的级联特性。因此,只有涉及到关系的元素才有cascade属性。
具有cascade属性的标记包括<many-to-one /> <one-to-one /> <any /> <set /><bag /> <idbag /> <list /> <array />
注意:<ont-to-many />和 <many-to-many />是用在集合标记内部的,所以是不需要cascade属性的。
级联操作:指当主控方执行某项操作时,是否要对被关联方也执行相同的操作。
3.inverse和cascade的区别
作用的范围不同:
Inverse是设置在集合元素中的。
Cascade对于所有涉及到关联的元素都有效。
<many-to-one/><ont-to-many/>没有inverse属性,但有cascade属性
执行的策略不同
Inverse 会首先判断集合的变化情况,然后针对变化执行相应的处理。
Cascade 是直接对集合中每个元素执行相应的处理
执行的时机不同
Inverse是在执行SQL语句之前判断是否要执行该SQL语句
Cascade则在主控方发生操作时用来判断是否要进行级联操作
执行的目标不同
Inverse对于<ont-to-many>和<many-to-many>处理方式不相同。
对于<ont-to-many>,inverse所处理的是对被关联表进行修改操作。
对于<many-to-many>,inverse所处理的则是中间关联表
Cascade不会区分这两种关系的差别,所做的操作都是针对被关联的对象。
总结:
<one-to-many>中,建议inverse=”true”,由“many”方来进行关联关系的维护
<many-to-many>中,只设置其中一方inverse=”false”,或双方都不设置
Cascade,通常情况下都不会使用。特别是删除,一定要慎重。
说明约定-如:
1.表属性 :T1oo->id,name
T2oo->id,avg,aid( 外键 ),version
2.代码过程是:Save->t1oo对象(T1oo 外键 T2oo)
T1oo t1oo = new T1oo();
t1oo.setName("xx");
t1oo.setT2ooSet(new HashSet());
T2oo t2oo2 = new T2oo(24);
//t2oo2.setT1oo(t1oo); (在下面的本用例表中的第3属性 )
t1oo.getT2ooSet().add(t2oo2); (在下面的本用例表中的第2属性 )
session.save(t1oo);
3.本例表使用:
T1oo.hbm.xml ->
<set name="t2ooSet" inverse="true" cascade = "all" > (在下面的本用例表中的第1,2属性 )
<key column="aid"/>
<one-to-many class="T2oo"/>
</set>
T2oo.hbm.xml ->
<many-to-one name="t1oo" column="aid" class="T1oo" cascade="all" /> (在下面的本用例表中的第1,2属性 )
结合上面说明得表为:
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | all | | |
+-------+-------+-------+ true | false |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
4.执行 Hibernate语句 :
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
5.结果为:
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 2 | 24 | NULL | 0 | //主要就是看 aid属性
+----+-----+------+---------+
1 row in set (0.00 sec)
mysql> select * from t1oo; // 因为 T1oo总是可以 Save 下面就不再提了
+----+------+
| id | name |
+----+------+
| 2 | xx |
+----+------+
1 row in set (0.00 sec)
可改项:
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | 可改4 | 可改2 | | |
+-------+-------+-------+ | 可改1 |
| t2oo | | 可改3 | | |
+-------+-------+-------+----------+----------+
主本:
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | all | | |
+-------+-------+-------+ true | true |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | 1 | 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)
改1
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | all | | |
+-------+-------+-------+ true | false |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | NULL| 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)
改2
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | | | |
+-------+-------+-------+ true | true |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)
改2 改1
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | | | |
+-------+-------+-------+ true | false |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)
改3
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | all | | |
+-------+-------+-------+ true | true |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | 1 | 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)
改3 改2
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | | | |
+-------+-------+-------+ true | true |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)
改3 改1
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | all | | |
+-------+-------+-------+ true | false |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | NULL | 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)
改3 改2 改1
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | true | | | |
+-------+-------+-------+ true | false |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Empty set (0.00 sec)
改4
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | all | | |
+-------+-------+-------+ true | true |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | 1 | 0 |
+----+-----+------+---------+
1 row in set (0.02 sec)
改4 改1
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | all | | |
+-------+-------+-------+ true | false |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | 1 | 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)
改4 改2
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | | | |
+-------+-------+-------+ true | true |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)
改4 改3
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | all | | |
+-------+-------+-------+ true | true |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | 1 | 0 |
+----+-----+------+---------+
1 row in set (0.00 sec)
改4 改3 改2
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | | | |
+-------+-------+-------+ true | true |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)
改4 改1 改2
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | | | |
+-------+-------+-------+ true | false |
| t2oo | | all | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)
改4 改1 改3
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | all | | |
+-------+-------+-------+ true | false |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set aid=? where id=?
mysql> select * from t2oo;
+----+-----+------+---------+
| id | avg | aid | version |
+----+-----+------+---------+
| 1 | 24 | 1 | 0 |
+----+-----+------+---------+
1 row in set (0.02 sec)
改4 改3 改2 改1
+-------+-------+-------+----------+----------+
|hbn.xml|inverse|cascade|t1oo->t2oo|t2oo->t1oo|
+-------+-------+-------+----------+----------+
| t1oo | false | | | |
+-------+-------+-------+ true | false |
| t2oo | | | | |
+-------+-------+-------+----------+----------+
Hibernate: insert into t1oo (name, id) values (?, ?)
Hibernate: update t2oo set aid=? where id=?
Empty set (0.00 sec)
相关推荐
Hibernate中cascade与inverse属性详解
### Hibernate Inverse 和 Cascade 的详细讲解 #### 一、引言 在ORM(Object-Relational Mapping)领域,Hibernate作为一款流行的Java持久层框架,它提供了丰富的API和配置选项来帮助开发者实现对象与数据库表之间...
### Hibernate中的Cascade选项详解 #### 一、概述 在Hibernate框架中,cascade选项提供了一种管理实体间关联的便捷方式,使得对一个实体的操作能够自动地应用到与之相关的其他实体上。这对于处理复杂的数据库关系...
在深入探讨Hibernate集合映射中的`inverse`与`cascade`属性之前,我们首先需要理解Hibernate框架的基本概念。Hibernate是一个开放源代码的对象关系映射(ORM)框架,它为Java应用程序提供了一种将对象模型与数据库...
2. **级联操作**:`inverse`属性常与`cascade`属性一起使用。`cascade`定义了关联实体的操作是否应级联到另一端。例如,`cascade="save-update"`意味着当父实体被保存或更新时,其关联的子实体也会随之保存或更新。...
在Hibernate的XML映射文件中,集合类型的关联关系经常使用`<set>`标签,如`<set>`下的`name`属性指定集合属性名,`table`属性指定中间表(多对多关系时),`inverse`属性表示维护关系的责任方,`cascade`属性控制...
<set name="orders" cascade="all" inverse="true"> <key column="CUSTOMER_ID"/> </hibernate-mapping> ``` 在`Orders.hbm.xml`中,我们需要指定`Customers`作为其外键的实体类: ```xml <!DOCTYPE ...
- **Inverse和Cascade**:用于控制关联关系中对象的级联操作。 - **延迟加载(LazyLoading)**:这是一种优化策略,只有在真正需要的时候才加载关联对象的数据,以减少内存占用和提高性能。 #### 八、事务管理 - **...
hibernate中一对一,一对多,多对多关系的配置,延迟加载,cascade,inverse hibernate查询方式概述,HQL查询,QBC查询,分页,结果集封装方式 ,高级查询 查询的优化,一级缓存,二级缓存,批量查询,注解方式
### Hibernate配置详解(二)——深入理解`.hbm.xml`文件与实体关联 #### 引言 在前文《Hibernate 配置详解》中,我们已经初步探讨了Hibernate框架的基本配置,包括`hibernate.cfg.xml`文件的重要性及其在...
### Hibernate常用注解详解 #### 一、JPA与Hibernate注解基础 JPA(Java Persistence API)是一种标准规范,用于实现对象关系映射(ORM),允许开发人员使用注解或XML来描述实体对象与数据库表之间的映射关系。...
以下是对Hibernate一对多映射配置的详解: 一、XML文件配置 1. 单向关联 在这种配置中,关系只在一个方向上定义,例如User类有多个Account。在User的配置文件中,不直接声明与Account的关联,而是在Account的配置...
### Hibernate的映射、三态、脏数据、Session缓存详解 #### 一、Hibernate映射 **Hibernate映射**是Hibernate框架的核心功能之一,它实现了Java对象与数据库表之间的映射关系。通过这种方式,开发人员可以使用面向...
【Hibernate配置文件中映射元素详解】 在对象关系映射(ORM)框架Hibernate中,对象与数据库表之间的关联是通过一个XML配置文件来定义的。这个XML文件包含了映射元素,用于描述Java类如何转化为数据库中的表。映射...
<set name="addresses" inverse="true" cascade="all"> <key column="user_id"/> ``` 在`User`类中,我们需要定义一个Set类型的属性来对应这个集合: ```java public class User { // ... private Set...
**Hibernate 3.x 关联映射详解** 在Java开发中,Hibernate是一个强大的对象关系映射(ORM)框架,它简化了数据库操作,使得开发者能够用面向对象的方式处理数据。Hibernate 3.x 版本是其成熟且广泛使用的版本,提供...