Skip to content

Commit

Permalink
Enable HibernateHints.HINT_READ_ONLY hint to count() queries
Browse files Browse the repository at this point in the history
This adds the HibernateHints.HINT_READ_ONLY to the `count()` calls.
Named queries can specify the hint in the @NamedQuery annotation, so
they don't need to be explicitly set.

- Fixes quarkusio#35978
  • Loading branch information
gastaldi committed Sep 19, 2023
1 parent 3bf8a5a commit dc787f0
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.transaction.TransactionManager;

import org.hibernate.Session;
import org.hibernate.jpa.HibernateHints;

import io.agroal.api.AgroalDataSource;
import io.quarkus.agroal.DataSource;
Expand Down Expand Up @@ -320,6 +321,7 @@ public Stream<?> streamAll(Class<?> entityClass, Sort sort) {
public long count(Class<?> entityClass) {
return (long) getEntityManager(entityClass)
.createQuery("SELECT COUNT(*) FROM " + PanacheJpaUtil.getEntityName(entityClass))
.setHint(HibernateHints.HINT_READ_ONLY, true)
.getSingleResult();
}

Expand All @@ -333,7 +335,9 @@ public long count(Class<?> entityClass, String panacheQuery, Object... params) {
return (long) bindParameters(
getEntityManager(entityClass)
.createQuery(PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params))),
params).getSingleResult();
params)
.setHint(HibernateHints.HINT_READ_ONLY, true)
.getSingleResult();
} catch (IllegalArgumentException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
Expand All @@ -349,7 +353,9 @@ public long count(Class<?> entityClass, String panacheQuery, Map<String, Object>
return (long) bindParameters(
getEntityManager(entityClass)
.createQuery(PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params))),
params).getSingleResult();
params)
.setHint(HibernateHints.HINT_READ_ONLY, true)
.getSingleResult();
} catch (IllegalArgumentException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
Expand Down

0 comments on commit dc787f0

Please sign in to comment.