JPA How to get the number of parameter markers used in Criteria API?

隐身守侯 提交于 2019-12-25 03:55:05

问题


How do I get the number of parameter markers used in Criteria API?

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(...);
}

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.


回答1:


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));
}


来源:https://stackoverflow.com/questions/25546038/jpa-how-to-get-the-number-of-parameter-markers-used-in-criteria-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!