diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchPaginationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchPaginationTest.java new file mode 100644 index 0000000000..2fcf659b73 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchPaginationTest.java @@ -0,0 +1,159 @@ +package org.hibernate.orm.test.batch; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.annotations.BatchSize; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.Query; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@Jpa( + annotatedClasses = { + BatchPaginationTest.Article.class, + BatchPaginationTest.Tag.class + } + , + useCollectingStatementInspector = true +) +@TestForIssue( jiraKey = "HHH-16005") +public class BatchPaginationTest { + + @BeforeAll + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + List tags = List.of( new Tag( "t1" ), new Tag( "t2" ), new Tag( "t3" ) ); + List tags2 = List.of( new Tag( "t4" ), new Tag( "t5" ) ); + + Article article = new Article( "0", tags ); + Article article2 = new Article(); + Article article3 = new Article( "3", tags2 ); + Article article4 = new Article(); + Article article5 = new Article(); + + tags.forEach( entityManager::persist ); + tags2.forEach( entityManager::persist ); + + entityManager.persist( article ); + entityManager.persist( article2 ); + entityManager.persist( article3 ); + entityManager.persist( article4 ); + entityManager.persist( article5 ); + } + ); + } + + @Test + void testIt(EntityManagerFactoryScope scope) { + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); + scope.inTransaction( + entityManager -> { + statementInspector.clear(); + Query query = entityManager.createQuery( "select a from Article a" ); + List
tech = query.setMaxResults( 20 ).getResultList(); + + tech.stream() + .map( ArticleResponseDto::new ) + .toList(); + assertThat( statementInspector.getSqlQueries().size() ).isEqualTo( 2 ); + } + ); + } + + public static class ArticleResponseDto { + + private final int id; + private final List tags; + + public ArticleResponseDto(Article article) { + this.id = article.getId(); + this.tags = article.getTags().stream() + .map( TagDto::new ) + .toList(); + } + } + + public static class TagDto { + + private final String name; + + public TagDto(Tag tag) { + this.name = tag.getName(); + } + } + + @Entity(name = "Tag") + public static class Tag { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(unique = true) + private String name; + + public Tag() { + } + + public Tag(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + } + + @Entity(name = "Article") + public static class Article { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + private String categoryId; + + @BatchSize(size = 20) + @ManyToMany + private List tags = new ArrayList<>(); + + public Article() { + } + + public Article(String categoryId, List tags) { + this.categoryId = categoryId; + this.tags = tags; + } + + public int getId() { + return id; + } + + public String getCategoryId() { + return categoryId; + } + + public List getTags() { + return tags; + } + } + +}