我構建一個Hibernate的標準,使用子查詢如下NULL,在Hibernate API的標準再選擇
DetachedCriteria subselect =
DetachedCriteria.forClass(NhmCode.class, "sub"); // the subselect selecting the maximum 'validFrom'
subselect.add(Restrictions.le("validFrom", new Date())); // it should be in the past (null needs handling here)
subselect.add(Property.forName("sub.lifeCycle").eqProperty("this.id")); // join to owning entity
subselect.setProjection(Projections.max("validFrom")); // we are only interested in the maximum validFrom
Conjunction resultCriterion = Restrictions.conjunction();
resultCriterion.add(Restrictions.ilike(property, value)); // I have other Restrictions as well
resultCriterion.add(Property.forName("validFrom").eq(subselect)); // this fails when validFrom and the subselect return NULL
return resultCriterion;
它工作正常,到目前爲止處理,但在最後一行的限制之前return語句是假的當validFrom和subselect結果爲NULL時。
我需要的是一個處理這種情況爲真的版本。可能通過應用NVL或聯合或類似的。
我該怎麼做?
更新:有這樣的where子句中sqlRestriction結果----------------------------
彼得斯想法:
...
and (
nhmcode1_.valid_from = (
select
max(sub_.valid_from) as y0_
from
nhm_code sub_
where
sub_.valid_from<=?
and sub_.lc_id=this_.id
)
or (
nhmcode1_.valid_from is null
and sub.validFrom is null
)
)
...
這又導致:
ORA-00904: 「SUB _」 「VALIDFROM」:ungültigerBezeichner
錯誤消息的意思是 '無效的標識符'
剛剛發現這個:http://www.opendocs.net/javadoc/hibernate/3/org/hibernate/criterion/Subqueries.html ...也許這有些用處? – 2010-12-10 12:40:10