2013-02-11 104 views
0

我正在使用SQL視圖,並且我對NHibernate,視圖或一般數據庫知之甚少,無法知道問題出在哪裏。我正在使用的視圖曾經有一個字符串列「ExemptionCode」。現在,該視圖可能會有許多免除代碼。下面是新的關係的XML映射:NHibernate:在SQL視圖中映射一對多關係

<class name="LatestDocumentVersion" table="LatestDocumentVersion" mutable="false" schema-action="none"> 
    <id name="DocumentVersionID" type="Int32"/>   
    <property name="ContainerDocumentID" type="Int32"/> 
    <!--<property name="ExemptionCode" length="10" />--> 
    <set name="ExemptionCodes" cascade="all-delete-orphan" lazy="false" inverse="false"> 
     <key column="ContainerDocumentID"/> 
     <one-to-many class="ContainerDocumentExemptions"/> 
    </set> 
    <--Properties omitted--> 
</class> 

下面是ContainerDocumentExemptions類的映射:

<class name ="ContainerDocumentExemptions" lazy ="false"> 
    <id name ="ContainerDocumentExemptionID" type="Int32"> 
    <generator class="identity"/> 
    </id> 
    <many-to-one name="ContainerDocumentID" class="ContainerDocuments" column="ContainerDocumentID" cascade="none"/> 
    <property name="ExemptionCode" length="10"/> 
</class> 

的ContainerDocumentExemption類實際上有一個雙向一個一對多的關係ContainerDocument對象。這裏的另一端:

<class name="ContainerDocuments" lazy="false" table="ContainerDocuments"> 
    <id name="ContainerDocumentID" type="Int32"> 
     <generator class="identity"/> 
    </id> 
    <!--<property name="ExemptionCode" length="10" />--> 
    <set name="Exemptions" cascade="all-delete-orphan" lazy="false" inverse="true"> 
     <key column="ContainerDocumentID"/> 
     <one-to-many class="ContainerDocumentExemptions"/> 
    </set> 
    <--Properties omitted--> 
</class> 

加入這行來ContainerDocuments下課後,ContainerDocuments可以正確地寫入和新ContainerDocumentExemptions表閱讀:

public class ContainerDocuments { 
    public virtual ISet<ContainerDocumentExemptions> Exemptions { get; set; } 
    //Properties omitted 
} 

所以,我將此代碼添加到LatestDocumentVersion類:

public class LatestDocumentVersion { 
    public virtual int ContainerDocumentID { get; set; } 
    public virtual ISet<ContainerDocumentExemption ExemptionCodes { get; set; } 
    //properties omitted 
} 

LatestDocumentVersion是執行內部連接和上一堆不同表的外連接的視圖,並且需要一個包子從每個不同的列。 (創建視圖的SQL非常複雜,並且希望與手頭的問題無關。)新添加的LatestDocumentVersion.ContainerDocumentID(它是ContainerDocumentExemptions表中的外鍵)始終正確填充。但是,ExemptionCodes集合始終爲空。

我有一種感覺,問題的一部分是ContainerDocumentExemptions類中的ContainerDocument反向引用。這可否阻止我在LatestDocumentVersion類中使用相同的映射?我認爲如果這是一個問題,那麼使單向的LatestDocumentVersion-ContainerDocumentExemptions關係可以緩解這個問題。那麼如何填充LatestDocumentVersion.ExemptionCodes字段?任何人至少能給我提示如何調試問題?

回答

1

ContainerDocumentIDContainerDocument類中的標識,但它不在LatestDocumentVersion類中,默認情況下每個一對多都加入到標識中。將property-ref添加到LatestDocumentVersion映射

<class name="LatestDocumentVersion" table="LatestDocumentVersion" mutable="false" schema-action="none"> 
    <id name="DocumentVersionID" /> 
    <property name="ContainerDocumentID"/> 
    <set name="ExemptionCodes" cascade="all-delete-orphan" lazy="false" inverse="false"> 
     <key column="ContainerDocumentID" property-ref="ContainerDocumentID"/> 
     <one-to-many class="ContainerDocumentExemptions"/> 
    </set> 
</class> 
+0

成功!非常感謝! – 2013-02-14 13:03:14