From 4c699f8d7553c0fc6c7f7921a168d0ef78a4db8f Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 7 Feb 2023 21:12:38 +0100 Subject: [PATCH] HHH-16137 Add test for issue --- .../NamedParameterInSelectAndWhereTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/NamedParameterInSelectAndWhereTest.java 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 new file mode 100644 index 0000000000..02d62f5017 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/NamedParameterInSelectAndWhereTest.java @@ -0,0 +1,66 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.query; + +import org.hibernate.testing.orm.domain.gambit.BasicEntity; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +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") +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 ); + } ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete from BasicEntity" ).executeUpdate() ); + } + + @Test + 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() + ) ); + } + + @Test + 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() + ) ); + } +}