0
我正在自定義N2CMS的數據庫結構,並遇到了一個問題。下面列出了這兩個類。Nhibernate一對多每個子類都有表
public class Customer : ContentItem
{
public IList<License> Licenses { get; set; }
}
public class License : ContentItem
{
public Customer Customer { get; set; }
}
nhibernate映射如下。
<class name="N2.ContentItem,N2" table="n2item">
<cache usage="read-write" />
<id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
<generator class="native" />
</id>
<discriminator column="Type" type="String" />
</class>
<subclass name="My.Customer,My" extends="N2.ContentItem,N2" discriminator-value="Customer">
<join table="Customer">
<key column="ItemID" />
<bag name="Licenses" generic="true" inverse="true">
<key column="CustomerID" />
<one-to-many class="My.License,My"/>
</bag>
</join>
</subclass>
<subclass name="My.License,My" extends="N2.ContentItem,N2" discriminator-value="License">
<join table="License" fetch="select">
<key column="ItemID" />
<many-to-one name="Customer" column="CustomerID" class="My.Customer,My" not-null="false" />
</join>
</subclass>
然後,當得到客戶的情況下,customer.Licenses永遠是空的,但實際上是在數據庫中爲客戶許可證。當我檢查了NHibernate的日誌文件,我發現SQL查詢是這樣的:
SELECT licenses0_.CustomerID as CustomerID1_,
licenses0_.ID as ID1_,
licenses0_.ID as ID2_0_,
licenses0_1_.CustomerID as CustomerID7_0_,
FROM n2item licenses0_
inner join License licenses0_1_
on licenses0_.ID = licenses0_1_.ItemID
WHERE licenses0_.CustomerID = 12 /* @p0 */
看來,NHibernate的認爲,在客戶是在「n2item」表。我不知道爲什麼,但爲了使它工作,我認爲SQL應該是這樣的。
SELECT licenses0_.ID as ID1_,
licenses0_.ID as ID2_0_,
licenses0_1_.CustomerID as CustomerID7_0_,
FROM n2item licenses0_
inner join License licenses0_1_
on licenses0_.ID = licenses0_1_.ItemID
WHERE licenses0_1_.CustomerID = 12 /* @p0 */
難道有人指出我的映射有什麼問題嗎?我怎樣才能獲得一個客戶的正確許可?提前致謝。
感謝梅森,我想我應該堅持使用內置的n2數據庫結構,因爲定製的有很多問題。 – wenqiang 2010-04-12 09:41:29
子類映射沒有問題我添加了一對多元素。而且我已經嘗試了一對多和包包元素上的幾乎所有屬性。沒有用。 – wenqiang 2010-04-12 09:42:58