Hibernate 是否将数组位置与JPA标准API一起使用?

Hibernate 是否将数组位置与JPA标准API一起使用?,hibernate,function,jpa,criteria,criteria-api,Hibernate,Function,Jpa,Criteria,Criteria Api,我使用的是springjpa和postgresql,我的数据库中有一个字段是bitint数组类型,在Java中是一个长值 我正在使用spring jpa CriteriaBuilder&Specification制作一个数组位置函数: private Predicate toPredicate(String propName, Query q, Root<T> root, CriteriaBuilder cb) { ... return cb.isNotNull(cb

我使用的是springjpa和postgresql,我的数据库中有一个字段是bitint数组类型,在Java中是一个长值

我正在使用spring jpa CriteriaBuilder&Specification制作一个数组位置函数:

private Predicate toPredicate(String propName, Query q, Root<T> root, CriteriaBuilder cb) {
    ...
    return cb.isNotNull(cb.function("array_position", Integer.class,
                            xx expression,
                            cb.literal(q.value)));  // q.value is a long type value
}

jpa似乎无法识别我的长类型值,我尝试使用:

BigInteger bigInteger = new BigInteger(String.valueOf(q.value));
return cb.isNotNull(cb.function("array_position", Integer.class,
                            toExpression(propName, root),
                            cb.literal(bigInteger)));                    
还是错误。 如果我直接在pg中执行SQL查询:

select * from tbl_ruletemp t where array_position(t.user_id, cast(1 as BIGINT)) is not null;
会有用的


和想法?

您可以使用标准生成器创建一个
cast
表达式,但这可能不是必需的。与其将数字用作文字,不如将其作为参数传递

ParameterExpression<BigInteger> pid = cb.parameter(BigInteger.class, "pid");
return cb.isNotNull(cb.function("array_position", Integer.class,
                            toExpression(propName, root),
                            pid));
ParameterExpression pid=cb.parameter(BigInteger.class,“pid”);
返回cb.isNotNull(cb.function(“数组位置”),Integer.class,
toExpression(propName,root),
pid);

然后像通常那样简单地绑定参数。由于您使用的是数组,也许最好使用
arrayoverlap
而不是链接
array\u position
调用和
表达式。

对不起,您能更清楚一点吗?如何用ParameterExpression绑定我的值。使用
entityManager.createQuery
创建查询后,您可以调用
setParameter(“pid”),bigIntValue)
ParameterExpression<BigInteger> pid = cb.parameter(BigInteger.class, "pid");
return cb.isNotNull(cb.function("array_position", Integer.class,
                            toExpression(propName, root),
                            pid));