JPA如何获取Criteria API中使用的参数标记的数量? [英] JPA How to get the number of parameter markers used in Criteria API?
问题描述
如何获取Criteria API中使用的参数标记的数量?
How do I get the number of parameter markers used in Criteria API?
我正在使用Criteria API通过具有许多参数标记的IN
关键字创建SQL语句.
I am using Criteria API to create SQL statement with IN
keyword that have many parameter markers.
CriteriaBuilder cb = ...
...
CriteriaBuilder.In<String> in = cb.in(...);
...
for (...) {
in.value(...);
}
我开始得到SQLException
准备好的或可调用的语句具有2000多个参数标记."
I started getting SQLException
"Prepared or callable statement has more than 2000 parameter markers."
有没有一种方法可以获取到目前为止使用的参数标记的实际数量?我可以自己算一下,但这很容易出错.
Is there a way to get the actual number of parameter markers used so far? I could count them by myself, but it is error prone.
推荐答案
既然您知道某个时候可能会达到参数限制,因为它不可能是无限的,为什么不在代码中处理呢?
Since you know you are liable to hit a parameter limit at some point, since it cannot be infinite, why not handle this in your code?
将参数数组分成多个块,对每个块运行查询,然后将结果组合成一个循环.
Break your parameter array into chunks, run your query for each chunk and assemble your results in a loop.
然后,您不必关心实际的参数数量是多少.它永远不会大于您决定的大小.
Then you don't care what the actual number of parameters is. It will never be larger than the size you decide.
public static List<<List<Long>> chunkifyIdList(List<Long>, int chunkSize) {
...
}
List<MyObject> results = new ArrayList<MyObject>();
for (List<Long> chunk: chunkifyIdList(inputData)) {
results.add(myDAO.findAll(chunk));
}
这篇关于JPA如何获取Criteria API中使用的参数标记的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!