diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/ToHqlStringTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/ToHqlStringTest.java new file mode 100644 index 0000000000..7d0179726c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/ToHqlStringTest.java @@ -0,0 +1,77 @@ +package org.hibernate.orm.test.query; + +import org.hibernate.query.spi.SqmQuery; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Query; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Expression; +import jakarta.persistence.criteria.Root; + +@Jpa( + annotatedClasses = ToHqlStringTest.TestEntity.class +) +@TestForIssue( jiraKey = "HHH-15389") +public class ToHqlStringTest { + + @Test + public void testCriteriaCountDistinctToHqlString(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = builder.createQuery( Long.class ); + + Root root = criteriaQuery.from( TestEntity.class ); + Expression countDistinct = builder.countDistinct( root ); + criteriaQuery = criteriaQuery.select( countDistinct ); + + TypedQuery query = entityManager.createQuery( criteriaQuery ); + ( (SqmQuery) query ).getSqmStatement().toHqlString(); + } + ); + } + + @Test + public void testHqlCountDistinctToHqlString(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Query query = entityManager.createQuery( "select count (distinct t) from TestEntity t" ); + ( (SqmQuery) query ).getSqmStatement().toHqlString(); + } + ); + } + + @Test + public void testDynamicInstantiationToHqlString(EntityManagerFactoryScope scope){ + scope.inTransaction( + entityManager -> { + Query query = entityManager.createQuery( "select new org.hibernate.orm.test.query.ToHqlStringTest$TestDto(" + + " t.id, t.name ) from TestEntity t" ); + ( (SqmQuery) query ).getSqmStatement().toHqlString(); + } + ); + } + + public static class TestDto { + @Id + public Integer id; + + public String name; + } + + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + public Integer id; + + public String name; + } +}