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.  [ 2 posts ] 
Author Message
 Post subject: In memory object consistency when deleting
PostPosted: Thu Feb 05, 2009 9:24 pm 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
I'm quite newbie on NHibernate, so sorry if i'm not following right guide for asking.

My question is fairly simple, i've been trying to do something for a long while and now i'm starting to wonder whether is possible or not.

Let's assume we got the usual:

Code:

      public class Parent
      {
             public Parent(){Children = new List<Child>()}
             public int ID{get;set;}
             public IList<Child> Children{get;set;}
      }



and

Code:

      public class Child
      {
             public Child(){}
             public int Id{get;set;}
             public Parent Parent{get;set;}
      }



Let's assume following tables:

Code:
{Parents [int ID]}
{Children [int parentID (FK Parents.ID)] [int ID]}


My question is whether i should take care of having consistency between in memory objects and persisted entities.

Code:

     Child childToBeDeleted = ....
     Parent parent = ....
     parent.Children.Add(childToBeDeleted);
     parent.Children.Add(new Child());
     parent.Children.Add(new Child());
     parent.Children.Add(new Child());
     parent.Children.Add(new Child());
     //I've checked here parent.Children.Count == 5
     session.Save(parent)
     //It works until previous line
     session.Delete(childToBeDeleted);
     //What i would like to have here is (parent.Children.Count == 4) but it's 5 instead
     session.Delete(parent)
     //previous line is firing an exception
     


Or i should take care about consistency:


Code:

     Child childToBeDeleted = ....
     Parent parent = ....
     parent.Children.Add(childToBeDeleted);
     parent.Children.Add(new Child());
     parent.Children.Add(new Child());
     parent.Children.Add(new Child());
     parent.Children.Add(new Child());
     //I've checked here parent.Children.Count == 5
     session.Save(parent)
     //Here i've made it works
     session.Delete(childToBeDeleted);
//////This line is added to keep consistency
     parent.Children.Remove(childToBeDeleted);
//////
     session.Delete(parent);


I'm really stuck in this point, how shoudl i map this to avoid removing in code (second block). I will invite a beer to anyone who provided me a nice answer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 1:23 pm 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
I simple "yes you have to take care about consistency" or "no, mapping will resolve that for you" will be enough


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.