具有相同类型属性的NHibernate映射类

具有相同类型属性的NHibernate映射类

本文介绍了具有相同类型属性的NHibernate映射类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.NET MVC的新手。然而,这个问题我看起来很常见,我找不到任何教程或stackoverflow线程,解释如何正确地做到这一点。

I am new to the .NET MVC. However this "problem" I am stuck at looks pretty common, I cannot find any tutorial or stackoverflow thread that explains how to do it properly.

我有一个类,MyClass有两个相同类型的属性

I have a class, MyClass which has two attributes of same type

 public class MyClass : IEquatable<MyClass>
 {
     public virtual MyClass LeftChild { get; set; }
     public virtual MyClass RightChild { get; set; }
     ...
 }

现在我遇到了nhibernate映射问题。起初我尝试了一对一的映射。我创建了新实例,并且没有设置Childs,持久化它(假设Id = 1),并将此实例传递给View,我预计RightChild将为NULL,LeftChild将为NULL。但是在debbug模式中,我可以看到,RightChild设置为MyClass,Id = 1(像MyClass实例一样设置为此属性),与LeftChild相同。

Now I have problem with nhibernate mapping. At first I tried one-to-one mapping. I created new instance and DO NOT set Childs , persisted it (lets say Id=1), and pass this instance to View and I expected that RightChild will be NULL and LeftChild will be NULL. But in the debbug mode i can see, that the RightChild was set to MyClass with Id=1 (Like MyClass instance set itself to this attribute) and same with LeftChild.

映射MyClass.hbm.xml

Mapping MyClass.hbm.xml

...
<one-to-one name="LeftChild" class="MyClass"/>
<one-to-one name="RightChild" class="MyClass"/>
...

采用一对一或者一对一的做法是正确的吗?我应该使用其他东西吗?

Is it right approach to do it with one-to-one or I should use something else ?

推荐答案

我们的表包含外键的引用几乎总是最好用<$ c映射$ C>多到一个。

References, where our table contains foreign keys, are almost always best to map with many-to-one.

只需将其视为对其他实例(国家货币)的标准引用...这是意外的同一类型。

Simply think about it as a standard reference to other instance (Country, Currency)... which is accidentally of a same type.

<many-to-one name="LeftChild"  column="LeftChild_ID"  class="MyClass"/>
<many-to-one name="RightChild" column="RightChild_ID" class="MyClass"/>

我看到的唯一挑战是确保服务器部分(C#代码,应用程序)将正确设置这些值。这种持久化信息中没有双向映射。每个兄弟都需要自己留下的信息。

The only challenge I see, is to be sure that the server part (C# code, app) will be properly setting these values. There is not bi-directional mapping in this kind of persisted information. Each sibling, needs its own information who is right who is left.

我的意思是,与类似的映射比较:父子(也是同一类型)。在这种情况下,我们将有孩子参考父母,父母有孩子的集合。

I mean, comparing with Similar mapping: parent-child (also same type). In that case we would have child having reference to parent, and parent having collection of children.

但这里的情况不一样..再次..我们只映射关系的一侧。

But that is not the same here.. again.. we map just one side of the relation.

一对一在这里不合适,因为它需要两个表,(几乎)相同的行数,共享相同的列...我喜欢使用它,但是对于附加信息......请参阅:

A one-to-one is not suitable here, because it requires two tables, with (almost) the same amount of rows, sharing same column as a key... I like to use it, but for kind of Additional info... see:




  • NHibernate Dynamic Columns Number
  • NHibernate Optional Join generates insert instead of update

这篇关于具有相同类型属性的NHibernate映射类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:19