i've been playing with the NHibernate sources to support a mapping we need, and whilst testing it I found that adding children to a parent collection wasn't working. I scaled back to the simple case that should have worked without any mods, but that exhibited the same problem. After getting nowhere looking for the error, I plugged my home built library into an application that worked with the downloaded library, expecting to get the same problem, but it worked. I'm now confused, and suspect I've done something daft with my code or mapping, but I can't see what. Can anyone point me at the doubtless glaringly obvious mistake?
Here are the test tables, pretty simple:
Code:
Table Parent
ID int not null Identity, primary key
Name varchar(50) not null
Table Child
ID int not null Identity, primary key
Name varchar(50) not null
ParentID int not null, foreign key linked to ID in Parent
Here is the mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DBKeyTest" assembly="DBKeyTest">
<class name="DBKeyTest.IDParent, DBKeyTest" table="IDParent">
<id name="ID" column="ID" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Name" type="String" />
<bag name="ChildList" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="ParentID"/>
<one-to-many class="DBKeyTest.IDChild, DBKeyTest"/>
</set>
</class>
<class name="DBKeyTest.IDChild, DBKeyTest" table="IDChild">
<id name="ID" column="ID" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Name" type="String" />
<many-to-one name="Parent" column="ParentID" class="DBKeyTest.IDParent, DBKeyTest" not-null="true"/>
</class>
</hibernate-mapping>
and here's the sample code that's giving the error:
Code:
Private Sub SimpleChildTest(ByVal session As ISession)
Dim parent As IDParent
parent = session.Load(GetType(IDParent), 18)
parent.ChildList = New List(Of IDChild)
Dim child As New IDChild
child.Name = "Bart"
child.Parent = parent
parent.ChildList.Add(child)
child = New IDChild
child.Name = "Lisa"
child.Parent = parent
parent.ChildList.Add(child)
session.Update(parent)
session.Flush()
End Sub
[From IDParent]
Private mChildList As List(Of IDChild)
Public Overridable Property ChildList() As List(Of IDChild)
Get
Return mChildList
End Get
Set(ByVal Value As List(Of IDChild))
mChildList = Value
End Set
End Property
[From IDChild]
Private mParent As IDParent
Public Overridable Property Parent() As IDParent
Get
Return mParent
End Get
Set(ByVal Value As IDParent)
mParent = Value
End Set
End Property
Apart from the properties given, IDParent and IDChild just consist of simple constructors and simple scalar properties for the remain properties in the mappings.
When I run the code, I get the error
Quote:
You may not dereference an collection with cascade="all-delete-orphan"
If I remove the child.Parent=parent lines, I get the following error instead:
Quote:
not-null property references a null or transient value: DBKeyTest.IDChild.Parent
Presumably I've got a mistake somewhere, but so far examing it or comparing it to similar code in the working application hasn't shed any light.
Incidentally, looking at the examples from the documentation, the latter should work, but in my (working) apps I always have to set the parent reference explicitly - which way should it work?
Thanks,
Kev