NHibernate标准

时间:2011-06-30 07:36:36

标签: nhibernate nhibernate-criteria

public class A
{    
public string aname {get; set;}
public string aId {get; set;}                                                
public string bId {get; set;}
}

public class B 
{    
public string bId {get; set;}                                                
public string bname {get; set;}                                                  
public string cId {get; set;}
}

public class C 
{                                                                                
public string cId {get; set;}
public string cfirstname {get; set;}    
public string clastname {get; set;}
}

public class abcDTO
{
public string aname {get; set;}
public string bname {get; set;} 
public string clastname {get; set;}
}

我正在寻找的查询是

SELECT a.aid, b.bname, c.clastname FROM A thisa 
inner join B thisb on thisa.bid=thisb.bid
inner join C thisc  on thisb.cid=thisc.cid and this_.POLICY_SEARCH_NBR like '%-996654%'

我正在尝试的标准是,请让我知道编写标准的最佳方法,以便我可以获得abcdto对象

var policyInsuranceBusiness = DetachedCriteria.For<A>()
                .SetProjection(Projections.Property("a.aid"))
                .Add(Restrictions.Like("a.aid", "1-SAP-3-996654", MatchMode.Anywhere))
                .CreateCriteria("b.bid", "b", JoinType.InnerJoin) 
                .SetProjection(Projections.Property("b.bname")) // ERROR OUT - COULD NOT RESOLVE PROPERTY
                .CreateCriteria("c.cid", "c", JoinType.InnerJoin)
                .SetProjection(Projections.Property("c.clastname")); // ERROR - COULD NOT RESOLVE PROPERTY

IList<abcDTO> plo = policyInsuranceBusiness.GetExecutableCriteria(_session).SetResultTransformer(NHibernate.Transform.Transformers
                .AliasToBean<abcDTO).List<abcDTO>();

1 个答案:

答案 0 :(得分:1)

如果没有在类和映射中定义它们之间的关系,就不能在NHibernate中连接对象。

所以以你的例子为例,我估计下面的类定义:

public class A {    
    public virtual string Name {get; set;}
    public virtual string PolicySearchNumber {get; set;}
    public virtual int Id {get; set;}                                                
    public virtual B B {get; set;}
}

public class B {    
    public virtual int Id {get; set;}                                                
    public virtual string Name {get; set;}                                                  
    public virtual C C {get; set;}
}

public class C {                                                                                
    public virtual int Id {get; set;}
    public virtual string Firstname {get; set;}    
    public virtual string Lastname {get; set;}
}

public class abcDTO {
    public string int aid {get; set;}
    public string aname {get; set;}
    public string bname {get; set;} 
    public string clastname {get; set;}
}

您的Criteria Query将是:

var results = session.CreateCriteria<A>()
    .CreateCriteria("B", "b")
    .CreateCriteria("b.C", "c")
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Id"), "aid")
        .Add(Projections.Property("Name"), "aname")
        .Add(Projections.Property("b.Name"), "bname")
        .Add(Projections.Property("c.Lastname"), "clastname")
    )
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(abcDTO)))
    .List<abcDTO>();

您可以选择选择内部或左侧连接,具体取决于您在条件查询中处理空引用的方式。