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..378abe89040d4d 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,8 @@ import jakarta.transaction.TransactionManager; import org.hibernate.Session; +import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; import io.agroal.api.AgroalDataSource; import io.quarkus.agroal.DataSource; @@ -209,7 +211,8 @@ public PanacheQueryType find(Class entityClass, String panacheQuery, Sort sor } String translatedHqlQuery = PanacheJpaUtil.createFindQuery(entityClass, panacheQuery, paramCount(params)); - return createPanacheQuery(em, translatedHqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort), params); + Dialect dialect = em.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect(); + return createPanacheQuery(em, translatedHqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort, dialect), params); } public PanacheQueryType find(Class entityClass, String panacheQuery, Map params) { @@ -230,7 +233,8 @@ public PanacheQueryType find(Class entityClass, String panacheQuery, Sort sor } String translatedHqlQuery = PanacheJpaUtil.createFindQuery(entityClass, panacheQuery, paramCount(params)); - return createPanacheQuery(em, translatedHqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort), params); + Dialect dialect = em.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect(); + return createPanacheQuery(em, translatedHqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort, dialect), params); } public PanacheQueryType find(Class entityClass, String panacheQuery, Parameters params) { @@ -298,7 +302,8 @@ public PanacheQueryType findAll(Class entityClass) { public PanacheQueryType findAll(Class entityClass, Sort sort) { String query = "FROM " + PanacheJpaUtil.getEntityName(entityClass); EntityManager em = getEntityManager(entityClass); - return createPanacheQuery(em, query, null, PanacheJpaUtil.toOrderBy(sort), null); + Dialect dialect = em.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect(); + return createPanacheQuery(em, query, null, PanacheJpaUtil.toOrderBy(sort, dialect), null); } public List listAll(Class entityClass) { diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java index 30e13c7a45c5f6..44f97850c4e4f6 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import org.hibernate.dialect.*; import org.junit.jupiter.api.Test; import io.quarkus.panache.common.Sort; @@ -18,7 +19,7 @@ public void testEmptySortByYieldsEmptyString() { @Test public void testSortBy() { Sort sort = Sort.by("foo", "bar"); - assertEquals(" ORDER BY foo , bar", PanacheJpaUtil.toOrderBy(sort)); + assertEquals(" ORDER BY \"foo\" , \"bar\"", PanacheJpaUtil.toOrderBy(sort, new H2Dialect())); } @Test @@ -30,13 +31,13 @@ public void testEmptySortEmptyYieldsEmptyString() { @Test public void testSortByNullsFirst() { Sort emptySort = Sort.by("foo", Sort.Direction.Ascending, Sort.NullPrecedence.NULLS_FIRST); - assertEquals(" ORDER BY foo NULLS FIRST", PanacheJpaUtil.toOrderBy(emptySort)); + assertEquals(" ORDER BY `foo` NULLS FIRST", PanacheJpaUtil.toOrderBy(emptySort, new MySQLDialect())); } @Test public void testSortByNullsLast() { Sort emptySort = Sort.by("foo", Sort.Direction.Descending, Sort.NullPrecedence.NULLS_LAST); - assertEquals(" ORDER BY foo DESC NULLS LAST", PanacheJpaUtil.toOrderBy(emptySort)); + assertEquals(" ORDER BY [foo] DESC NULLS LAST", PanacheJpaUtil.toOrderBy(emptySort, new SQLServerDialect())); } } diff --git a/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/AdditionalJpaOperations.java b/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/AdditionalJpaOperations.java index f136bf2ec6cf46..9809f5b81ee08d 100644 --- a/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/AdditionalJpaOperations.java +++ b/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/AdditionalJpaOperations.java @@ -14,8 +14,10 @@ import jakarta.persistence.metamodel.EntityType; import jakarta.persistence.metamodel.Metamodel; +import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.CascadeStyle; import org.hibernate.engine.spi.CascadingActions; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.metamodel.model.domain.internal.EntityTypeImpl; @@ -33,7 +35,8 @@ public static PanacheQuery find(AbstractJpaOperations jpaOperations, Class String countQuery, Sort sort, Map params) { String findQuery = createFindQuery(entityClass, query, jpaOperations.paramCount(params)); EntityManager em = jpaOperations.getEntityManager(); - Query jpaQuery = em.createQuery(sort != null ? findQuery + toOrderBy(sort) : findQuery); + Dialect dialect = em.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect(); + Query jpaQuery = em.createQuery(sort != null ? findQuery + toOrderBy(sort, dialect) : findQuery); JpaOperations.bindParameters(jpaQuery, params); return new CustomCountPanacheQuery(em, jpaQuery, countQuery, params); } @@ -48,7 +51,8 @@ public static PanacheQuery find(AbstractJpaOperations jpaOperations, Class String countQuery, Sort sort, Object... params) { String findQuery = createFindQuery(entityClass, query, jpaOperations.paramCount(params)); EntityManager em = jpaOperations.getEntityManager(); - Query jpaQuery = em.createQuery(sort != null ? findQuery + toOrderBy(sort) : findQuery); + Dialect dialect = em.unwrap(SessionFactoryImplementor.class).getJdbcServices().getDialect(); + Query jpaQuery = em.createQuery(sort != null ? findQuery + toOrderBy(sort, dialect) : findQuery); JpaOperations.bindParameters(jpaQuery, params); return new CustomCountPanacheQuery(em, jpaQuery, countQuery, params); } diff --git a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java index 6745b72f55274e..0150a91c7974e5 100644 --- a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java +++ b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java @@ -9,6 +9,8 @@ import jakarta.persistence.LockModeType; +import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.util.LockModeConverter; import org.hibernate.reactive.mutiny.Mutiny; import org.hibernate.reactive.mutiny.Mutiny.Session; @@ -109,6 +111,7 @@ public PanacheQueryType find(Class entityClass, String panacheQuery, Object.. public PanacheQueryType find(Class entityClass, String panacheQuery, Sort sort, Object... params) { Uni session = getSession(); + Dialect dialect = ((SessionFactoryImplementor) SessionOperations.getSessionFactory()).getJdbcServices().getDialect(); if (PanacheJpaUtil.isNamedQuery(panacheQuery)) { String namedQuery = panacheQuery.substring(1); if (sort != null) { @@ -117,10 +120,10 @@ public PanacheQueryType find(Class entityClass, String panacheQuery, Sort sor + "\" instead"); } NamedQueryUtil.checkNamedQuery(entityClass, namedQuery); - return createPanacheQuery(session, panacheQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort), params); + return createPanacheQuery(session, panacheQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort, dialect), params); } String hqlQuery = PanacheJpaUtil.createFindQuery(entityClass, panacheQuery, paramCount(params)); - return createPanacheQuery(session, hqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort), params); + return createPanacheQuery(session, hqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort, dialect), params); } public PanacheQueryType find(Class entityClass, String panacheQuery, Map params) { @@ -129,6 +132,7 @@ public PanacheQueryType find(Class entityClass, String panacheQuery, Map entityClass, String panacheQuery, Sort sort, Map params) { Uni session = getSession(); + Dialect dialect = ((SessionFactoryImplementor) SessionOperations.getSessionFactory()).getJdbcServices().getDialect(); if (PanacheJpaUtil.isNamedQuery(panacheQuery)) { String namedQuery = panacheQuery.substring(1); if (sort != null) { @@ -137,10 +141,10 @@ public PanacheQueryType find(Class entityClass, String panacheQuery, Sort sor + "\" instead"); } NamedQueryUtil.checkNamedQuery(entityClass, namedQuery); - return createPanacheQuery(session, panacheQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort), params); + return createPanacheQuery(session, panacheQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort, dialect), params); } String hqlQuery = PanacheJpaUtil.createFindQuery(entityClass, panacheQuery, paramCount(params)); - return createPanacheQuery(session, hqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort), params); + return createPanacheQuery(session, hqlQuery, panacheQuery, PanacheJpaUtil.toOrderBy(sort, dialect), params); } public PanacheQueryType find(Class entityClass, String query, Parameters params) { @@ -184,7 +188,8 @@ public PanacheQueryType findAll(Class entityClass) { public PanacheQueryType findAll(Class entityClass, Sort sort) { String query = "FROM " + PanacheJpaUtil.getEntityName(entityClass); Uni session = getSession(); - return createPanacheQuery(session, query, null, PanacheJpaUtil.toOrderBy(sort), null); + Dialect dialect = ((SessionFactoryImplementor) SessionOperations.getSessionFactory()).getJdbcServices().getDialect(); + return createPanacheQuery(session, query, null, PanacheJpaUtil.toOrderBy(sort, dialect), null); } public Uni> listAll(Class entityClass) { diff --git a/extensions/panache/panache-hibernate-common/runtime/pom.xml b/extensions/panache/panache-hibernate-common/runtime/pom.xml index dd0b52b346c16d..867bd6792121aa 100644 --- a/extensions/panache/panache-hibernate-common/runtime/pom.xml +++ b/extensions/panache/panache-hibernate-common/runtime/pom.xml @@ -28,6 +28,10 @@ jakarta.persistence jakarta.persistence-api + + org.hibernate.orm + hibernate-core + org.junit.jupiter junit-jupiter-api diff --git a/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java b/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java index 3029a33398242c..a7f410560d890e 100644 --- a/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java +++ b/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java @@ -3,6 +3,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.hibernate.dialect.Dialect; +import org.hibernate.dialect.H2Dialect; + import io.quarkus.panache.common.Sort; import io.quarkus.panache.common.exception.PanacheQueryException; @@ -17,6 +20,8 @@ public class PanacheJpaUtil { static final Pattern FROM_PATTERN = Pattern.compile("^\\s*FROM\\s+.*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + static final Dialect DEFAULT_DIALECT = new H2Dialect(); + public static String getCountQuery(String query) { // try to generate a good count query from the existing query Matcher selectMatcher = SELECT_PATTERN.matcher(query); @@ -163,7 +168,12 @@ public static String createDeleteQuery(Class entityClass, String query, int p return "DELETE FROM " + getEntityName(entityClass) + " WHERE " + query; } + @Deprecated public static String toOrderBy(Sort sort) { + return toOrderBy(sort, DEFAULT_DIALECT); + } + + public static String toOrderBy(Sort sort, Dialect dialect) { if (sort == null) { return null; } @@ -175,7 +185,7 @@ public static String toOrderBy(Sort sort) { Sort.Column column = sort.getColumns().get(i); if (i > 0) sb.append(" , "); - sb.append(column.getName()); + sb.append(dialect == null ? column.getName() : dialect.toQuotedIdentifier(column.getName())); if (column.getDirection() != Sort.Direction.Ascending) { sb.append(" DESC"); }