From d9a33bf405810b257cfd240c862ca0ab21a976b3 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 19 Jun 2020 12:33:25 +0100 Subject: [PATCH] HHH-14077 Add test for issue --- .../CriteriaLiteralWithSingleQuoteTest.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/literal/CriteriaLiteralWithSingleQuoteTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/literal/CriteriaLiteralWithSingleQuoteTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/literal/CriteriaLiteralWithSingleQuoteTest.java new file mode 100644 index 0000000000..cec46bd0fc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/literal/CriteriaLiteralWithSingleQuoteTest.java @@ -0,0 +1,121 @@ +package org.hibernate.orm.test.jpa.criteria.literal; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + +import org.hibernate.testing.junit5.EntityManagerFactoryBasedFunctionalTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class CriteriaLiteralWithSingleQuoteTest extends EntityManagerFactoryBasedFunctionalTest { + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { Student.class }; + } + + @Test + public void literalSingleQuoteTest() { + + inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery(); + query.select( cb.literal( '\'' ) ).from( Student.class ); + Object object = entityManager.createQuery( query ).getSingleResult(); + assertEquals( '\'', object ); + } + ); + } + + @Test + public void literalProjectionTest() { + + inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery(); + query.multiselect( cb.literal( "' || aValue || '" ) ).from( Student.class ); + Object object = entityManager.createQuery( query ).getSingleResult(); + assertEquals( "' || aValue || '", object ); + } + ); + } + + @Test + public void testLiteralProjectionAndGroupBy() { + inTransaction( + entityManager -> { + + final String literal = "' || aValue || '"; + + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery(); + query.multiselect( cb.literal( literal ) ) + .from( Student.class ); + query.groupBy( cb.literal( literal ) ); + + Object object = entityManager.createQuery( query ).getSingleResult(); + assertEquals( literal, object ); + } + ); + } + + @BeforeEach + public void setupData() { + inTransaction( + entityManager -> { + Student student = new Student(); + student.setAValue( "A Value" ); + entityManager.persist( student ); + } + ); + } + + @AfterEach + public void cleanupData() { + inTransaction( + entityManager -> { + entityManager.createQuery( "delete from Student" ); + } + ); + } + + @Entity(name = "Student") + @Table(name = "Students") + public static class Student { + + @Id + @GeneratedValue + private Long id; + + @Column + private String aValue; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + this.id = id; + } + + public String getAValue() { + return aValue; + } + + public void setAValue(String value) { + this.aValue = value; + } + } +} \ No newline at end of file