【发布时间】:2011-02-05 07:40:45
【问题描述】:
我正在构建一个 Hibernate Criterion,使用子选择如下
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;
到目前为止它工作正常,但是当validFrom和subselect导致NULL时,return语句前最后一行的限制为false。
我需要的是一个能够处理这种情况的版本。可能通过应用 NVL 或合并或类似方法。
我该怎么做?
更新:----------------------------
带有 sqlRestriction 的 Péters 想法导致了这样的 where 子句:
...
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ültiger Bezeichner
错误信息意思是“无效的标识符”
【问题讨论】:
-
刚刚发现这个:opendocs.net/javadoc/hibernate/3/org/hibernate/criterion/… ...也许这有点用?
标签: hibernate null criteria subquery nvl