2009-07-29 7 views
0

私は6レベルの階層とその上にドメインモデルを持つデータベースを持っています。このような 何か:Nhibernateマルチレベル階層の保存エラー?

カテゴリー -SubCategory - コンテイナー -DataDescription |私は、カテゴリを保存しようとすると、いくつかの理由

<class name="Category, Sample" table="Categories"> 
    <id name="Id" column="Id" type="System.Int32" unsaved-value="0"> 
     <generator class="native"/> 
    </id>  
    <property name="Name" access="property" type="String" column="Name"/> 
    <property name="Metadata" access="property" type="String" column="Metadata"/> 
    <bag name="SubCategories" 
     cascade="save-update" 
     lazy="true" 
     inverse="true"> 
     <key column="Id" foreign-key="category_subCategory_fk"/> 
     <one-to-many class="SubCategory, Sample" /> 
    </bag> 
    </class> 

<class name="SubCategory, Sample" table="SubCategories"> 
    <id name="Id" column="Id" type="System.Int32" unsaved-value="0"> 
     <generator class="native"/> 
    </id> 
    <many-to-one name="Category" 
       class="Category, Sample" 
       foreign-key="subCat_category_fk"/> 

    <property name="Name" access="property" type="String"/> 
    <property name="Metadata" access="property" type="String"/> 

    <bag name="Containers" 
     inverse="true" 
     cascade="save-update" 
     lazy="true"> 
     <key column="Id" foreign-key="subCat_container_fk" /> 
     <one-to-many class="Container, Sample" /> 
    </bag> 
</class> 

<class name="Container, Sample" table="Containers"> 
    <id name="Id" column="Id" type="System.Int32" unsaved-value="0"> 
     <generator class="assigned"/> 
    </id> 
    <many-to-one name="SubCategory" 
       class="SubCategory,Sample"     
       foreign-key="container_subCat_fk"/>  

    <property name="Name" access="property" type="String" column="Name"/> 

    <bag name="DataDescription" cascade="all" lazy="true" inverse="true"> 
     <key column="Id" foreign-key="container_ DataDescription_fk"/> 
     <one-to-many class="DataDescription, Sample" /> 
    </bag> 

    <bag name="MetaData" cascade="all" lazy="true" inverse="true"> 
     <key column="Id" foreign-key="container_metadata_cat_fk"/> 
     <one-to-many class="MetaData, Sample" /> 
    </bag> 
</class> 

(サブカテゴリで、コンテナなど添付)私が手: - データ

私が使用しているマッピングは、次のパターンに従いますが、メタデータデータベースからの外部キー違反。

コードはこのようなものです(疑似)。ここで

var category = new Category(); 
var subCategory = new SubCategory(); 
var container = new Container(); 
var dataDescription = new DataDescription(); 
var metaData = new MetaData(); 

category.AddSubCategory(subCategory); 
subCategory.AddContainer(container); 
container.AddDataDescription(dataDescription); 
container.AddMetaData(metaData); 

Session.Save(category); 

は、この試験からのログです:

DEBUG NHibernate.SQL - INSERT INTO Categories (Name, Metadata) VALUES (@p0, @p1); select SCOPE_IDENTITY(); @p0 = 'Unit test', @p1 = 'unit test' 

DEBUG NHibernate.SQL - INSERT INTO SubCategories (Category, Name, Metadata) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY(); @p0 = '1', @p1 = 'Unit test', @p2 = 'unit test' 

DEBUG NHibernate.SQL - INSERT INTO Containers (SubCategory, Name, Frequency, Scale, Measurement, Currency, Metadata, Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7); @p0 = '1', @p1 = 'Unit test', @p2 = '15', @p3 = '1', @p4 = '1', @p5 = '1', @p6 = 'unit test', @p7 = '0' 

ERROR NHibernate.Util.ADOExceptionReporter - The INSERT statement conflicted with the FOREIGN KEY constraint "subCat_container_fk". The conflict occurred in database "Sample", table "dbo.SubCategories", column 'Id'. 

次のようにオブジェクトに項目を追加するための方法が常にある:私は

public void AddSubCategory(ISubCategory subCategory) 
{ 
    subCategory.Category = this; 
    SubCategories.Add(subCategory); 
} 

何をしないのです??

おかげで、 nisbus

+0

は、私は同じエラー上で、私の頭をスクラップし始めたのだ、あなたは私の一日を作った 、あなたに男をありがとう。.. – Agasani

答えて

0

は、まあ、私はエラーを検出しました。

これを正しく動作させるには、Id以外のマッピングの1対多と多対1の列に名前を付ける必要がありました。

正しいマッピングは次のようになります:

<class name="Category, Sample" table="Categories"> 
    <id name="Id" column="Id" type="System.Int32" unsaved-value="0"> 
     <generator class="native"/> 
    </id>  
    <property name="Name" access="property" type="String" column="Name"/> 
    <property name="Metadata" access="property" type="String" column="Metadata"/> 
    <bag name="SubCategories" 
     cascade="save-update" 
     lazy="true" 
     inverse="true"> 
     <key column="Category_Id" foreign-key="category_subCategory_fk"/> 
     <one-to-many class="SubCategory, Sample" /> 
    </bag> 
    </class> 

<class name="SubCategory, Sample" table="SubCategories"> 
     <id name="Id" column="Id" type="System.Int32" unsaved-value="0"> 
       <generator class="native"/> 
     </id> 
     <many-to-one name="Category" 
           class="Category, Sample" 
           column="Category_Id" 
           foreign-key="subCat_category_fk"/> 

     <property name="Name" access="property" type="String"/> 
     <property name="Metadata" access="property" type="String"/> 

     <bag name="Containers" 
       inverse="true" 
       cascade="save-update" 
       lazy="true"> 
       <key column="Container_Id" foreign-key="subCat_container_fk" /> 
       <one-to-many class="Container, Sample" /> 
     </bag> 
</class> 

すべてが今、素晴らしい作品。

おかげで、 nisbus

関連する問題