- 浏览: 97145 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
Storm_Four:
...
异常的概念和Java异常体系结构 -
java苹果+番茄:
...
Hibernate的merge与update方法的区别 -
shlei:
这让我对IBM的架构有了比较多的认识,谢谢。
今日IBM ( IBM china / IGSC / ISSC / ETC ) -
dong_w_0418:
如果没有对应的记录,merge会把该记录当作新的记录来插入。此 ...
Hibernate的merge与update方法的区别 -
blue3377:
呵呵,感谢。注册成功啦
myeclipse 6.0 注册码 (有效期至2012年)
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)
发表评论
-
hibernate的各种保存方式的区别
2009-11-25 00:44 962hibernate的保存 hibernate对于对象的保存提供 ... -
Hibernate中dynamic-insert和dynamic-update
2009-11-24 20:51 1395Hibernate允许我们在映射文件里控制insert和upd ... -
Hibernate级联删除的疑问
2009-11-23 21:43 1177今天做了个测试,一共两张表,person和order,pers ... -
Hibernate的merge与update方法的区别
2009-11-22 21:13 9560今天做了个测试,写了个测试用例来看看merge与update时 ... -
Hibernate中的实体状态
2009-11-22 19:51 8651.临时状态和游离状态都不在session的生命周期中. ... -
Hibernate中get()与load(),query.list()与query.iterate()区别
2009-11-19 17:50 2259Hibernate中有两个极为相似的方法get()与load( ... -
Hibernate的session.flush做了什么呢?
2009-11-14 20:44 877这是在一次事务提交时 ... -
Hibernate3中取得多层数据的所产生的n+1 selects问题的解决
2009-11-11 14:00 776Hibernate3中取得多层数据 ... -
Hibernate缓存学习笔记
2009-10-20 14:37 934一级缓存(在同一个Session中,缓存实体对象,生命周期与S ... -
hibernate.hbm2ddl.auto配置
2009-10-20 14:20 1019hibernate.cfg.xml 中hibernate.hb ...
相关推荐
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)的过程,使得开发人员可以更加专注于业务逻辑的实现而无需过多...