0

我不明白为什么 NHibernate 试图插入父对象 - 当行已经​​存在于数据库中时 - 当我插入子行时。

父映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="ReportDistribution.Client.ReportMgr.Model.ClientReport, ReportDistribution.Client.ReportMgr.Model" 
     table="ClientReport" 
     lazy="false"
     dynamic-update="true">
<id name="Id" access="property" column="ReportID">
  <generator class="assigned"></generator>
</id>
<property name="MaxAge" access="property" />
<property name="DeleteUnread" access="property" />
<property name="Description" access="property" />
<property name="Name" access="property" />
<bag name="ClientPublications" cascade="all" lazy="false">
  <key column="ReportID" />
  <one-to-many class="ReportDistribution.Client.ReportMgr.Model.ClientPublication, ReportDistribution.Client.ReportMgr.Model" />        
</bag>
</class>  
</hibernate-mapping>

子映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="ReportDistribution.Client.ReportMgr.Model.ClientPublication, ReportDistribution.Client.ReportMgr.Model" 
   table="ClientPublication" 
   lazy="false"
   dynamic-update="true">
<id name="Id" access="property" column="PublicationID">
  <generator class="assigned"></generator>
</id>  
<property name="CreatedOn" access="property" type="DateTime"></property>
<property name="IsMarkedForDeletion" access="property"></property>
<property name="IsDeleted" access="property"></property>
<property name="HasBeenRead" access="property"></property>
<property name="ReceivedOn" access="property" type="DateTime"></property>
<property name="FileExtension" access="property"></property>  
<property name="IsDownloaded" access="property"></property>
<property name="MustRead" access="property"></property>
<many-to-one    
  name="Report"
  class="ReportDistribution.Client.ReportMgr.Model.ClientReport, ReportDistribution.Client.ReportMgr.Model"
  lazy="false"
  column="ReportID">
</many-to-one>
</class>
</hibernate-mapping>

父类(报告)具有作为子类集合的属性。Child 类(Publication)具有作为父对象的属性。

提前致谢....

4

1 回答 1

0

在我看来,当您保存孩子时,父对象不再连接到会话。HNibernate 跟踪连接到会话的实体的状态,但如果实体分离,它将失去跟踪状态的能力。

可以这样想——如果一个实体没有通过您当前使用的 ISession 的确切实例,那么它不知道它存在。因此,它将从未见过的所有事物都视为“新事物”。

一种选择是使用 ISession.Load(entity); 在保存之前重新加载您的父母。

于 2009-08-28T13:26:53.657 回答