From dc787f0fb02549831eaad7470a21222c4d33fb3e Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Tue, 19 Sep 2023 08:44:15 -0300 Subject: [PATCH] Enable `HibernateHints.HINT_READ_ONLY` hint to `count()` queries 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 #35978 --- .../panache/common/runtime/AbstractJpaOperations.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java index ebcb984482cf59..866add3b39df47 100644 --- a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java +++ b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java @@ -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; @@ -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(); } @@ -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); } @@ -349,7 +353,9 @@ public long count(Class entityClass, String panacheQuery, Map 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); }