简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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