何时在 NHibernate/Hibernate OneToMany 关系上使用 inverse=false? [英] When to use inverse=false on NHibernate / Hibernate OneToMany relationships?

查看:31
本文介绍了何时在 NHibernate/Hibernate OneToMany 关系上使用 inverse=false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试掌握 Hibernate 的 inverse 属性,它似乎只是概念上困难的事情之一.

I have been trying to get to grips with Hibernate's inverse attribute, and it seems to be just one of those things that is conceptually difficult.

我得到的要点是,当您有一个使用 一对多 映射的具有子对象集合的父实体(例如父实体)时,在映射上设置 inverse=true告诉 Hibernate '另一方(孩子)有责任更新自己以维护其表中的外键引用'.

The gist that I get is that when you have a parent entity (e.g. Parent) that has a collection of Child objects using a one-to-many mapping, setting inverse=true on the mapping tells Hibernate that 'the other side (the Child) has responsibility to update itself to maintain the foreign key reference in its table'.

这样做似乎有 2 个好处,即在您的代码中将子项添加到集合中,然后保存父项(使用级联所有设置):你在数据库上保存了一个不必要的命中(因为没有逆集,Hibernate认为它有两个地方可以更新FK关系),并且根据官方文档:

Doing this appears to have 2 benefits when it comes to adding Children to the collection in your code, and then saving the Parent (with cascade-all set): you save an unneccessary hit on the database (because without inverse set, Hibernate thinks it has two places to update the FK relationship), and according to the official docs:

如果一列协会已声明NOT NULL, NHibernate 可能导致创建时违反约束或更新关联.阻止这个问题,你必须使用双向关联许多有价值的结束(套装或包)标记为 inverse="true".

If the column of a association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true".

到目前为止,这一切似乎都有意义.我不明白的是:您什么时候想在一对多关系中使用 inverse=true ?

This all seems to make sense so far. What I don't get is this: when would you NOT want to use inverse=true on a one-to-many relationship?

推荐答案

正如 Matthieu 所说,您不想设置 inverse = true 的唯一情况是孩子负责更新没有意义本身,例如在孩子不知道其父母的情况下.

As Matthieu says, the only case where you wouldn't want to set inverse = true is where it does not make sense for the child to be responsible for updating itself, such as in the case where the child has no knowledge of its parent.

让我们尝试一个真实的世界,而不是人为的例子:

Lets try a real world, and not at all contrived example:

<class name="SpyMaster" table="SpyMaster" lazy="true">
  <id name="Id">
    <generator class="identity"/>
  </id>
  <property name="Name"/>
  <set name="Spies" table="Spy" cascade="save-update">
    <key column="SpyMasterId"/>
    <one-to-many class="Spy"/>
  </set>
</class>

<class name="Spy" table="Spy" lazy="true">
  <id name="Id">
    <generator class="identity"/>
  </id>
  <property name="Name"/>
</class>

间谍大师可以有间谍,但间谍永远不知道他们的间谍大师是谁,因为我们没有在间谍类中包含多对一的关系.此外(方便地)间谍可能会变成流氓,因此不需要与间谍大师相关联.我们可以按如下方式创建实体:

Spymasters can have spies, but spies never know who their spymaster is, because we have not included the many-to-one relationship in the spy class. Also (conveniently) a spy may turn rogue and so does not need to be associated with a spymaster. We can create entities as follows:

var sm = new SpyMaster
{
    Name = "Head of Operation Treadstone"
};
sm.Spies.Add(new Spy
{
    Name = "Bourne",
    //SpyMaster = sm // Can't do this
});
session.Save(sm);

在这种情况下,您会将 FK 列设置为可空,因为保存 sm 的行为会插入到 SpyMaster 表和 Spy 表中,只有在此之后它才会更新 Spy 表以设置 FK.在这种情况下,如果我们设置 inverse = true,FK 将永远不会更新.

In such a case you would set the FK column to be nullable because the act of saving sm would insert into the SpyMaster table and the Spy table, and only after that would it then update the Spy table to set the FK. In this case, if we were to set inverse = true, the FK would never get updated.

这篇关于何时在 NHibernate/Hibernate OneToMany 关系上使用 inverse=false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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