需要帮助与NHibernate建立一对多的关系

时间:2011-11-04 21:05:41

标签: c# .net nhibernate

以下是我的对象的映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernateTest"
                   namespace="NHibernateTest.Database">
    <class name="Employer">
        <id name="Id" type="Int64" column="id" unsaved-value="0">
            <generator class="identity"/>
        </id>
        <property name="Name"/>
    </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernateTest"
                   namespace="NHibernateTest.Database">
    <class name="Employee">    
        <id name="Id" type="Int64" column="id" unsaved-value="0">
            <generator class="identity"/>
        </id>
        <property name="FirstName"/>
        <property name="LastName"/>
        <property name="Email"/>
    </class>
</hibernate-mapping>

以下是对象本身:

namespace NHibernateTest.Database {
    public class Employee {
        public virtual long Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string Email { get; set; }
    }

    public class Employer {
        public virtual long Id { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<Employee> Employees { get; set; }
    }
}

我正在尝试使用SchemaExport实用程序生成架构,但无法弄清楚如何编写IList的映射。自从我接触到Hibernate for Java以来​​,已经有很长一段时间了,我发现NHibernate的一对多文档有点难以理解。如果有人能写出这个映射,我会非常感激。

1 个答案:

答案 0 :(得分:1)

我不得不猜测Employee表和Employer.Id外键的名称,但这是我将如何做的:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernateTest"
                   namespace="NHibernateTest.Database">
    <class name="Employer">
        <id name="Id" type="Int64" column="id" unsaved-value="0">
            <generator class="identity"/>
        </id>
        <property name="Name"/>
        <bag name="Employees" table="Employees" >
            <key column="EmployerId"></key>
            <one-to-many class="Employee"></one-to-many>
       </bag>
    </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernateTest"
                   namespace="NHibernateTest.Database">
    <class name="Employee">    
        <id name="Id" type="Int64" column="id" unsaved-value="0">
            <generator class="identity"/>
        </id>
        <property name="EmployerId" />
        <property name="FirstName"/>
        <property name="LastName"/>
        <property name="Email"/>
    </class>
</hibernate-mapping>

这是.net代码:

namespace NHibernateTest.Database {
    public class Employee {
        public Employee(Employer employer) { EmployerId = employer.Id; }
        protected Employee() {} // nHibernate needs access to a parameterless constructor.
        public virtual long Id { get; set; }
        protected virtual long EmployerId { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string Email { get; set; }
    }

    public class Employer {
        public virtual long Id { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<Employee> Employees { get; set; }
    }
}