Jen*_*der 2 null hibernate criteria subquery nvl
我正在构建一个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;
Run Code Online (Sandbox Code Playgroud)
到目前为止它工作正常,但当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
)
)
...
Run Code Online (Sandbox Code Playgroud)
这反过来导致:
ORA-00904:"SUB _"."VALIDFROM":ungültigerBezeichner
错误消息,表示"无效标识符"
你可以尝试这样的东西,而不是有问题的线:
resultCriterion.add(
Restrictions.or(
Restrictions.and(
Restrictions.isNull("validFrom"),
Restrictions.sqlRestriction("sub.validFrom is null")
),
Property.forName("validFrom").eq(subselect)
)
);
Run Code Online (Sandbox Code Playgroud)
这可能不会立即起作用,但希望有所帮助.