I have a parent class (Item) and child a class (Attribute) with one-to-many relationship. When I try to save Item that has a number of child Attributes, I get the following error:
Quote:
Cannot insert the value NULL into column 'item_id', table 'ST_Attribute'; column does not allow nulls. INSERT fails. The statement has been terminated.
I know that if I save Item first and then save its Attributes it will work, as we'll have a value for Item's Id. But I can't believe there isn't a way in NHibernate to handle it. I'm pretty sure I'm missing something in my mapping files, but can't figure out what. Any help would be greatly appreciated.
Following are the mapping files:
1. Item
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DomainObjects" assembly="DomainObjects">
<class name="Item" table="ST_Item">
<id name="Id" column="id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="Name" column="name"/>
<bag name="Attributes" cascade="all-delete-orphan" inverse="true">
<key column="item_id"/>
<one-to-many class="Attribute"/>
</bag>
</class>
</hibernate-mapping>
2. Attribute
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DomainObjects" assembly="DomainObjects">
<class name="Attribute" table="ST_Attribute">
<id name="Id" column="id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="Name" column="name"/>
<property name="Value" column="value"/>
<many-to-one name="Item" class="Item" column="item_id" cascade="none"/>
<many-to-one name="AttributeType" class="AttributeType" column="attribute_type_id"/>
</class>
</hibernate-mapping>