2009-09-15 55 views
0

當我保存一個新的報表時,NHibernate插入報表,忽略發佈並嘗試插入UserPublication。然而,SQL然後抱怨違反FK約束。 它就像NHibernate不認爲發佈是新的,即使該行不存在於數據庫中。NHibernate沒有將父插入db

覺得作爲實體關係: 報告可以有許多出版物(刊物屬於報告) 出版物可以有很多UserPublications(UserPublications屬於出版物)

任何想法,我做了什麼錯誤? 在此先感謝。

這裏的映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> 
<class name="Model.Report, Model" table="Report" lazy="true"> 
    <id name="Id" access="property" column="ReportID"> 
    <generator class="assigned"></generator> 
    </id>  
    <property name="DeleteUnread" access="property" /> 
    <property name="Description" access="property" /> 
    <property name="Name" access="property" />  
    <bag name="Publications" access="property" lazy="true" cascade="all-delete-orphan"> 
    <key column="ReportID"/> 
    <one-to-many class="Model.Publication, Model"/>   
    </bag> 
</class> 
</hibernate-mapping> 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> 
    <class name="Model.Publication, Model" table="Publication" lazy="true"> 
    <id name="Id" access="property" column="PublicationID">  
     <generator class="assigned"></generator> 
    </id> 
    <property name="CreatedOn" access="property" /> 
    <property name="FileExtension" access="property" /> 
    <property name="IsDownloaded" access="property" /> 
    <property name="ToBeDownloaded" access="property" /> 
    <property name="Name" access="property"/> 
    <bag name="UserPublications" access="property" lazy="true" cascade="all-delete-orphan">  
     <key column="PublicationID"></key> 
     <one-to-many class="Model.UserPublication, Model" /> 
    </bag> 
    <many-to-one name="Report" class="Model.Report, Model" lazy="false" column="ReportID" not-null="true" cascade="none"> 
    </many-to-one> 
    </class> 
</hibernate-mapping> 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> 
    <class name="Model.UserPublication, Model" table="UserPublication" lazy="true"> 
    <id name="Id" access="property" column="UserPublicationID"> 
     <generator class="native"></generator> 
    </id> 
    <property name="IsFlaggedForDeletion" access="property" column="IsFlaggedForDeletion" /> 
    <property name="HasBeenRead" access="property" column="HasBeenRead" /> 
    <property name="DateReceived" access="property" column="DateReceived" /> 
    <property name="MustRead" access="property" column="MustRead" /> 
    <property name="ShowToolbar" access="property" column="ShowToolbar" /> 
    <property name="MaxAge" access="property" column="MaxAge" /> 
    <property name="FeedId" access="property" column="FeedId" /> 
    <property name="CanEdit" access="property" column="CanEdit" />  
    <many-to-one name="User" access="property" column="ClientUserID" class="Model.ClientUser, Model" not-null="true" cascade="none">  
    </many-to-one> 
    <many-to-one name="Publication" access="property" class="Model.Publication, Model" column="PublicationID" not-null="true" cascade="none">  
    </many-to-one> 
</class> 

回答

0

This Works。我將unsaved-value屬性設置爲「any」。

我不認爲會有任何反彈。

<id name="Id" access="property" column="PublicationID" unsaved-value="any">  
    <generator class="assigned"></generator> 
</id> 
0

我認爲這個問題是出版物的該ID是分配的ID,因此當它應該插入一個發佈的NHibernate無法識別。 刷新會話時,首先插入所有插入的對象,然後更新所有更新的對象,然後刪除所有刪除的對象。 所以我認爲這將發生在這裏: 您保存一份報告,其中包含具有用戶發佈的發佈。由於發佈ID已分配NHibernate假定它必須更新並忽略它,但UserPublication ID是本地的,NHibernates知道應該何時插入並嘗試插入它,從而發生FK違規。 要解決此問題,您可以將版本屬性添加到發佈中,以便NHibernate可以根據其版本值插入版本屬性。

+0

是的,你說的是完全有道理的。不過,我用一種不同的方式來修復而不是使用版本。如下所示。 – empo 2009-09-17 15:36:30

+0

感謝您的評論btw。 – empo 2009-09-17 15:37:07

+0

我不知道這個解決方案是否會工作,如果你想更新出版物,因爲在這種情況下,NHibernate將所有給定的ID作爲未保存,並且我不知道如果出版物存在會發生什麼 – Beatles1692 2009-09-17 21:11:00

0

Publication類中的UserPublications包包含錯誤的關鍵元素。它應該是:

<key column="PublicationID"/> 
+0

斑點:-) – empo 2009-09-17 15:34:34