-->
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.  [ 7 posts ] 
Author Message
 Post subject: Any advice designing self-referencing class?
PostPosted: Fri May 26, 2006 10:03 am 
Beginner
Beginner

Joined: Mon May 22, 2006 12:12 am
Posts: 23
Hi all,


Any idea on designing self-referencing class? i.e. class pointing to itself.

e.g. A (big) District, is made up of a number of (smaller) Districts.

There will be a single table named, District (right to do it this way?)

so, it is one-to-many parent-child relationship.

How should I design in NHibernate the hbm.xml file?

best wishes,
jL


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 10:29 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
Why do You think that the self reference should be different from reference to another class? Have You got any issues? As far as I remember I didn't got any...

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 12:02 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
a single table is correct. but the ParentDistrictID has to be nullable:

Code:
T_District
DistrictID       [ Identity ]
DistrictName     [ varchar(50) ]
ParentDistrictID [ Nullable ]


Your class might look like:

Code:
public class District() {
   private int id;
   private string name;
   private District parentDistrict;
   privte IList childrenDistricts;

   ... properties ...
}


Class mapping:

Code:
<class name=District lazy=true>
   <id name="id" ... >
       generator class="identity" />
   </id>

   <property name="Name" column="DistrictName" type=string not-null=true />

   <many-to-one name="ParentDistrict" class="district" column="ParentDistrictID" />

</class>


i definitely recommend marking the association as lazy otherwise you might load an entire object graph that you don't want.

then in your DAO you can get all children like so:

Code:
return session.CreateCriteria(typeofDistrict))
   .Add(Expression.Eq("ParentDistrict", parentDistrict))
   .AddOrder(Order.Asc("Name"))
   .List();


wheee! :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 2:04 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
actually, i was thinking about this more and i'm wondering if anyone has successfully implemented the Composite pattern in NH? i't might apply here...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 4:39 am 
Beginner
Beginner

Joined: Wed May 03, 2006 5:10 am
Posts: 32
Location: Monopoli - Italy
I implemented one month ago a "SelfJoin" class in my solution as devonl did.
It works without problems :)


Antonella


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 12:24 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
found this in the Hibernate docs. easily ported to NH/.NET:

http://www.hibernate.org/86.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 4:05 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I was able to implement an Item object which had Items as children. So far, it's just a unidirectional relationship, but, it seems to work OK. i.e. I didn't have to do anything special. It just works the same way it does if I was mapping to another class.


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