-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Deleting unreferenced one to one object
PostPosted: Fri Feb 29, 2008 10:52 am 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 29, 2008 11:07 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I suppose you have to set both parent and child to null. Can you post the code you use to delete the child ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 29, 2008 1:21 pm 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
My original code was:

Code:
OneToOneParent parent =  (OneToOneParent)session.Get(typeof(OneToOneParent), 1);
parent.OneToOneChild = null;
session.BeginTransaction();
session.Save(parent);
session.Transaction.Commit();


After your comment about deleting the parent I tried:

Code:
OneToOneParent parent =  (OneToOneParent)session.Get(typeof(OneToOneParent), 1);
parent.OneToOneChild.OneToOneParent = null;
parent.OneToOneChild = null;
session.BeginTransaction();
session.Save(parent);
session.Transaction.Commit();


i.e. setting the parent of the child to null also but this still doesn't work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 29, 2008 1:45 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have to delete the child !

session.Delete(parent.OneToOneChild)

"all-delete-orphan" only come into play when you delete the parent !

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.