简体   繁体   English

Nhibernate一对一地映射类与其自身

[英]Nhibernate one-to-one mapping a class with itself

We have a situation where we want to define a relationship where a class (named Module) may or may not be related to a Module object that is a predecessor to it. 在一种情况下,我们想定义一个关系,其中一个类(名为Module)可能与该类的前一个Module对象有关,也可能不相关。 There can be zero or none predecessors. 可以有零个或没有前任。 The class looks like this: 该类如下所示:

public class Module
{
    public int Id
    {
        get;
        set;
    }

    // other stuff here

    public Module Predecessor
    {
        get;
        set;
    }
}

And we have defined our mapping so that Predecessor is a property of type Module like so: 并且我们已经定义了映射,以便Predecessor是Module类型的属性,如下所示:

<class name="Module">
    <Id name="Id">
        <generator class="native/>
    </Id
    <property name="Predecessor" type="Module" "unique="true"/>
<class>

However we are getting complaints about the mapping not being able to compile because it cannot find the type "Module". 但是,由于映射无法找到类型“ Module”,因此我们抱怨它无法编译。 We have tried the long name for the class 我们已经尝试了班级的长名

type="STC.EI.JobSubmissionSystem.Data.Domain"

and the fully-qualified name for the class 以及班级的全名

type="STC.EI.JobSubmissionSystem.Data.Domain, STC.EI.JobSubmissionSystem.Data"

to no avail. 无济于事。 My question is: 我的问题是:

Are we mapping this properly, and if not then how do we map it properly? 我们是否正确映射了它,如果不正确,那么如何正确映射呢?

You could use the many-to-one element: 您可以使用多对一元素:

<class name="Module">
    <Id name="Id">
        <generator class="native"/>
    </Id>
    <many-to-one name="Predecessor" class="Module" column="predecessor_id" />
<class>

Note that you need a column in your table to define the relation. 请注意,您需要在表中的一列中定义关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM