- 浏览: 188401 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
Errorize:
一个用servlet实现导出csv文件的实例 -
wendellhu:
求monkey talk应用文档的解压密码
monkey talk应用文档 -
wendellhu:
加密的?
monkey talk应用文档 -
hugang357:
hslh22 写道不错的资源,可以使用,只是没有最大最小值的限 ...
数字微调输入框 -
hslh22:
不错的资源,可以使用,只是没有最大最小值的限制,需要自己去加逻 ...
数字微调输入框
请您先登录,才能继续操作
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)
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)
发表评论
-
struts2.1.8 + spring2.5.6 + hibernate3.3.2 + TOMCAT 官方网站
2011-05-26 19:36 1545下面为3层框架他们的官网地址 给各位eye友提供帮助 希望你们 ... -
Hibernate映射的基本操作
2009-12-07 17:31 980Hibernate映射主要是通过对象关 ... -
spring 的 AOP 事务
2009-11-26 15:35 1314今天对 spring 的 AOP 事务有了 ... -
数据库设计多对多关系的几种形态
2009-11-24 21:21 2115数据库设计多对多 ... -
Hibernate Query Language(HQL)
2009-11-24 21:04 1172Hibernate Query Language(HQ ... -
事务隔离
2009-11-24 20:52 946事务隔离意味着对于某 ... -
事务的特性
2009-11-24 20:49 1196事务具备原子性(Atomicity)、一致性(Consiste ... -
利用关联关系操纵对象
2009-11-24 19:45 854利用关联关系操纵对象 数据对象之间关联关系有一对一 ... -
一对一关联关系的使用
2009-11-24 19:41 1146一对一关系在实际生活中是比较常见的,例如学生与学生证的关 ... -
一对多关联关系的使用
2009-11-24 19:40 917一对多关联关系的使用: 一对多关系很常见,例如父亲和 ... -
多对多关联关系的使用
2009-11-24 19:37 948Student(学生)和Course(课程)的关系就是 ...
相关推荐
Hibernate中cascade与inverse属性详解
### Hibernate Inverse 和 Cascade 的详细讲解 #### 一、引言 在ORM(Object-Relational Mapping)领域,Hibernate作为一款流行的Java持久层框架,它提供了丰富的API和配置选项来帮助开发者实现对象与数据库表之间...
在深入探讨Hibernate集合映射中的`inverse`与`cascade`属性之前,我们首先需要理解Hibernate框架的基本概念。Hibernate是一个开放源代码的对象关系映射(ORM)框架,它为Java应用程序提供了一种将对象模型与数据库...
本文将深入探讨Hibernate中的一对多关系,并重点解析`inverse`属性的作用及其应用。 一对多关系是数据库设计中常见的关联类型,例如一个用户可以拥有多个订单,一个班级可以包含多个学生等。在Hibernate中,通过...
### Hibernate中的Cascade选项详解 #### 一、概述 在Hibernate框架中,cascade选项提供了一种管理实体间关联的便捷方式,使得对一个实体的操作能够自动地应用到与之相关的其他实体上。这对于处理复杂的数据库关系...
在Hibernate的XML映射文件中,集合类型的关联关系经常使用`<set>`标签,如`<set>`下的`name`属性指定集合属性名,`table`属性指定中间表(多对多关系时),`inverse`属性表示维护关系的责任方,`cascade`属性控制...
<set name="orders" cascade="all" inverse="true"> </hibernate-mapping> ``` 在`Orders.hbm.xml`中,我们需要指定`Customers`作为其外键的实体类: ```xml <!DOCTYPE hibernate-mapping PUBLIC "-//...
hibernate中一对一,一对多,多对多关系的配置,延迟加载,cascade,inverse hibernate查询方式概述,HQL查询,QBC查询,分页,结果集封装方式 ,高级查询 查询的优化,一级缓存,二级缓存,批量查询,注解方式
### Hibernate常用注解详解 #### 一、JPA与Hibernate注解基础 JPA(Java Persistence API)是一种标准规范,用于实现对象关系映射(ORM),允许开发人员使用注解或XML来描述实体对象与数据库表之间的映射关系。...
### Hibernate配置详解(二)——深入理解`.hbm.xml`文件与实体关联 #### 引言 在前文《Hibernate 配置详解》中,我们已经初步探讨了Hibernate框架的基本配置,包括`hibernate.cfg.xml`文件的重要性及其在...
以下是对Hibernate一对多映射配置的详解: 一、XML文件配置 1. 单向关联 在这种配置中,关系只在一个方向上定义,例如User类有多个Account。在User的配置文件中,不直接声明与Account的关联,而是在Account的配置...
【Hibernate配置文件中映射元素详解】 在对象关系映射(ORM)框架Hibernate中,对象与数据库表之间的关联是通过一个XML配置文件来定义的。这个XML文件包含了映射元素,用于描述Java类如何转化为数据库中的表。映射...
**Hibernate 结构框架详解** Hibernate 是一款流行的 Java ORM(对象关系映射)框架,它允许开发者将数据库操作转化为对 Java 对象的操作,简化了数据库应用的开发。在深入分析 Hibernate 结构框架之前,我们先来...
### Hibernate中的Inverse属性详解 在Hibernate框架中,`inverse`属性是用于管理对象关系的一方在持久化操作中是否承担删除或更新关联的责任。当两个实体之间存在双向关联时(如一对多或许多对一的关系),`inverse...
- `<one-to-one>`、`<many-to-one>`、`<one-to-many>`和`<many-to-many>`:分别用于一对一、一对多、多对一和多对多的关系映射,通过`<class>`元素的嵌套和属性如`inverse`、`cascade`等来控制关系的维护方式和级联...
### Hibernate关系映射详解 #### 一、引言 Hibernate是一种广泛使用的Java持久化框架,它简化了对象关系映射(Object-Relational Mapping,简称ORM)的过程,使得开发人员可以更加专注于业务逻辑的实现而无需过多...