条件何处消除hql中的重复

时间:2015-09-16 13:44:49

标签: hibernate hql

如何简化下一个where子句?:

    where
        e.withdrawalMonth is null
        and ((:debt = 1 and not exists
            (from
            PaidConcept cc
                join cc.invoice i
             where
             cc.enrollment = e
             and cc.month = m
             and cc.concept = co
             and i.status = 'PAID'
            ))
         or(:debt = 0 and exists
            (from
            PaidConcept cc
                join cc.invoice i
             where
             cc.enrollment = e
             and cc.month = m
             and cc.concept = co
             and i.status = 'PAID'
            ))
        )

有两个相等的子查询,但第一个是not exists,第二个是exists我希望在debt参数为1时首先运行,在debt参数为0时最后运行

怎么可以做得更干?

1 个答案:

答案 0 :(得分:0)

String subquery = (from
            PaidConcept cc
                join cc.invoice i
             where
             cc.enrollment = e
             and cc.month = m
             and cc.concept = co
             and i.status = 'PAID'
            ))

String query = "where e.withdrawalMonth is null 
      + " and ((:debt = 1 and not exists "
      + subquery 
      + " or(:debt = 0 and exists " 
      + subquery + ")"