I have had some trouble deleting the child in a one-to-many relationship in NHibernate. My schema described a simple parent-child relationship: a parent can have multiple children and every child has exactly one parent.
In the database schema I had two tables parentTable and childTable. ParentTable had an ID column and some other columns. The childTable had an ID primary key column, some other columns and a parentId NOT NULL column which had a foreign key constraint against the ID column in the parentTable.

The NHibernate schema describing the relationship was the following:
- Parent schema (the relevant part):
- Child schema (the relevant part):
I was trying to delete the child the following way:
ParentClass parent; ChildClass child; parent.children.Remove(child); session.Delete(child); session.Save(parent); session.Flush();
When executing the code I got an exception saying
Cannot insert the value NULL into column 'parentId', table 'childTable'; column does not allow nulls. UPDATE fails. The statement has been terminated.
It took me some googling to find out what I was doing wrong. The NHibernate schema was incomplete. The errors were:
- I did not specify that the parentId column in the childTable cannot be NULL. This can be done using the not-null=”true” attribute
- I did not specify that the one-to-many relation was inverse even though it was: the child knew its parent but the parent did not directly know its children in the database schema. (See this article on what this inverse attribute means in more of depth).
- It is obvious that no child can exist without a parent. NHibernate will take care of deleting the child once it is removed from the children collection of the parent if the on-delete=”cascade” is set on the key element within the children collection.
The NHibernate schema that solved my problem finally is the following:
- Parent schema (the relevant part):
- Child schema (the relevant part):
Hello,
I would like to take to this opportunity to ask a question about one-to-many. I ahve two classes:
GeneralInformation and famousPlacesInLondon
public class GeneralInformation
{
private int id;
public virtual int Id
{
get { return id; }
set { id = value; }
}
private IList famousPlacesOutLondon;
public virtual IList FamousPlacesOutLondon
{
get { return famousPlacesOutLondon; }
set { famousPlacesOutLondon = value; }
}
}
public class FamousPlacesInLondon
{
private int id;
public virtual int Id
{
get { return id; }
set { id = value; }
}
private string link;
public virtual string Link
{
get { return link; }
set { link = value; }
}
private string title;
public virtual string Title
{
get { return title; }
set { title = value; }
}
}
}
My mapping are as follows:
If I fill the GeneralInformation with data and do a save using NHibernate, everything gets inserted into the appropriate table, except the ForeignKey GeneralInformationId. What is wrong?
TIA
Yaz
Yazid
April 23rd, 2009
i Have mapping like this, and it’s doesn’t work…:/
I don’t know why.
in Parent
In child
Artur
September 12th, 2009
i Have mapping like this, and it’s doesn’t work…:/
I don’t know why.
app.config
===============
NHibernate.Dialect.MsSql2005Dialect
NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.SqlClientDriver
1
Server=art-PC;
Database=T1;
User ID = sa;
Password=password;
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
True
===============
in Parent
In child
Artur
September 12th, 2009
<!–
s
September 12th, 2009