使用条件作为子查询

时间:2016-02-24 09:20:16

标签: java hibernate hql criteria

"select nation,count(Cid) 
     from Consumer 
         where Cid in(
             select user_id from user 
                 where merchant_id='"+Mid +"') 
         and nation='"+"ind"+"' 
     group by (nation)";

这是查询,我不知道如何使用条件编写子查询....如果任何人可以将上述查询转换为条件,将会更有帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试此查询:

DetachedCriteria userSubquery = DetachedCriteria.forClass(User.class, "u")
    .add(Property.forName("u.merchant_id").eq("Mid"))
    .add(Property.forName("u.nation").eq("ind"))
    .setProjection( Projections.property("u.user_id") );

ProjectionList projectionList = Projections.projectionList()
    .add(Projections.groupProperty("nation"))
    .add(Projections.rowCount());

Criteria query = session.createCriteria(Consumer.class, "c")
    .add( Subqueries.propertyIn("c.cid", userSubquery) )
    .setProjection(projectionList);
List results = query.list();