【发布时间】:2012-09-25 17:37:22
【问题描述】:
我在我的最新项目中使用了 nHibernate,并成功映射了所有基本关系,其中值存在于我正在使用的主表中或通过像复合这样的简单关系。
我遇到困难的地方是如何映射复杂的连接?
例如,我有一个名为 Contact 的实体,每个 contact 都有您常用的属性,如姓名、出生日期、电话...。但我还需要它有一个名为 AccreditationList 的属性,即 @987654324 @。
这是 Contact XML 声明的示例。
<class name="Contact" table="Contact" lazy="true">
<id name="ID" column="ContactID" type="guid">
<generator class="guid" />
</id>
<property name="FirstName">
<column name="FirstName" sql-type="nvarchar(500)" not-null="true" />
</property>
<property name="LastName">
<column name="LastName" sql-type="nvarchar(500)" not-null="true" />
</property>
<bag name="AccreditationList" lazy="true">
//How do I express the relationship here?
</bag>
</class>
List<Accreditation> 只能通过这样的一系列连接来确定。
SELECT Accreditation.* FROM CourseEnrolment
INNER JOIN Course ON Course.CourseID = CourseEnrolment.CourseID
INNER JOIN CourseType ON CourseType.CourseTypeID = Course.CourseTypeID
INNER JOIN Accreditation ON Accreditation.AccreditationID = CourseType.AccreditationID
WHERE CourseEnrolment.ContactID = :ContactID
是通过 nHibernate 在代码中使用 CreateSQLQuery 手动调用 SQL 来完成此操作的唯一方法,或者我可以使用命名查询之类的东西来表达这种关系吗?什么是正确的方法?任何指导将不胜感激。
【问题讨论】:
标签: c# .net nhibernate nhibernate-mapping