Hello,
i have run into issues with hibernate native query.
I have pretty complex query consisting of multiple unions and many where clauses. The query works fine for about 10 calls but then slows down significantly. Changing simple param(for example removing single value from “IN” condition) makes the query fast again. The database queries takes the same amount of time but it seems like the building of the query is much slower - according to “generate_statistics” logging.
The code looks like this:
NativeQuery hibernateQuery = (NativeQuery) entityManager.createNativeQuery(
aggregationQueryBuilder.query.toString(),
"AggregationSearchResult"
)
.setFirstResult(offset)
.setMaxResults(size);
addQueryParameters(hibernateQuery, queryParams);
List<AggregationSearchResult> result = hibernateQuery.getResultList();
addQueryParams looks like this:
private void addQueryParameters(NativeQuery query, HashMap<String, Object> queryParams) {
queryParams.forEach(
(name, value) -> {
if (value instanceof Collection) {
query.setParameterList(name, (Collection) value);
} else if (value instanceof Object[]) {
query.setParameterList(name, (Object[]) value);
} else if (value instanceof LockMode) {
query.setLockMode(name, (LockMode) value);
} else {
query.setParameter(name, value);
}
}
);
where the “AggregationSearchResult” is defined using @ConstructorResult
. I am not aware of using any kind of caching, even thought it seems to be some kind of caching problem since changing the params helps.
Using hibernate 6.2.5 and spring boot 3.1.1. Upgrading to hibernate 6.4 did not fix the issue.
Any kind of help or hint where to look would be appreciated.