NHibernate CreateSqlQuery和对象图

NHibernate CreateSqlQuery和对象图,nhibernate,Nhibernate,你好,我是NHibernate的新手。我想使用三个表的连接对数据库进行一次sql查询 我有一个应用程序,它有许多角色和许多用户。我试图让NHibernate从应用程序对象开始正确地形成对象图。例如,如果我有10条应用程序记录,我需要10个应用程序对象,然后这些对象有它们的角色,这些角色有它们的用户。然而,我得到的类似于笛卡尔积,在笛卡尔积中,我拥有的应用程序对象和总用户记录一样多 我已经对此进行了相当多的研究,不确定是否有可能正确地形成应用程序层次结构。我只能让被压扁的物体回来。这似乎是“可能”

你好,我是NHibernate的新手。我想使用三个表的连接对数据库进行一次sql查询

我有一个应用程序,它有许多角色和许多用户。我试图让NHibernate从应用程序对象开始正确地形成对象图。例如,如果我有10条应用程序记录,我需要10个应用程序对象,然后这些对象有它们的角色,这些角色有它们的用户。然而,我得到的类似于笛卡尔积,在笛卡尔积中,我拥有的应用程序对象和总用户记录一样多

我已经对此进行了相当多的研究,不确定是否有可能正确地形成应用程序层次结构。我只能让被压扁的物体回来。这似乎是“可能”的,因为在我的研究中,我读到了即将发布的LINQ到NHibernate版本中的“分组连接”和“分层输出”。尽管我还是个新手

[根据Fran在Ayende的帖子中的评论更新,我猜我想做什么是不可能的]

提前谢谢你的时间

Session.CreateSQLQuery(@"SELECT a.ID,
                a.InternalName,
                r.ID,
                r.ApplicationID,
                r.Name,
                u.UserID,
                u.RoleID
              FROM dbo.[Application] a  JOIN dbo.[Roles] r ON a.ID = r.ApplicationID
                 JOIN dbo.[UserRoleXRef] u ON u.RoleID = r.ID")
                .AddEntity("app", typeof(RightsBasedSecurityApplication))
                .AddJoin("role", "app.Roles")
                .AddJoin("user", "role.RightsUsers")
                .List<RightsBasedSecurityApplication>().AsQueryable();
Session.CreateSQLQuery(@“选择a.ID,
a、 我的名字,
r、 身份证,
r、 应用程序ID,
r、 名字,
u、 用户ID,
u、 罗莱德
从dbo。[Application]a加入dbo。[Roles]r ON a.ID=r.ApplicationID
加入dbo。[UserRoleXRef]u ON u.RoleID=r.ID”)
.附录(“应用程序”,类型(右侧基础安全应用程序))
.AddJoin(“角色”、“应用程序角色”)
.AddJoin(“用户”、“角色.权限用户”)
.List().AsQueryable();

我刚刚发现了批处理。这应该足够好了,尽管使用联接会更好

return Session
            .CreateCriteria<RightsBasedSecurityApplication>()
            .SetFetchMode("Roles", FetchMode.Select)
            .List<RightsBasedSecurityApplication>().AsQueryable();

public class RightsBasedSecurityApplicationMapping: ClassMap<RightsBasedSecurityApplication>
{
    public RightsBasedSecurityApplicationMapping()
    {
        Table("Application");
        Id(x => x.ID, "ID");//.Column("ID");
        Map(x => x.InternalName);
        HasMany(x => x.Roles).BatchSize(10);

public class RoleMapping : ClassMap<Role>
{
    public RoleMapping()
    {
        Table("Roles");
        Id(x => x.ID, "ID");
        References(x => x.Application, "ApplicationID").Not.LazyLoad();
        Map(x => x.Name);
        HasMany(x => x.RightsUsers).Table("UserRoleXRef").BatchSize(100);
返回会话
.CreateCriteria()
.SetFetchMode(“角色”,FetchMode.Select)
.List().AsQueryable();
公共类权限BasedSecurity应用程序映射:ClassMap
{
公共权利基础安全应用程序映射()
{
表(“申请”);
Id(x=>x.Id,“Id”);//列(“Id”);
Map(x=>x.InternalName);
HasMany(x=>x.Roles);
公共类角色映射:类映射
{
公共角色映射()
{
表(“角色”);
Id(x=>x.Id,“Id”);
引用(x=>x.Application,“ApplicationID”).Not.LazyLoad();
Map(x=>x.Name);
HasMany(x=>x.RightsUsers).Table(“UserRoleXRef”).BatchSize(100);