我是新来NHibernate和我使用NHibernate 2.1.0 RC1。在C#中,我有以下类别:需要帮助NHibernate的映射(父/子关系)
public class Application
{
public virtual int Id { get; set; }
public virtual Applicant Applicant { get; set; }
}
public class Applicant
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual IList<Application> Applications { get; set; } //maybe i should use set to eliminate duplicates
}
而且我在SQL Server 2005中的下列数据库模式:
Applications table
{
ApplicationId int PK IDENTITY NOTNULL
FK_ApplicantId int FK NOTNULL
}
Applicants table
{
ApplicantId int PK IDENTITY NOTNULL
FirstName string NOTNULL
LastName string NOTNULL
}
而且我有以下NHibernate的映射文件:
我需要双向映射:
- 1申请人可以有> 1级的应用
- 1申请属于1名申请人
我不知道如何应用集合映射到申请人。请帮忙。谢谢! 另外我不使用Fluent Nhibernate,因为它似乎不支持Nhibernate 2.1.0 RC1。
更新(这是工作版本):
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="HelloWorld"
namespace="HelloWorld">
<class name="Application" table="Applications">
<id name="Id" column="ApplicationID" />
<property name="Reference" />
<many-to-one name="Applicant" column="ApplicantID" not-null="true"/>
</class>
<class name="Applicant" table="Applicants">
<id name="Id" column="ApplicantID" />
<property name="FirstName" column="FirstName" />
<property name="LastName" column="LastName" />
<set name="Applications" inverse="true"> <!-- good to remove this mapping -->
<key column="ApplicantID"/>
<one-to-many class="Application"/>
</set>
</class>
</hibernate-mapping>
我也将消除从申请对象的应用程序集合,以减少负载成千上万的应用程序,申请人提交的变化。 Reason for this is here.。
见https://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html – 2009-07-10 04:01:57
我读它谢谢。完成后我会发布更新后的映射文件。 – Jeff 2009-07-10 04:12:22