开发者

mapping on one of two columns / many-to-many mapping with fluentnhibernate

I'm trying to map the following:

A user can be connected to another user. The user class has a 'Connections' property which a collection of UserConnection. A UserConnection has t开发者_StackOverflow社区wo references to User, one for the requester (he who initiated the connection) and one for the requestee (he who is the target of the connection).

Using FluentNHibernate I want to load all UserConnections for when loading the User. A User's connections are those where the user's id matches either the requesterId or the requesteeId.

public class User
{
    //....
    public virtual IList<UserConnection> Connections { get; set; }
}

public class UserConnection
{
    //....
    public virtual User Requester { get; set; }
    public virtual User Requestee { get; set; }
}

This feels like a many-to-many relationship, but I can't quite wrap my head around it. Can anyone help?

EDIT:

My current User mapping (below) obviously only matches on one column...

HasMany(x => x.Connections).Inverse().Key(k => k.Columns.Add("Requestee_id")).Fetch.Select().Not.LazyLoad();

EDIT 2:

I have considered this approach but I would prefer to avoid that if possible...


Isn't it 1-many? user has a collection of Connections? But each connection has 1 RequesterUser or 1 RequesteeUser?

This would be a self referencing relation, mapping something like below.

    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("Users");
            DynamicUpdate();            
            References<User>(x => x.Requester)
                .Cascade.None()
                .NotFound.Ignore();
            References<User>(x => x.Requestee)
                .Cascade.None()
                .NotFound.Ignore();
            HasMany<User>(x => x.RequesterConnections)
                .Cascade.AllDeleteOrphan()
                .Inverse()
                .KeyColumn("RequesterId");
            HasMany<User>(x => x.RequesteeConnections)
                .Cascade.AllDeleteOrphan()
                .Inverse()
                .KeyColumn("RequesteeId");
        }
    }

I could be totally wrong, not been using NH long but maybe it will spark some ideas. I think this is how i would try and do it.

Obviously you don't have a single collection of Connections but you could get them both pretty easily.

Hope this helps. Kohan

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜