如何在NHibernate中映射自引用

时间:2021-02-28 11:24:29

I have a very special NHibernate mapping case. The class has a reference to itself.

我有一个非常特殊的NHibernate映射案例。该类有自己的参考。

public class MyClass
{
   public Guid Id { get; set; }
   public MyClass SelfReference { get; set; }
}

The data base table has a foreign key field on the primary key of the same table. And event worse, this self reference can be null.

数据库表在同一个表的主键上有一个外键字段。更糟糕的是,这种自我引用可以为空。

Is that possible to map and how can this be done?

这有可能映射,如何做到这一点?

4 个解决方案

#1


one-to-one can be used to synchronize primary keys and is used rarely, in my experience many-to-one is the most "natural" association for "normal" references:

一对一可用于同步主键并且很少使用,根据我的经验,多对一是“正常”引用的最“自然”关联:

<many-to-one name="SelfReference" class="MyClass" column="SelfReference_FK" />

#2


Seems like you're willing to map a tree:

好像你愿意映射一棵树:

http://nhibernate.hibernatingrhinos.com/16/how-to-map-a-tree-in-nhibernate

#3


Seems like you'd just treat it the same as any other one-to-one relationship?

好像你只是把它当作任何其他一对一关系一样对待它?

#4


i think its a simple one-to-one.

我认为这是一个简单的一对一。

i hope this will help u

我希望这会对你有所帮助

<one-to-one
name="PropertyName"
class="ClassName"
cascade="all|none|save-update|delete"
constrained="true|false"
fetch="join|select"
property-ref="PropertyNameFromAssociatedClass"
access="field|property|nosetter|ClassName"/>

#1


one-to-one can be used to synchronize primary keys and is used rarely, in my experience many-to-one is the most "natural" association for "normal" references:

一对一可用于同步主键并且很少使用,根据我的经验,多对一是“正常”引用的最“自然”关联:

<many-to-one name="SelfReference" class="MyClass" column="SelfReference_FK" />

#2


Seems like you're willing to map a tree:

好像你愿意映射一棵树:

http://nhibernate.hibernatingrhinos.com/16/how-to-map-a-tree-in-nhibernate

#3


Seems like you'd just treat it the same as any other one-to-one relationship?

好像你只是把它当作任何其他一对一关系一样对待它?

#4


i think its a simple one-to-one.

我认为这是一个简单的一对一。

i hope this will help u

我希望这会对你有所帮助

<one-to-one
name="PropertyName"
class="ClassName"
cascade="all|none|save-update|delete"
constrained="true|false"
fetch="join|select"
property-ref="PropertyNameFromAssociatedClass"
access="field|property|nosetter|ClassName"/>