diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/fetch/FetchModeSubselectAndOrderByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/fetch/FetchModeSubselectAndOrderByTest.java new file mode 100644 index 0000000000..7bbdad28d0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/fetch/FetchModeSubselectAndOrderByTest.java @@ -0,0 +1,158 @@ +package org.hibernate.orm.test.fetch; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +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.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +@DomainModel( + annotatedClasses = { + FetchModeSubselectAndOrderByTest.Parent.class, + FetchModeSubselectAndOrderByTest.Child.class + } +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-16202") +public class FetchModeSubselectAndOrderByTest { + + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Parent parent = new Parent( "Filippo" ); + + Child child = new Child( parent, "Luigi" ); + Child child2 = new Child( parent, "And" ); + Child child3 = new Child( parent, "Mary" ); + + session.persist( child ); + session.persist( child2 ); + session.persist( child3 ); + + } + ); + } + + @Test + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + List parents = session.createQuery( "select p from Parent p", Parent.class ).list(); + + assertThat( parents.size() ).isEqualTo( 1 ); + + Parent parent = parents.get( 0 ); + List children = parent.getChildren(); + + assertThat( children.size() ).isEqualTo( 3 ); + assertThat( children.get( 0 ).getName() ).isEqualTo( "And" ); + assertThat( children.get( 1 ).getName() ).isEqualTo( "Luigi" ); + assertThat( children.get( 2 ).getName() ).isEqualTo( "Mary" ); + } + ); + } + + + @Entity(name = "Parent") + public static class Parent { + @Id + @GeneratedValue + private Integer id; + + private String name; + + @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) + @Fetch(value = FetchMode.SUBSELECT) + @OrderBy("name ASC") + private List children = new ArrayList<>(); + + public Parent() { + } + + public Parent(String name) { + this.name = name; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + } + + @Entity(name = "Child") + public static class Child { + @Id + @GeneratedValue + private Integer id; + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "parent_id", nullable = false) + private Parent parent; + + private String name; + + public Child(Parent parent, String name) { + this.parent = parent; + parent.children.add( this ); + this.name = name; + } + + public Child() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Parent getParent() { + return parent; + } + + public void setParent(Parent parent) { + this.parent = parent; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +}