2009-08-28 19 views
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> 

親クラス(レポート)は、子クラスのコレクションである性質を持っています。 子クラス(パブリケーション)には、親オブジェクトであるプロパティがあります。事前に

おかげで....

+0

ClientPublicationsバッグにinverse = trueを入れてみましたか? – Rashack

+0

パブリケーションID列にNULLが挿入されているため、ADOExceptionが発生します。 – empo

+0

残念ですが、最後のコメントはid generatorを "native"に変更しているためです。 – empo

答えて

0

あなたは子供を保存しているとき、親オブジェクトは、もはやセッションに接続されているようですが、私には鳴りません。 HNibernateはセッションに接続されているエンティティの状態を追跡しますが、エンティティがデタッチされると状態を追跡する能力が失われます。

このように考えると、エンティティが現在使用しているISessionの正確なインスタンスを経由していない場合、エンティティが存在することがわかりません。したがって、それはまるで "新しい"かのように見られないすべてのものを扱います。

1つのオプションは、ISession.Load(エンティティ)を使用することです。保存する前に親をリロードする。

関連する問題