Java hibernate中的cascade和inverse有什么区别,它们有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3667387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:19:08  来源:igfitidea点击:

What is the difference between cascade & inverse in hibernate, what are they used for?

javahibernatecascadeinverse

提问by Chandra Sekhar

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?

如何在休眠中使用级联和反向?定义它们的程序/标签是什么?它们是否相互关联,它们有何用处?

采纳答案by Kaushik Lele

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.

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

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

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

<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.

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

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

以下是级联和逆的不同组合及其影响。

1) CASCADE IS NONE and INVERSE is false

1) CASCADE IS NONE 和 INVERSE 为假

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

2) CASCADE 为 NONE 且 INVERSE 为真

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

3) CASCADE is save-update and INVERSE is false

3) CASCADE 为保存更新,INVERSE 为假

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

4) CASCADE 是 save-update 和 INVERSE 真

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.

可以看出,只有当 CASCADE 是 save-update 时,才会在 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.

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

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

因此,在两个实体的关系的情况下,“级联”影响其他实体表,“逆”影响中间表。所以它们的作用是独立的。

回答by YoK

Information referenced from Different between cascade and inverselink:

级联和反向链接的区别引用的信息:

1. inverse:This is used to decide which side is the relationship owner to manage the relationship (insert or update of the foreign key column).

2. cascade:In cascade, after one operation (save, update and delete) is done, it will decide whether it need to call other operations (save, update and delete) on another entities which has relationship with each other.

Conclusion:In short, the “inverse” decides which side will update the foreign key, while “cascade” decides what's the follow by operation should execute. Both look quite similar in relationship, but it's totally two different things. Hibernate developers are worth to spend time to research on it, because misunderstanding the concept or misusing it will bring serious performance or data integrity issue in their application.

1.inverse:这个用来决定哪一方是关系所有者来管理关系(插入或更新外键列)。

2. 级联:在级联中,在一个操作(保存、更新和删除)完成后,它会决定是否需要对另一个相互关联的实体调用其他操作(保存、更新和删除)。

结论:简而言之,“逆”决定哪一方更新外键,而“级联”决定后续操作应该执行的内容。两者的关系看起来很相似,但完全是两种不同的东西。Hibernate 开发人员值得花时间研究它,因为误解或滥用它会在他们的应用程序中带来严重的性能或数据完整性问题。

Also check this forum topic: https://forum.hibernate.org/viewtopic.php?f=1&t=949041

另请查看此论坛主题:https: //forum.hibernate.org/viewtopic.php?f=1&t=949041

回答by Kaushik Lele

These are orthogonal concepts.

这些是正交概念。

In associations, one of the side must be marked as inverse by using inverseattribute or mappedByattribute (manyside in one-to-many/ many-to-oneassociation and either side in many-to-manyassociation). This information is needed for Hibernate to correctly determine, how Java classes (object-oriented association) will be mapped to the database tables(relational association).

在关联中,必须使用inverse属性或mappedBy属性(manyside in one-to-many/ many-to-oneassociation 和任一侧 in many-to-manyassociation)将一侧标记为反向。Hibernate 需要此信息才能正确确定 Java 类(面向对象的关联)将如何映射到数据库表(关系关联)。

What about cascading - you can explicitly specify for Hibernate to perform operations on associated entities:

级联怎么样 - 您可以明确指定 Hibernate 对关联实体执行操作:

  • CascadeType.PERSIST- When the save()or persist()method is called for the owner, all the associated entities are also saved;
  • CascadeType.REMOVE- When the delete()method is called for the owner, all the associated entities are also deleted;
  • CascadeType.MERGE- When the merge()method is called for the owner, all the associated entities are also merged to managed/ persisted state;
  • CascadeType.REFRESH- When the refresh()method is called for the owner, all the associated entities are also refreshing from their database representation;
  • CascadeType.DETACH- When the session with which this entity was associated is closed, all the related entities would in the detached state;
  • CascadeType.ALL- Includes all the cascade operations;
  • "orphan removal" - removes associated entity from the database, when this entity removed from the relationship.
  • CascadeType.PERSIST- 当为所有者调用save()orpersist()方法时,所有关联的实体也被保存;
  • CascadeType.REMOVE- 当delete()为所有者调用该方法时,所有关联的实体也被删除;
  • CascadeType.MERGE- 当merge()为所有者调用该方法时,所有关联的实体也被合并到托管/持久化状态;
  • CascadeType.REFRESH- 当refresh()为所有者调用该方法时,所有关联的实体也从它们的数据库表示中刷新;
  • CascadeType.DETACH- 当与该实体关联的会话关闭时,所有相关实体都将处于分离状态;
  • CascadeType.ALL- 包括所有级联操作;
  • “孤儿删除” - 当该实体从关系中删除时,从数据库中删除关联实体。