6

我有域类位置

    public abstract class BaseEntity<T> where T: struct 
    {
    public virtual T Id { get; set; }
    public virtual bool Equals(BaseEntity<T> other)
    }

    public class Location : BaseEntity<Int32>
    {

    public  User User {get;set;}
    }

    public class User : BaseEntity<Int32>
    {
    public string Name {get;set;
    }

    public OtherInfo Otherinfo {get;set;};
     }
    public class OtherInfo
    {
    public  string preference {get;set;};
    }

    var criteria = session.CreateCriteria(typeof(Location), "alias");
    criteria.CreateCriteria("User", "user", JoinType.InnerJoin);
    criteria.CreateCriteria("user.Otherinfo", "userInfo",JoinType.InnerJoin); 
    criteria.Add(Restrictions.Eq("user.Id", 100));
    criteria.SetProjection(Projections.Alias(Projections.Id(), "Id"),                            Projections.Alias(Projections.Property("user.Name"), "Name"),  Projections.Alias(Projections.Property("userInfo.preference "), "pref"));

现在当我执行上述标准时,它会在 userInfo.preference 上给出错误。{NHibernate.QueryException:无法解析属性:Otherinfo of:Location.User 这里有什么错误。是不是因为有多个嵌套对象

4

2 回答 2

4

请改用 CreateAlias:

criteria.CreateAlias("User", "user", JoinType.InnerJoin);
criteria.CreateAlias("user.Otherinfo", "userInfo",JoinType.InnerJoin); 
于 2012-05-24T14:27:40.917 回答
3

这适用于寻找嵌套投影和 NHibernate Criteria 的嵌套连接的其他人:

public class A
{
    public B {get;set;}
    public string PropertyA {get;set;}
}
public class B
{
    public C {get;set;}
}    
public class C
{
    public string CName {get;set;}
}
//you want to project and join 3 tables querying from A and get CName of C
//  InstanceA.B.C.CName

你不能使用“。” 点作为您的别名 + 您只能在别名中访问 1 级(aliasA.PropertyA)

//you have to create 3 alias for each class/instance/table
DetachedCriteria joinNested3tables = DetachedCriteria.For<A>("aliasA") //level 1 alias
            .CreateAlias("aliasA.B", "aliasB", JoinType.InnerJoin) //level 2 alias
            .CreateAlias("aliasB.C", "aliasC", JoinType.InnerJoin) //level 3 alias
          .SetProjection(Projections.ProjectionList()
            .Add(Projections.Property("aliasC.CName"), "CNameProjection")
            //you cannot have more than 1 dot operator like below
            //.Add(Projections.Property("aliasB.C.CName"), "CNameProjection")
            );
于 2012-09-23T14:26:55.327 回答