级联和级联有什么区别?inverse in hibernate,它们有什么用? [英] What is the difference between cascade & inverse in hibernate, what are they used for?

查看:30
本文介绍了级联和级联有什么区别?inverse in hibernate,它们有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在hibernate中使用cascade和inverse?定义它们的程序/标签是什么?它们是否相互关联?它们有何用处?

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

例如假设以下场景1 名学生可以拥有多部手机.因此 Student 类具有 Set of phone 的属性.1 部手机也可以由多个学生拥有.所以电话类具有学生集的属性.这个映射在stud_phone 表中提到.

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.

所以有三个表即.Student、Phone 和stud_phone(中介)表.映射可能如下所示:

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> 

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

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 和 INVERSE 是假的

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 为 NONE,INVERSE 为真

2) CASCADE is NONE and INVERSE is true

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

3) CASCADE 是保存更新,INVERSE 是假的

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 为 save-update 和 INVERSE 为真

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 (?, ?)

可以看出,只有当 CASCADE 是 save-update 时,才会在 PHONE 表中创建记录.否则不行.

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

当 INVERSE 为假(即学生是关系的所有者)时,中间表 STUD_PHONE 被更新.当 inverse 为真时,Phone 是关系的所有者,因此即使创建了一个新学生,中间表也没有更新.

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.

这篇关于级联和级联有什么区别?inverse in hibernate,它们有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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