-->
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.  [ 9 posts ] 
Author Message
 Post subject: NHibernate 1.2 - How to wire up entities re: associations
PostPosted: Wed Feb 07, 2007 1:12 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
I read this question here on code project, and note that it doesn't have an answer. I have the same question

http://www.codeproject.com/aspnet/NHibe ... x1810733xx
(question titled Upgrade to NHibernate 1.2.0 - Add/Remove Scaffolding)


When using NHibernate.Generics, it provided a useful method for wiring up entity relationships when adding a child object to a collection on the parent. Now that it is no longer really necessary to use NHibernate.Generics with 1.2 supporting .NET generics, is there a good way to implement this same functionality? What is the best way to wire up these types of relationships so that adds/removes work properly??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 2:27 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
NHibernate.Generics is deprecated not only because 1.2 has native generics support, but because I found that the auto wiring led to much confusion down the road.
I suggest that you will use the recommended way of adding:
AddXYZ() to handle this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:33 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
Ok, I thought I had this sorted, but it appears not, and I thought it was simple, so I'm finding it a bit frustrating wiring up these entities.

I've implemented the approach discussed by jnapier in this thread here:
http://forum.hibernate.org/viewtopic.ph ... c&start=15
to use a custom list implementation with events on the Add and Remove methods to ensure that parent relationships are set up when the child is added to the collection. I'm still not entirely clear on how to manage the relationship when the child is added to the parent.

i.e. the scenario:

Code:
parent.Children.Add(child)


works fine, because the event that gets triggered on the add has the code for child.Parent = this;
However, when I come from the other direction, if I do:

Code:
child.Parent = p;


and then in the setter for child.Parent do something along the lines of:

Code:
set
{
   this.Parent = value;
   if(!value.Children.Contains(this))
   {
      value.Children.Add(this);
   }
}


I get a LazyInitializationException : cannot access loading collection.

What's the (or a) right way to manage a bidirectional parent/child relationship that will be used from both directions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 6:49 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
Anyone? Is this just waaay too obvious and I'm barking up the wrong tree?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 6:56 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You should avoid putting logic in the property accessors that NHibernate uses because you can't really predict what will be the object state when NHibernate calls them. The best way would be to have a separate method, Parent.AddChild(Child) that wires both instances together appropriately.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 7:09 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
Thanks for the reply, however I don't think I have a problem with the parent.AddChild(child). My problem is when I call child.Parent = p (or similar).

When I try to set the parent on the child, I have to add the child to the parent's children collection, and it is at this stage that I am getting the LazyInitializationException.

I was under the impression that I had to manage the association from both ends - i.e. when wanting to add a child to the parent I have to do:

this.children.add(c);
c.parent = this;

but also the other way around:

p.children.add(this);
this.parent = p;

The first is handled ok currently, the second is causing issues. Should I not be using lazy="true"? should I not be using a bag?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 7:18 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You should avoid putting logic in the property accessors that NHibernate uses because you can't really predict what state the object will be in when NHibernate calls them.

Or use access="field.somenamingstrategy".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 8:42 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
Thanks for your help - sorry it took me a while to understand :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:14 am 
Newbie

Joined: Mon Jul 18, 2005 7:45 am
Posts: 16
Quote:
You should avoid putting logic in the property accessors that NHibernate uses because you can't really predict what will be the object state when NHibernate calls them. The best way would be to have a separate method, Parent.AddChild(Child) that wires both instances together appropriately.


The problem we've found with this approach is that collections mapped as IList exposes Add/Remove methods which don't maintain bi-directionality. For that reason, we've created a IReadOnlyCollection interface:

Code:
    public interface IReadOnlyCollection : IEnumerable
    {
        #region Properties

        /// <summary>
        /// Indexer that gets and item by index.
        /// </summary>
        object this[int index]
        {
            get;
        }

        /// <summary>
        /// Gets the number of items in the collection
        /// </summary>
        int Count
        {
            get;
        }

        #endregion
    }



and a base class implementation.

Then we are able to derived custom collection classes with custom add/remove methods as required. It's more work, but results in a more elegant and intuitive design.


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