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: Self-Referential Composite Collection ?
PostPosted: Fri Nov 23, 2007 1:14 am 
Newbie

Joined: Thu Nov 22, 2007 11:20 pm
Posts: 1
Location: Toronto
I'm looking for help to create a social graph, for a social network. I've looked through NHibernate in action, even stepped through the source, read lots of forum posts but I'm not sure how to make this happen.


I have an object called Member, with certain properties.

I want a collection called Friends, of Members.

This was what I had used...

from Model.Member.cs
Code:
private IList<Member> _friends=new List<Member>();
      [Bag(1, Table="Friends", Cascade = CascadeStyle.AllDeleteOrphan,Inverse=true)]
      [Key(2, Column = "member_id")]
      [ManyToMany(3,ClassType=typeof(Member),Column="friend_id")]
      public virtual IList<Member> Friends {
        get { return _friends; }
        set { _friends = value; }
      }


but this code...

Code:
       aMember.Friends.Add(otherMember);

// CHECKED IN DEBUGGER, aMember.Friends.Count=1
        session.Update(toMember);
        session.Flush();


The SQL generated fine, making a two column join table from Member to Member, but NHibernate would not allow me to add to the collection.

A debugger showed that aMember.Friends[0] indeed contained otherMember, and stepping through nHibernate itself, the PreFlush cycle noted that the originalMember.Friends collection was dirty. However it saw only an empty set when it came time to generate an insertion statement it came up with an empty set and didn't insert a thing. Which was maddenning.


In truth what I really want is a self-refferential many to many with a few attributes added, like the date the relationship was formed. I've seen self-referential one to many described:


http://lykke.wordpress.com/2006/08/04/s ... hibernate/

In it I want a List or Bag of "Friends", which I'd like to be castable to Member but also contain some extra properties.

I though perhaps I could have Friend inherit from Member as a joined sub-class with a one to many. However Nhibernate complained about duplicate references on schema generation. Also I could figure out how to "add" an existing member. New friends could be created only with new member objects. How would I even form such a constructor.

Apologies if this is a basic thing. I would not have posted if I hadn't put in considerable effort trying to solve it myself.

nick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 4:00 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
You cannot do this with inheritance, because you can't change the type of an existing record--a 'Member' can't become a 'Friend'. But more accurately, you aren't modeling 'Friends' per se, but 'Friendships' between members.

Not sure how this is done with attributes, but here is some mapping XML for the appropriate bag. I'll let you work backwards to the code and properties you need:

Code:
<class name="Member"... >
   ...
   <list name="Friends" table="friends" lazy="true">
      <key column="friend_id" />
      <index column="I" />
      <composite-element class="Friendship">
         <property name="DateEstablished"/>   
         <many-to-one name="With" class="Member"/> <!-- class attribute is optional -->
      </composite-element>
   </list>
   ...
</class>


See http://www.hibernate.org/hib_docs/nhibe ... ollections


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.