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