我有一个6级层次结构的数据库和一个域模型。 像这样的东西:
类别 -SubCategory -容器 -DataDescription |元数据 -data
我正在使用的映射遵循以下模式:
<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
答案 0 :(得分:0)
我发现了错误。
我需要在映射中为一对多和多对一的名称命名列,以使其正常工作。
正确的映射看起来像这样:
<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