2009-09-15 70 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