Help with a particular mapping scenario

10 views
Skip to first unread message

sed

unread,
Nov 16, 2010, 12:29:38 PM11/16/10
to Fluent NHibernate
I am having problem find the best way to map a particular
relationship. We have a feature where people can follow other people.
Typically we would have a table People, like this

People-
PersonID
Name
Etc

A linking table is typically used that contains the Person being
followed ID and followerID.

We need to be able to be able to perform queries that would allow a
person to see who they follow, filter content by people who they
follow and view the users who follow them.

Has anyone else faced this kind of problem before?

Paul Batum

unread,
Nov 19, 2010, 8:56:04 AM11/19/10
to fluent-nhibernate
People usually refer to this as a self referencing many-to-many. I wrote a little sample of this earlier in the year. Perhaps it will help:

Here's the most important bit:

public class CustomerMap : ClassMap<Customer>
    {
        public CustomerMap()
        {
            Id(x => x.CustomerId);
            Map(x => x.Birthday);
            Map(x => x.FirstName);

            HasManyToMany(x => x.Parents)
                .ParentKeyColumn("ChildID")
                .ChildKeyColumn("ParentID")
                .Inverse();
            
            HasManyToMany(x => x.Children)
                .ParentKeyColumn("ParentID")
                .ChildKeyColumn("ChildID");
        }
    }


--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.


Nick Webb

unread,
Nov 19, 2010, 8:56:55 AM11/19/10
to fluent-n...@googlegroups.com
I am a little confused by your schema - do you have a single People object, each object representing a person, that links back to itself?  Or do you have a People object that links to a Person object?

I would suggest:

Person
 - Id
 - Name
 - [stuff!]
 - Followers (IList<Person>)

With Followers being a ManyToMany.  Then you could simply query against the property like anything else using a join:

"select p from Person p join p.Followers as Person f where p.Id = :personId and f.Name = :whatever"

Nick Webb

unread,
Nov 19, 2010, 8:57:10 AM11/19/10
to fluent-n...@googlegroups.com
Beat me to it by seconds!

sed

unread,
Nov 23, 2010, 12:10:01 PM11/23/10
to Fluent NHibernate
Thanks, I have mapped this out and it seems good but querying is
really difficult.

I have any kind of random content we'll just called Content that
HasOne<Person>. I need to query Content and filter it to the Followees
list. Is that possible?

On Nov 19, 8:57 am, Nick Webb <nwe...@gmail.com> wrote:
> Beat me to it by seconds!
>
>
>
>
>
>
>
> On Fri, Nov 19, 2010 at 8:56 AM, Paul Batum <paul.ba...@gmail.com> wrote:
> > People usually refer to this as a self referencing many-to-many. I wrote a
> > little sample of this earlier in the year. Perhaps it will help:
>
> >https://github.com/paulbatum/Fluent-NH-Test-Bed/tree/self-referencing...
>
> > <https://github.com/paulbatum/Fluent-NH-Test-Bed/tree/self-referencing...>Here's
> >> fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr i...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/fluent-nhibernate?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Fluent NHibernate" group.
> > To post to this group, send email to fluent-n...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr i...@googlegroups.com>
> > .

Nick Webb

unread,
Nov 23, 2010, 6:07:01 PM11/23/10
to fluent-n...@googlegroups.com
Just another join like any join.  Read up here.

Basically, your hql would be along the lines of:
"from Content content join Followees as person where person.Name [... your where clause here]"

To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages