cascade& amp; amp;&在休眠中反向,它们用于什么? [英] What is the difference between cascade & inverse in hibernate, what are they used for?

查看:135
本文介绍了cascade& amp; amp;&在休眠中反向,它们用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在hibernate中使用级联和反转?
定义它们的过程/标签是什么?
它们是否相互关联以及它们如何有用?

解决方案

如果存在多对多关系通过中介表; Cascade表示是否在子表中创建/更新记录。而反向表示记录是否将在中间表中创建/更新



例如。假设下面的情景
1学生可以有多个电话。所以学生类拥有Set of phones的属性。
另外1部电话可以由多名学生拥有。所以电话班有属性的学生。
这个映射在stud_phone表中提到。



所以有三个表格,学生,电话和stud_phone(中介)表。
映射可能如下所示:

 < set name =phonesettable =stud_phonecascade =save -updateinverse =true> 
< key column =mapping_stud_id>< /键>
< / set>

创建一个新学生对象并将2个新手机对象添加到其集合中。并调用 session.save(student_obj)
根据级联和反向设置,不同的查询将被触发。



以下是级联和反转以及它们的影响的不同组合。



1)CASCADE为NONE且INVERSE为false

  Hibernate:insert into STUDENT (Name,stud_id)values(?,?)
Hibernate:插入stud_phone(mapping_stud_id,mapping_phon_id)值(?,?)
Hibernate:插入到stud_phone(mapping_stud_id,mapping_phon_id)值(?,? )



2)CASCADE为NONE且INVERSE为真

  Hibernate:insert into STUDENT(Name,stud_id)values(?,?)



3)CASCADE是save-update和INVERSE是false

  Hibernate:insert into STUDENT(Name (?,?)
Hibernate:插入到phone(phone_num,phone_id)值(?,?)
Hibernate:插入到phone(phone_num,phone_id)values(?,?)
Hibernate:插入stud_phone(mapping_stud_id,mapping_phon_id)值(?,?)
Hibernate:插入stud_phone(mapping_stud_id,mapping_phon_id)值(?,?)

4)CASCADE是save-update和INVERSE TRUE
$ b $ pre $ Hibernate:插入STUDENT (?,?)
Hibernate:插入到phone(phone_num,phone_id)值(?,?)
Hibernate:插入到phone(phone_num,phone_id)values(?,? )

可以看出,只有当CASCADE为save-update时,记录才在PHONE表中创建也。



当INVERSE为false(即Student是关系的所有者)时,中介表STUD_PHONE已更新。当inverse为true时,Phone是关系的所有者,所以即使创建了一个新学生,中间表也没有更新。因此,在两个实体关系的情况下,级联影响其他实体表,反向影响中间表。所以他们的效果是独立的。


How to use cascade and inverse in hibernate? What is the procedure/tag to define them? Are they related to each other and how are they useful?

解决方案

In case of many-to-many relation through intermediary table; "Cascade" says whether a record will be created/updated in the child table. Whereas "Inverse" says whether a record will be created/updated in the intermediary table

e.g. Assume below scenario 1 student can have multiple phones. So Student class has property for Set of phones. Also 1 Phone can be owned by multiple students. So Phone class has property for Set of Students. This mapping is mentioned in stud_phone table.

So there are three tables viz. Student, Phone and stud_phone (intermediary) table. Mapping might look like:

<set name="phoneset" table="stud_phone" cascade="save-update" inverse="true">
  <key column="mapping_stud_id">< /key>
  <many-to-many class="com.domain.Phone" column="mapping_phon_id"/>
</set> 

A new student object is created and 2 new phone objects are added to its set. And session.save(student_obj) is called. Depending upon "cascade" and "inverse" settings different queries will be fired.

Below are different combinations of cascade and inverse and their impact.

1) CASCADE IS NONE and INVERSE is false

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?)
Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?)
Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?)

2) CASCADE is NONE and INVERSE is true

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?)

3) CASCADE is save-update and INVERSE is false

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?)
Hibernate: insert into phone (phone_num, phone_id) values (?, ?)
Hibernate: insert into phone (phone_num, phone_id) values (?, ?)
Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?)
Hibernate: insert into stud_phone (mapping_stud_id, mapping_phon_id) values (?, ?)

4) CASCADE is save-update and INVERSE true

Hibernate: insert into STUDENT (Name, stud_id) values (?, ?)
Hibernate: insert into phone (phone_num, phone_id) values (?, ?)
Hibernate: insert into phone (phone_num, phone_id) values (?, ?)

As it can be seen, only when CASCADE was save-update the records were created in PHONE table also. Otherwise not.

When INVERSE was false ( i.e. Student was the owner of relationship ) the intermediary table STUD_PHONE was updated. When inverse is true, Phone is owner of relationship, so even though a new student was created, the intermediary table was not updated.

So in case of relation of two entities, "cascade" affects other entity table and "inverse" affects intermediary table. So their effect is independent.

这篇关于cascade&amp; amp; amp;&amp;在休眠中反向,它们用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆