0
如何使用Hibernate的分離標準解決此查詢?對我來說最難的部分是將and u1.abrechnungsDatum is null
納入子選擇。 我想這樣的查詢:休眠DetachedCriteria子查詢中帶有和子句的查詢
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)
我有這個
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));
這給了我下面的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
)
正如你所看到的,和 - 字段abrechnungsDatum
不適用於子選擇。我怎樣才能做到這一點呢?
謝謝您的回答,但你的suggesetion拋出一個'NullPointerException'。我必須設置一個投影,所以我嘗試了這種方式: (參見上面的「編輯A」) 無論如何,這也不會給我我想要的結果,因爲那樣我就失去了連接'p.id =子查詢中的u1.patient_id'。結果SQL顯示在答案的「編輯B」中。 – Tarator 2014-11-21 11:41:51
它是否適用於您更新的查詢? – 2014-11-21 11:44:11
不,不幸的是,這不是我想要的...請參閱上面的編輯...問題是,我沒有將主要查詢的「患者」加入連接。 – Tarator 2014-11-21 11:47:46