在HQL中使用子查询

时间:2014-08-06 13:15:41

标签: sql hibernate subquery hql

我从有效的HQL查询中获得了我需要的SQL查询

  Select   Temp.*,
    (select
       case 
        when count(tab7.id)>0 then 1 
        else 0 
       end 
      from
       table7 tab7 
      where
       tab7.id = Temp.col_3_0_ 
       ) As Col_6_0_  
       from (Select
      tab5.col1 As Col_0_0_,
      tab5.col2 As Col_1_0_,
      sum(tab5.col3) as col_2_0_,
      tab5.col4 as col_3_0_,
     from

      table5 tab5,
       table1 tab1
  table2 tab2
  where
    tab2.somecol=tab5.somecol 
      and tab1.id=tab5.id 
     GROUP BY  tab5.col1    ,
      tab5.col2                 ,
       tab5.col4   ) Temp

但是当我尝试以这种方式执行HQL查询时,我正面临语法问题。

问题:

  1. 有没有办法在HQL中使用from cluase中的子查询?

  2. 如果没有,我如何在休眠中实现这一目标?

  3. 请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以使用分离的条件来触发子查询或在hibernate

中的条件中查询

下面是您的示例代码

Criteria cr = getSession().createCriteria(Payment.class, "P"); cr.add(Subqueries.propertyNotIn("P.paymentId",DetachedCriteria.forClass(Refund.class, "refund") .add(Restrictions.or(Restrictions.eq("refund.isCancelled", false), Restrictions.isNull("refund.isCancelled"))) .setProjection(Property.forName("refund.payment.paymentId"))))

欢呼声