问题
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