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 {
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;
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<Person> 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;
}
}