HHH-17826 Add test for issue

This commit is contained in:
Andrea Boriero 2024-04-03 18:47:07 +02:00 committed by Andrea Boriero
parent aa7575aabd
commit bd740f2dbf
1 changed files with 108 additions and 0 deletions

View File

@ -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<Son> 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;
}
}
}