【问题标题】:nHibernate JoinnHibernate 加入
【发布时间】:2011-06-29 06:53:45
【问题描述】:

我是 NHibernate 的新手。是否可以为以下场景创建标准?

public class A{
    public string name {get; set;}
}

public class B {
    public string name {get; set;}
}

public class C {
    public string firstname {get; set;}
    public string lastname {get; set;}
}

最终我想构建以下查询

SELECT a.*, b.*, c.* FROM A AS a
INNER JOIN B as b ON a.id = b.Id 
INNER JOIN C AS c ON b.id = c.Id
WHERE o.lastname like '%ted%' 
OR c.firstname like '%test%' 
OR b.name like '%test%' 
OR a.name like '%test%'

编辑:根据作者的评论分类。

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

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

public class C
{ 
  public string firstname {get; set;} 
  public string lastname {get; set;} 
}

【问题讨论】:

  • 类模型中缺少一些引用。不应该有从a到b到c的引用吗?还是有继承?
  • o.lastname 是什么?请提供正确的 sn-ps,我不明白你真正想要做什么。仍然不清楚你是如何从 a / b 到 c 的。

标签: nhibernate nhibernate-criteria


【解决方案1】:

我认为这与您所追求的很接近。可能存在创建多个“或”标准的更优雅的方法。我假设您上面的类确实有 id 并且相互引用,因为它们不包含在您上面的基本类中。

        ICriteria criteria = Session.CreateCriteria(typeof(a));

        criteria.CreateAlias("a.Id", "b", JoinType.InnerJoin);
        criteria.CreateAlias("b.Id", "c", JoinType.InnerJoin);

        criteria.Add(
            Expression.Or(

                Expression.Or(
                    Expression.Like("c.LastName", "Ted"),
                    Expression.Like("c.LastName", "Test")),

                Expression.Or(
                    Expression.Like("b.Name", "Ted"),
                    Expression.Like("a.Name", "Test"))
            )
        );

        criteria.List();

根据您的 sql 语句,我假设您的类模型应如下所示

public class A
{    
public string name {get; set;}
public B Id {get; set;}
}

public class B 
{    
public C Id {get; set;}
public string name {get; set;}
}

public class C 
{    
public string firstname {get; set;}    
public string lastname {get; set;}
}

【讨论】:

  • 您可以使用 Disjunction 而不是嵌套 OR: Disjunction d = new Disjunction().Add(Expression.Like("c.LastName", "Ted")).Add(Expression.Like( "c.LastName", "Test")).Add(Expression.Like("b.Name", "Ted")).Add(Expression.Like("a.Name", "Test"));标准.Add(d);
  • @mathieu 好东西。根据您的评论做了一些谷歌搜索,分离看起来更整洁。一个很好的解释位于这里...Disjunction Example
  • a.Id 到底是什么?为什么它被赋予别名 b?
  • @StefanSteinegger 从原始类模型来看,这有点不清楚,但我假设 sql 语句是正确的。如果这让事情变得更清楚,我已经更新了我的答案
  • 谢谢,对于以下场景,我尝试使用分离标准 public class A { public string aname {get;设置;} 公共字符串 aId {get;设置;}}公共类B {公共字符串bId {get;设置;} 公共字符串 bname {get;设置;}}公共类C {公共字符串名字{get;设置;}公共字符串姓{get;设置;}}
【解决方案2】:

这可能会有所帮助

private IQueryOver<parententity>GetQuery(string nameA ,string nameB,string fname , string lname ){
          Conjuction Acon = Restriction.Conjunction;conjuction Bcon = Restriction.Conjunction;
          Conjuction Ccon = Restriction.Conjunction;

 if(string.isNullOrEmpty(nameA))
    Acon.Add(Restrictions.Like(Projections<Aentity>(x => x.name), nameA),MatachMode.Any););
 if(string.isNullOrEmpty(nameB))
    Acon.Add(Restrictions.Like(Projections<Bentity>(x => x.name), nameB),MatachMode.Any););
 if(string.isNullOrEmpty(fname))
    Acon.Add(Restrictions.Like(Projections<Centity>(x => x.Fname), Fname),MatachMode.Any););
 if(string.isNullOrEmpty(Lname))
    Acon.Add(Restrictions.Like(Projections<Centity>(x => x.Lname), Lname),MatachMode.Any););

       IQueryOver<Aentity> query = NHSession.QueryOver<AEniity>()
                  .where(Acon)
                  .joinQueryOver(x => x.Bcon)
                  .where(Bcon)
                  .joinQueryOver(x=> c.Ccon)
                  .where(Ccon);   

             return query;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多