我想使用以下所示构建有子句CriteriaBuilder
:
select objectid,
sum(case when attr_meta = 'severity' then 1 else 0 end) as severity,
sum(case when attr_meta = 'priority' then 1 else 0 end) as priority
from object d
group by objectid
having sum(case when attr_meta = 'severity' then 1 else 0 end) != 1
or sum(case when attr_meta = 'priority' then 1 else 0 end) != 1;
我尝试了以下方法:
Predicate p = cb.equal(cb.sum(cb.<Integer>selectCase()
.when(cb.equal(root.get("name"), 'severity'), 1).otherwise(0)), 1);
p = cb.or(p, cb.equal(cb.sum(cb.<Integer>selectCase()
.when(cb.equal(root.get("name"), 'priority'), 1).otherwise(0)), 1));
但这给出了以下例外:
java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at org.hibernate.ejb.criteria.ValueHandlerFactory.isNumeric(ValueHandlerFactory.java:70)
at org.hibernate.ejb.criteria.predicate.ComparisonPredicate.<init>(ComparisonPredicate.java:69)
at org.hibernate.ejb.criteria.CriteriaBuilderImpl.equal(CriteriaBuilderImpl.java:392)
异常似乎来自外部CriteriaBuilderImpl.equal()
调用,即包含调用的selectCase()
调用。
调用内部equal()
需要Expression
. 该selectCase()
调用在创建Expression
时将类型输入为null
。我们有办法处理这种情况吗?是一种让equal()
知道类型的方法,还是对上述查询的完全不同的方法?