From bd740f2dbfa84642cf59e92b81df8519ef682384 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 3 Apr 2024 18:47:07 +0200 Subject: [PATCH] HHH-17826 Add test for issue --- .../ForwardOnlyScrollNoTransactionTest.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ForwardOnlyScrollNoTransactionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ForwardOnlyScrollNoTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ForwardOnlyScrollNoTransactionTest.java new file mode 100644 index 0000000000..8ef1f08bbb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/iterate/ForwardOnlyScrollNoTransactionTest.java @@ -0,0 +1,108 @@ +package org.hibernate.orm.test.iterate; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.ScrollMode; +import org.hibernate.ScrollableResults; +import org.hibernate.query.spi.QueryImplementor; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; + +@DomainModel( + annotatedClasses = { + ForwardOnlyScrollNoTransactionTest.Father.class, + ForwardOnlyScrollNoTransactionTest.Son.class + } +) +@SessionFactory +@Jira("HHH-17826") +public class ForwardOnlyScrollNoTransactionTest { + + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Son son = new Son( "Andrea" ); + Father father = new Father( "Lionello" ); + father.addSon( son ); + session.persist( father ); + + Son son1 = new Son( "Alberto" ); + Father father1 = new Father( "Roberto" ); + father1.addSon( son1 ); + session.persist( father1 ); + } + ); + } + + @Test + public void testScroll(SessionFactoryScope scope) { + scope.inSession( + session -> { + QueryImplementor query = session.createQuery( "select f from Father f" ); + + try (ScrollableResults result = query.scroll( ScrollMode.FORWARD_ONLY )) { + while ( result.next() ) { + result.get(); + } + } + } + ); + } + + @Entity(name = "Father") + public static class Father { + @Id + @GeneratedValue + private Integer id; + + private String name; + + @OneToMany(mappedBy = "father", fetch = FetchType.EAGER) + public List sons = new ArrayList<>(); + + public Father() { + } + + public Father(String name) { + this.name = name; + } + + public void addSon(Son son) { + sons.add( son ); + son.father = this; + } + } + + @Entity(name = "Son") + public static class Son { + @Id + @GeneratedValue + private Integer id; + + private String name; + + @ManyToOne + private Father father; + + public Son() { + } + + public Son(String name) { + this.name = name; + } + } +}