After having changed around my mappings a bit ( see my other question about cascade-delete for reasons) , i tried inserting a completely new object and all its subclasses.
After this another problem emerged, an issue with the insertion of the keys into the database. Situation is as follows:
I have an object with 2 tiers of subclasses, both a collection.
lets call the object Parent, this one has a collection of childs, and every enitity in this collection has its own collection of enitities. Which are mapped as follows.
Parent's mapping of its <set>
<!--Parent-->
<set name="Collection"
table="Table"
cascade="all-delete-orphan"
batch-size="15"
inverse="true">
<key column="ParentID"/>
<one-to-many class="CollectionObject,CollectionObject-ns"/>
</set
CollectionObject's Mapping
<id name="ID" column="ID">
<generator class="native"/>
</id>
<!-- property mappings-->
<property name="ParentID" column="ParentID" not-null="true"/>
<!--collection mapping-->
<set name="Collection"
table="Table"
cascade="all-delete-orphan"
inverse="true"
batch-size="15">
<key column="ChildID"/>
<one-to-many class="CollectionObject,CollectionObject-ns"/>
</set>
The mapping mapping of the object in the 2nd collection is similar tot he above. The envisioned scenario would be, I save Parent, this would trigger the save of subclasses/collections.
e.g. I have Parent with ID = 1, Parent is filled with 1 collection, and this collection has 1 collection of its own.
So I save Parent, parent gets an ID from database (native sql identity). Now the first collection should get its ParentID property filled with the ID the Parent just got from the database. And the collection of the collection should get its ChildID filled with the ID the Child got from the database in the same way as Parent got his ID.
What happens now is (I checked created SQL in NHProf) Everything gets inserted, with its own ID, But the collections do not get their keycolumn filled with the ID of their parentclass. (instead it just gets 0 inserted)
So my question boils down to, what did i forget to add to my mapping which causes the to happen? Does the key column not do what i think it should do?
If i forget to add anything here, please say so. I will gladly give additional information.
update
I think the problem might have to do with the childs not having a <many-to-one>
tag. So i tried to add one of those to the first child mapping. I came up with this
<many-to-one name="ParentID"
class="Parent,Parent-ns"
column="ParentID"
not-null="true"/>
However, this setup gives me the following error.
Exception occurred getter of Parent.ParentID
with InnerException
{"Object does not match target type."}
Sadly, This error does not give me any ideas of where to continue.