我正在构建一个休眠标准,使用如下子选择
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
错误消息意思是“无效的标识符”