NHibernate未将父项插入数据库

NHibernate未将父项插入数据库,nhibernate,parent,Nhibernate,Parent,保存新报告时,NHibernate会插入报告,忽略发布并尝试插入UserPublication。然而,SQL随后抱怨违反了FK约束。 就好像NHibernate不认为发布是新的,即使db中不存在行 将实体关系视为: 一个报表可以有许多出版物属于一个报表的出版物 一个发布可以有多个UserPublications UserPublications属于一个发布 知道我做错了什么吗? 提前谢谢 以下是映射: <hibernate-mapping xmlns="urn:nhibernate-map

保存新报告时,NHibernate会插入报告,忽略发布并尝试插入UserPublication。然而,SQL随后抱怨违反了FK约束。 就好像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>

我认为问题在于出版物的id是分配的id,因此NHibernate无法识别何时应该插入出版物。 刷新会话时,它首先插入所有插入的对象,然后更新所有更新的对象,然后删除所有删除的对象。 所以我认为这会发生在这里: 您保存的报表包含具有userpublications的发布。由于分配了发布id,NHibernate假定必须更新并忽略它,但UserPublication id是本机的,NHibernates知道何时应该插入它并尝试插入它,因此发生FK冲突。
要解决此问题,可以向publication添加一个version属性,以便NHibernate可以根据其版本值插入该属性。

publication类中的UserPublications包具有错误的键元素。应该是:

<key column="PublicationID"/>

这很有效。我将“未保存的值”属性设置为“任何”

我认为不会有任何再流通

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

是的,你说的完全有道理。然而,我使用了一种不同的方法来修复,而不是使用版本。如下图所示。如果要更新发布,我不确定此解决方案是否有效,因为在这种情况下,NHibernate将所有给定的ID都视为未保存,我不知道如果发布存在会发生什么