Nhibernate-为什么在插入子行时尝试插入现有的父行

Nhibernate-为什么在插入子行时尝试插入现有的父行,nhibernate,parent,Nhibernate,Parent,我不明白为什么NHibernate在插入子行时尝试插入父对象(当该行已经存在于数据库中) 父映射: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> <class name="ReportDistribution.Client.ReportMgr.Model.ClientReport, ReportDistribution.Client.ReportMgr.Model"

我不明白为什么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>
父类报表的属性是子类的集合。 子类发布具有作为父对象的属性


提前感谢……

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

这样想吧——如果一个实体没有经过您当前使用的ISession的确切实例,那么它就不知道它存在。因此,它对待所有从未见过的事物都像对待新事物一样


一种选择是使用ISession.Loadentity;在保存之前重新加载父项。

您是否尝试在ClientPublications包中放置inverse=true?我收到一个ADO异常,因为它在PublicationId列中插入NULL。很抱歉,忽略上一条注释是因为我将id生成器更改为本机。inverse=true不会。查看SQL分析器,Nihibernate甚至没有尝试插入。