Nhibernate多层次的层次结构保存错误?
问题描述:
我有一个数据库与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
答
好吧,我发现了错误。
我需要命名映射中的列一对多和多对一的东西以外的Id以使其正确工作。
正确映射会是这个样子:
<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
谢谢你的男人,你 让我的日子,我开始了同样的错误,放弃我的头.. – Agasani 2011-07-14 00:41:13