2009-08-28 2 views
0

자식 행을 삽입 할 때 NHibernate가 부모 개체를 삽입하려고하는 이유 (행이 이미 db에있을 때)를 이해하지 못합니다.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 클래스 (발행물)에는 부모 개체 인 속성이 있습니다. 당신이 아이를 저장할 때 부모 개체가 더 이상 세션에 연결되어있는 것처럼 사전에

덕분에 ....

+0

ClientPublications 가방에 inverse = true를 넣으려고 했습니까? – Rashack

+0

게시 키 열에 NULL을 삽입했기 때문에 ADOException이 발생합니다. – empo

+0

유감스럽게도 마지막 발언은 id generator를 "native"로 변경했기 때문에 무시되었습니다. – empo

답변

0

그것은 나에게 소리를하지 않습니다. Hibernate는 세션에 연결된 엔티티의 상태를 추적하지만 엔티티가 분리되면 상태를 추적하는 기능이 손실됩니다.

이렇게 생각하십시오. 현재 사용중인 ISession의 정확한 인스턴스를 통해 엔터티가 전달되지 않은 경우 엔터티가 존재하는지 여부를 모릅니다. 그러므로, 그것은 그것이 마치 "새로운"것처럼 보이지 않는 모든 것을 다룹니다.

하나의 옵션은 ISession.Load (엔터티)를 사용할 수 있습니다. 저장하기 전에 부모를 다시 불러올 수 있습니다.

관련 문제