HHH-17004 - Add test and fix for issue

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2023-07-28 19:09:24 +02:00 committed by Jan Schatteman
parent c0189c0bd4
commit b283813ee1
2 changed files with 63 additions and 27 deletions

View File

@ -223,7 +223,7 @@ public class JdbcOperationQuerySelect extends AbstractJdbcOperationQuery {
else { else {
value = requestedValue; value = requestedValue;
} }
return value != (int) jdbcParameterBinding.getBindValue(); return value == (int) jdbcParameterBinding.getBindValue();
} }
} }
} }

View File

@ -7,48 +7,84 @@
package org.hibernate.orm.test.jpa.query; package org.hibernate.orm.test.jpa.query;
import java.util.List; import java.util.List;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id; 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.junit.jupiter.api.AfterAll;
import org.hibernate.testing.transaction.TransactionUtil; import org.junit.jupiter.api.Test;
import org.junit.Before; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test;
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 * @author Andrea Boriero
*/ */
public class LimitExpressionTest extends BaseEntityManagerFunctionalTestCase { @Jpa(
annotatedClasses = { LimitExpressionTest.Person.class }
)
public class LimitExpressionTest {
@Override @Test
protected Class<?>[] getAnnotatedClasses() { @JiraKey(value = "HHH-11278")
return new Class[]{Person.class}; 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 @Test
@TestForIssue(jiraKey = "HHH-11278") @JiraKey(value = "HHH-17004")
public void testAnEmptyListIsReturnedWhenSetMaxResultsToZero() { public void testLimitReuse(EntityManagerFactoryScope scope) {
TransactionUtil.doInJPA( this::entityManagerFactory, (EntityManager entityManager) -> { scope.inTransaction(
final Query query = entityManager.createQuery( "from Person p" ); entityManager -> {
final List list = query.setMaxResults( 0 ).getResultList(); TypedQuery<Person> query;
assertTrue( "The list should be empty with setMaxResults 0", list.isEmpty() ); 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 @BeforeAll
public void prepareTest() throws Exception { public void prepareTest(EntityManagerFactoryScope scope) throws Exception {
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> { scope.inTransaction(
Person p = new Person(); entityManager -> {
Person p;
for ( int i = 0; i < 10; i++ ) {
p = new Person();
entityManager.persist( p ); entityManager.persist( p );
} ); }
}
);
}
@AfterAll
public void tearDown(EntityManagerFactoryScope scope) throws Exception {
scope.inTransaction(
entityManager -> entityManager.createQuery( "delete from Person" ).executeUpdate()
);
} }
@Entity(name = "Person") @Entity(name = "Person")
@ -56,6 +92,6 @@ public class LimitExpressionTest extends BaseEntityManagerFunctionalTestCase {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private Long id;
}
} }
}