Nhibernate multilevel hierarchy save error?

Posted by nisbus on Stack Overflow See other posts from Stack Overflow or by nisbus
Published on 2009-07-29T14:21:25Z Indexed on 2010/05/31 10:03 UTC
Read the original article Hit count: 286

Filed under:

Hi,

I have a database with a 6 level hierarchy and a domain model on top of that. something like this:

Category -SubCategory -Container -DataDescription | Meta data -Data

The mapping I'm using follows the following pattern:

  <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>

For some reason when I try to save the category (with the subcategory, container etc. attached) I get a foreign key violation from the database.

The code is something like this (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);

Here is the log from this test :

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'.

The methods for adding items to objects is always as follows:

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

What am I missing??

Thanks, nisbus

© Stack Overflow or respective owner

Related posts about nhibernate