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>
出於某種原因,當我嘗試保存類別(與子類,容器等附後),我收到了外鍵違反數據庫。
代碼是這樣的(Pseudo)。
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
謝謝你的男人,你 讓我的日子,我開始了同樣的錯誤,放棄我的頭.. – Agasani 2011-07-14 00:41:13