开发者

"Indirect" ManyToMany Relation

Warning: I am brand new to NHibernate!

I have a User object. This User object has related Roles. These Roles have related Privileges. The goal is to get a list of Privileges for a User. Privileges are "indirectly" related to Users through Roles.

When doing a select on the user to get the list of related Privileges I might get back multiples of a Privilege. So I do a UNIQE select.

I am wondering if it is possible to have some 'magic' NHibernate mapping that will fill my Roles list AND my Privilege list. The easy part I solved myself (proud! ;-) ) is mapping correctly the Roles. I have no idea how to get the 'indirectly' related (unique) Privileges.

Any idea开发者_开发知识库s?


public class User
{
    public virtual int Id { get; set; }

    public virtual ICollection<Role> Roles { get; private set; }

    public virtual IEnumerable<Privilege> Privileges
    {
        get { return Roles.SelectMany(role => role.Priveleges).Distinct(); }
    }

    public User()
    {
        Roles = new List<CompanyRole>();
    }
}

and query like

session.QueryOver<User>()
    .Fetch(u => u.Roles).Eager
    .Fetch(u => u.Roles.Privileges).Eager
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜