什么时候使用inverse = false对NHibernate / Hibernate OneToMany关系? [英] When to use inverse=false on NHibernate / Hibernate OneToMany relationships?

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

问题描述

我一直在试图掌握Hibernate的逆属性,它似乎只是其中一个在概念上很困难的事情之一。

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.

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

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

当您在代码中将Children添加到集合时,这似乎有两个好处,然后保存Parent(使用cascade-all集合):您保存对数据库的不必要的命中(因为没有反向设置,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的唯一情况是,孩子不必负责更新自己,这是没有意义的,例如在case

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可以有间谍,但间谍从来不知道他们的spymaster是谁,因为我们没有包括很多在间谍类中的一对一关系。此外(方便地)间谍可以变成流氓,因此不需要与管理员关联。我们可以创建如下的实体:

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.

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

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