[英]Using native sql in java criteria predicate
I have a predicate( javax.persistence.criteria.Predicate ) which filters raw data as follows: 我有一个谓词( javax.persistence.criteria.Predicate ),它按以下方式过滤原始数据:
public Predicate byAccountsId(Collection<Long> accountsId) {
ParameterExpression<?> param = createParam(AOraArrayUserType.class, new AOraArrayUserType(accountsId));
return criteriaBuilder().or(
criteriaBuilder()
.equal(criteriaBuilder().function("in_ex", Long.class, actSourceJoin().get(Account_.id),
param), 1),
criteriaBuilder().equal(
criteriaBuilder().function("in_ex", Long.class, actDestinationJoin().get(Account_.id),
param), 1));
}
This predicate builds the next part of the query: 该谓词构建查询的下一部分:
..
where
..
act.source_id in (accountsId.values())
or act.destination_id in (accountsId.values() - array ids)
..
It works fine but there may be too much raw data.I want to use "Oracle functional index", which removes unnecessary data from the query results. 它工作正常,但原始数据可能过多。我想使用“ Oracle功能索引”,该功能从查询结果中删除了不必要的数据。 I tried to rewrite my predicate as follows:
我尝试将谓词重写如下:
public Predicate byAccountsId(Collection<Long> accountsId) {
ParameterExpression<?> param = createParam(AOraArrayUserType.class, new AOraArrayUserType(accountsId));
return criteriaBuilder().or(
criteriaBuilder().literal(
Restrictions.sqlRestriction("case when state != 'ARCHIVE' then source_id else null end"))
.in(param));
}
Resurlt query builds fine but returns no result. Resurlt查询构建良好,但不返回任何结果。 But when I copy result query to sql developer and run the query it returns the expected result.
但是,当我将结果查询复制到sql developer并运行查询时,它将返回预期结果。
Part of the query which is build by new predicate: 由新谓词构建的部分查询:
..
where
..
(case when act.state != 'ARCHIVE' then act.source_id else null end) in (accountsId.values() - array ids)
..
Does anybody know why I don't get the correct result when I'm using the new predicate? 有人知道我在使用新谓词时为什么没有得到正确的结果吗? And can I use
hibernate.criterion.Restrictions
with javax.persistence.criteria.Predicate
? 我可以将
hibernate.criterion.Restrictions
与javax.persistence.criteria.Predicate
一起使用吗?
您可以在实体(AOraArrayUserType)中为表达式定义@Formula
并在条件中使用该字段
Restrictions.in("theFormulaField", param)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.