From 3942961eebf9ce0486d2924bfb484be240b4e7bd Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 22 Mar 2023 11:28:18 +0100 Subject: [PATCH] HHH-16305 Add test for issue --- .../NamedParameterInSelectAndWhereTest.java | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/NamedParameterInSelectAndWhereTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/NamedParameterInSelectAndWhereTest.java index 02d62f5017..a8d29f743d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/NamedParameterInSelectAndWhereTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/NamedParameterInSelectAndWhereTest.java @@ -6,61 +6,70 @@ */ package org.hibernate.orm.test.query; -import org.hibernate.testing.orm.domain.gambit.BasicEntity; +import java.time.LocalDate; + +import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import jakarta.persistence.Tuple; - import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Marco Belladelli */ @SessionFactory -@DomainModel(annotatedClasses = BasicEntity.class) -@JiraKey("HHH-16137") +@DomainModel( annotatedClasses = EntityOfBasics.class ) public class NamedParameterInSelectAndWhereTest { @BeforeAll public void setUp(SessionFactoryScope scope) { scope.inTransaction( session -> { - BasicEntity be1 = new BasicEntity( 1, "one" ); - session.persist( be1 ); - BasicEntity be2 = new BasicEntity( 2, "two" ); - session.persist( be2 ); + final EntityOfBasics e1 = new EntityOfBasics( 1 ); + final EntityOfBasics e2 = new EntityOfBasics( 2 ); + e2.setTheLocalDate( LocalDate.EPOCH ); + session.persist( e1 ); + session.persist( e2 ); } ); } @AfterAll public void tearDown(SessionFactoryScope scope) { - scope.inTransaction( session -> session.createMutationQuery( "delete from BasicEntity" ).executeUpdate() ); + scope.inTransaction( session -> session.createMutationQuery( "delete from EntityOfBasics" ).executeUpdate() ); } @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-16137" ) public void testSelectAndWhere(SessionFactoryScope scope) { - scope.inTransaction( session -> assertEquals( - 1, - session.createQuery( "SELECT :param FROM BasicEntity be WHERE be.id > :param", Integer.class ) - .setParameter( "param", 1 ) - .getSingleResult() - ) ); + scope.inTransaction( session -> assertEquals( 1, session.createQuery( + "select :param from EntityOfBasics e where e.id > :param", + Integer.class + ) + .setParameter( "param", 1 ) + .getSingleResult() ) ); } @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-16137" ) public void testSelectAndWhereIsNull(SessionFactoryScope scope) { - scope.inTransaction( session -> assertEquals( - 1, - session.createQuery( - "SELECT :param FROM BasicEntity be WHERE :param is null or be.id > :param", - Integer.class - ) - .setParameter( "param", 1 ) - .getSingleResult() - ) ); + scope.inTransaction( session -> assertEquals( 1, session.createQuery( + "select :param from EntityOfBasics be where :param is null or be.id > :param", + Integer.class + ).setParameter( "param", 1 ) + .getSingleResult() ) ); + } + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-16305" ) + public void testSelectFunctionAndWhere(SessionFactoryScope scope) { + scope.inTransaction( session -> assertEquals( 0, session.createQuery( + "select timestampdiff(year, e.theLocalDate, :date) from EntityOfBasics e where e.theLocalDate <= :date", + Long.class + ) + .setParameter( "date", LocalDate.EPOCH ) + .getSingleResult() ) ); } }