0

当我保存新报告时,NHibernate 插入报告,忽略发布并尝试插入 UserPublication。然而,SQL 然后抱怨违反 FK 约束。就像 NHibernate 不认为出版物是新的,即使该行在数据库中不存在。

将实体关系想象为:一个报告可以有很多出版物(出版物属于一个报告)一个出版物可以有很多用户出版物(用户出版物属于一个出版物)

任何想法我做错了什么?提前致谢。

这是映射:

<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>

4

3 回答 3

0

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

于 2009-09-16T08:40:42.687 回答
0

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

<key column="PublicationID"/>
于 2009-09-16T08:59:33.723 回答
0

这行得通。我将未保存的值属性设置为“任何”。

我认为不会有任何反响。

<id name="Id" access="property" column="PublicationID" unsaved-value="any">    
  <generator class="assigned"></generator>
</id>
于 2009-09-17T15:39:52.163 回答