Hibernate version: 1.2.1.4000
In a sample app I have a one-to-one relationship mapped using a foreign key association. Saves, inserts and updates work fine but when I set a child to null the foreign key is set to null but the child row remains. After looking through the NHibernate docs this behaviour is expected:
Quote:
Mapping an association (many-to-one, or collection) with cascade="all" marks the association as a parent/child style relationship where save/update/deletion of the parent results in save/update/deletion of the child(ren). Futhermore, a mere reference to a child from a persistent parent will result in save / update of the child. The metaphor is incomplete, however. A child which becomes unreferenced by its parent is not automatically deleted, except in the case of a <one-to-many> association mapped with cascade="all-delete-orphan"
I have added the one to one association and tried all the cascade options but I cannot get the unreferenced child to delete.
My db structure is:
Code:
CREATE TABLE OneToOneChild (
[ChildId] INT NOT NULL PRIMARY KEY CLUSTERED,
[Name] VARCHAR(255) NOT NULL,
)
CREATE TABLE OneToOneParent (
[ParentId] INT NOT NULL PRIMARY KEY CLUSTERED,
[Name] VARCHAR(255) NOT NULL,
[ChildId] INT NULL,
CONSTRAINT [FK_OneToOneParent_OneToOneChild] FOREIGN KEY (ChildId)
REFERENCES OneToOneChild (ChildId)
)
INSERT INTO OneToOneParent VALUES (1, 'Parent 1', 1)
INSERT INTO OneToOneParent VALUES (2, 'Parent 2', NULL)
INSERT INTO OneToOneChild VALUES (1, 'Child 1')
My class and mappings (using attributes) are:
Code:
[Class(Table = "OneToOneChild")]
public class OneToOneChild
{
private int childId = 0;
private string name;
private OneToOneParent oneToOneParent;
[Id(Name = "ChildId")]
[Column(Name = "ChildId", SqlType = "Int32", NotNull = true)]
[Generator(1, Class = "increment")]
public virtual int ChildId
{
get { return childId; }
set { childId = value; }
}
[Property(Column = "Name", NotNull = true)]
public virtual string Name
{
get { return name; }
set { name = value; }
}
[OneToOne(Name = "OneToOneParent", Class = "OneToOneParent", ClassType = typeof(OneToOneParent), PropertyRef = "OneToOneChild",
Cascade = CascadeStyle.AllDeleteOrphan)]
public virtual OneToOneParent OneToOneParent
{
get { return oneToOneParent; }
set { oneToOneParent = value; }
}
}
Code:
[Class(Table = "OneToOneParent")]
public class OneToOneParent
{
private int parentId = 0;
private string name;
private OneToOneChild oneToOneChild = null;
[Id(Name = "ParentId")]
[Column(Name = "ParentId", SqlType = "Int32", NotNull = true)]
[Generator(1, Class = "increment")]
public virtual int ParentId
{
get { return parentId; }
set { parentId = value; }
}
[Property(Column = "Name", NotNull = true)]
public virtual string Name
{
get { return name; }
set { name = value; }
}
[ManyToOne(Cascade = CascadeStyle.AllDeleteOrphan, Column = "ChildId", NotNull = false, Class = "OneToOneChild",
ClassType = typeof(OneToOneChild), Unique = true)]
public virtual OneToOneChild OneToOneChild
{
get { return oneToOneChild; }
set { oneToOneChild = value; }
}
}
I can load my OneToOneParent and the child is present, I then set the child to null e.g. onetoOneParent.OneToOneChild = null and the foreign key in the parent row is set to null but the child row remains.