我在我最近的項目中使用了nHibernate,併成功映射了所有基本關係,其中值存在於我正在使用的主表中,或者通過像複合這樣的簡單關係存在。映射nHibernate中的複雜連接
我陷入困境的地方在於如何去映射覆雜的連接?
比如我有一個名爲實體Contact
每個contact
有你平時的屬性,如姓名,出生日期,電話....但我也需要它有一個叫做AccreditationList
屬性,它會是一個List<Accreditation>
。
以下是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的手動調用SQL代碼與CreateSQLQuery
或者我可以表達使用類似命名查詢這種關係來實現這一目標的唯一途徑?什麼是正確的方法?任何指導將不勝感激。