From 146cad32ad3f130912f7d7185ead21e3cad9efba Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Fri, 28 Jul 2023 19:09:24 +0200 Subject: [PATCH] HHH-17004 - Add test and fix for issue Signed-off-by: Jan Schatteman --- .../exec/spi/JdbcOperationQuerySelect.java | 2 +- .../test/jpa/query/LimitExpressionTest.java | 88 +++++++++++++------ 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcOperationQuerySelect.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcOperationQuerySelect.java index 14dab952bc..94870452f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcOperationQuerySelect.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcOperationQuerySelect.java @@ -223,7 +223,7 @@ public class JdbcOperationQuerySelect extends AbstractJdbcOperationQuery { else { value = requestedValue; } - return value != (int) jdbcParameterBinding.getBindValue(); + return value == (int) jdbcParameterBinding.getBindValue(); } } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/LimitExpressionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/LimitExpressionTest.java index fa2bea3276..86cf6a3d89 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/LimitExpressionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/LimitExpressionTest.java @@ -7,48 +7,84 @@ package org.hibernate.orm.test.jpa.query; import java.util.List; + import jakarta.persistence.Entity; -import jakarta.persistence.EntityManager; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; -import jakarta.persistence.Query; +import jakarta.persistence.TypedQuery; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jpa; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.transaction.TransactionUtil; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeAll; -import static junit.framework.TestCase.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Andrea Boriero */ -public class LimitExpressionTest extends BaseEntityManagerFunctionalTestCase { +@Jpa( + annotatedClasses = { LimitExpressionTest.Person.class } +) +public class LimitExpressionTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{Person.class}; + @Test + @JiraKey(value = "HHH-11278") + public void testAnEmptyListIsReturnedWhenSetMaxResultsToZero(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + final jakarta.persistence.Query query = entityManager.createQuery( "from Person p" ); + final List list = query.setMaxResults( 0 ).getResultList(); + assertTrue( list.isEmpty(), "The list should be empty with setMaxResults 0" ); + } + ); } @Test - @TestForIssue(jiraKey = "HHH-11278") - public void testAnEmptyListIsReturnedWhenSetMaxResultsToZero() { - TransactionUtil.doInJPA( this::entityManagerFactory, (EntityManager entityManager) -> { - final Query query = entityManager.createQuery( "from Person p" ); - final List list = query.setMaxResults( 0 ).getResultList(); - assertTrue( "The list should be empty with setMaxResults 0", list.isEmpty() ); - } ); + @JiraKey(value = "HHH-17004") + public void testLimitReuse(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + TypedQuery query; + query = entityManager.createQuery( "from Person m", Person.class); + query.setMaxResults(10); + assertEquals( 10, query.getResultList().size() ); + + query = entityManager.createQuery("from Person m", Person.class); + query.setMaxResults(10); + query.setFirstResult(2); + assertEquals( 8, query.getResultList().size() ); + + query = entityManager.createQuery("from Person m", Person.class); + query.setMaxResults(10); + assertEquals( 10, query.getResultList().size() ); + } + ); } - @Before - public void prepareTest() throws Exception { - TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> { - Person p = new Person(); - entityManager.persist( p ); - } ); + @BeforeAll + public void prepareTest(EntityManagerFactoryScope scope) throws Exception { + scope.inTransaction( + entityManager -> { + Person p; + for ( int i = 0; i < 10; i++ ) { + p = new Person(); + entityManager.persist( p ); + } + } + ); + } + + @AfterAll + public void tearDown(EntityManagerFactoryScope scope) throws Exception { + scope.inTransaction( + entityManager -> entityManager.createQuery( "delete from Person" ).executeUpdate() + ); } @Entity(name = "Person") @@ -56,6 +92,6 @@ public class LimitExpressionTest extends BaseEntityManagerFunctionalTestCase { @Id @GeneratedValue private Long id; - } + }