简体   繁体   English

在subselect中使用and子句的Hibernate DetachedCriteria查询

[英]Hibernate DetachedCriteria Query with and clause in subselect

How can I solve this query with Hibernate's detached criteria? 如何使用Hibernate的分离条件解决此查询? The hardest part for me is to bring the and u1.abrechnungsDatum is null into the subselect. 对我来说,最难的部分是将and u1.abrechnungsDatum is null放入子选择中。 I want a query like this: 我想要这样的查询:

select * 
from 
   patient as p
where 
   p.krankenstand = ?
and 
   ? < ( select count(*) from ueberweisung u1 where  p.id = u1.patient_id
         and u1.abrechnungsDatum is null)

I have tried it with this 我已经尝试过了

DetachedCriteria dc = DetachedCriteria.forClass(Patient.class, "patient");
dc.add(Restrictions.eq("krankenstand", true));

DetachedCriteria nab = dc.createCriteria("ueberweisungs", "nab");
nab.add(Restrictions.isNull("nab.abrechnungsDatum"));
dc.add(Restrictions.sizeGt("ueberweisungs", 0));

which gives me the following SQL-Statement 这给了我下面的SQL语句

select
    *
from
    patient this_ 
inner join
    ueberweisung nab1_ 
        on this_.id=nab1_.patient_id 
where
    this_.krankenstand=? 
    and nab1_.abrechnungsDatum is null 
    and ? < (
        select
            count(*) 
        from
            ueberweisung 
        where
            this_.id=patient_id
    )

As you can see, the and-clause for the field abrechnungsDatum is not inside the subselect. 如您所见,字段abrechnungsDatum的和子句不在子选择内。 How can I achieve to get this inside? 我如何才能获得这个内在?

Try this instead: 尝试以下方法:

DetachedCriteria dc = DetachedCriteria.forClass(Patient.class, "patient");
dc.add(Restrictions.eq("krankenstand", true));

DetachedCriteria subquery = DetachedCriteria.forClass(Ueberweisung.class);
subquery.add(Restrictions.isNull("abrechnungsDatum"));

dc.add(Subqueries.ltAll(0, subquery));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM