|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.entitygraph; |
| 8 | + |
| 9 | +import java.util.LinkedHashSet; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +import org.hibernate.Hibernate; |
| 14 | +import org.hibernate.graph.spi.RootGraphImplementor; |
| 15 | +import org.hibernate.jpa.AvailableHints; |
| 16 | + |
| 17 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 18 | +import org.hibernate.testing.orm.junit.Jira; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.BeforeAll; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import jakarta.persistence.CascadeType; |
| 26 | +import jakarta.persistence.Entity; |
| 27 | +import jakarta.persistence.FetchType; |
| 28 | +import jakarta.persistence.Id; |
| 29 | +import jakarta.persistence.JoinColumn; |
| 30 | +import jakarta.persistence.ManyToOne; |
| 31 | +import jakarta.persistence.NamedAttributeNode; |
| 32 | +import jakarta.persistence.NamedEntityGraph; |
| 33 | +import jakarta.persistence.NamedEntityGraphs; |
| 34 | +import jakarta.persistence.NamedSubgraph; |
| 35 | +import jakarta.persistence.OneToMany; |
| 36 | +import jakarta.persistence.OrderBy; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Marco Belladelli |
| 42 | + */ |
| 43 | +@DomainModel( annotatedClasses = { |
| 44 | + FindGraphCollectionOrderByTest.Level1.class, |
| 45 | + FindGraphCollectionOrderByTest.Level2.class, |
| 46 | + FindGraphCollectionOrderByTest.Level3.class, |
| 47 | +} ) |
| 48 | +@SessionFactory |
| 49 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18436" ) |
| 50 | +public class FindGraphCollectionOrderByTest { |
| 51 | + @Test |
| 52 | + public void testLoadGraphFind(SessionFactoryScope scope) { |
| 53 | + executeTest( scope, AvailableHints.HINT_SPEC_LOAD_GRAPH, true ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testLoadGraphQuery(SessionFactoryScope scope) { |
| 58 | + executeTest( scope, AvailableHints.HINT_SPEC_LOAD_GRAPH, false ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testFetchGraphFind(SessionFactoryScope scope) { |
| 63 | + executeTest( scope, AvailableHints.HINT_SPEC_FETCH_GRAPH, true ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testFetchGraphQuery(SessionFactoryScope scope) { |
| 68 | + executeTest( scope, AvailableHints.HINT_SPEC_FETCH_GRAPH, false ); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private void executeTest(SessionFactoryScope scope, String hint, boolean find) { |
| 73 | + scope.inTransaction( session -> { |
| 74 | + final RootGraphImplementor<?> graph = session.getEntityGraph( "level1_loadAll" ); |
| 75 | + final Level1 root = find ? session.find( Level1.class, 1L, Map.of( hint, graph ) ) : |
| 76 | + session.createQuery( "from Level1 where id = :id", Level1.class ) |
| 77 | + .setParameter( "id", 1L ) |
| 78 | + .setHint( hint, graph ) |
| 79 | + .getSingleResult(); |
| 80 | + |
| 81 | + assertThat( root.getChildren() ).matches( Hibernate::isInitialized ).hasSize( 3 ); |
| 82 | + long i = 1; |
| 83 | + for ( final Level2 child : root.getChildren() ) { |
| 84 | + if ( i == 2 ) { |
| 85 | + assertThat( child.getChildren() ).matches( Hibernate::isInitialized ).hasSize( 1 ); |
| 86 | + } |
| 87 | + assertThat( child.getId() ).as( "Children not in expected order" ).isEqualTo( i++ ); |
| 88 | + } |
| 89 | + } ); |
| 90 | + } |
| 91 | + |
| 92 | + @BeforeAll |
| 93 | + public void setUp(SessionFactoryScope scope) { |
| 94 | + scope.inTransaction( session -> { |
| 95 | + final Level1 root = new Level1( 1L ); |
| 96 | + new Level2( root, 1L ); |
| 97 | + final Level2 child2 = new Level2( root, 2L ); |
| 98 | + new Level2( root, 3L ); |
| 99 | + new Level3( child2, 1L ); |
| 100 | + session.persist( root ); |
| 101 | + } ); |
| 102 | + } |
| 103 | + |
| 104 | + @AfterAll |
| 105 | + public void tearDown(SessionFactoryScope scope) { |
| 106 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 107 | + } |
| 108 | + |
| 109 | + @Entity( name = "Level1" ) |
| 110 | + @NamedEntityGraphs( { |
| 111 | + @NamedEntityGraph( |
| 112 | + name = "level1_loadAll", |
| 113 | + attributeNodes = { |
| 114 | + @NamedAttributeNode( value = "children", subgraph = "subgraph.children" ) |
| 115 | + }, |
| 116 | + subgraphs = { |
| 117 | + @NamedSubgraph( |
| 118 | + name = "subgraph.children", |
| 119 | + attributeNodes = { |
| 120 | + @NamedAttributeNode( value = "children" ) |
| 121 | + } |
| 122 | + ) |
| 123 | + } |
| 124 | + ) |
| 125 | + } ) |
| 126 | + static class Level1 { |
| 127 | + @Id |
| 128 | + private Long id; |
| 129 | + |
| 130 | + @OneToMany( fetch = FetchType.LAZY, |
| 131 | + mappedBy = "parent", |
| 132 | + cascade = CascadeType.PERSIST ) |
| 133 | + @OrderBy( "id" ) |
| 134 | + private Set<Level2> children = new LinkedHashSet<>(); |
| 135 | + |
| 136 | + public Level1() { |
| 137 | + } |
| 138 | + |
| 139 | + public Level1(Long id) { |
| 140 | + this.id = id; |
| 141 | + } |
| 142 | + |
| 143 | + public Long getId() { |
| 144 | + return id; |
| 145 | + } |
| 146 | + |
| 147 | + public void setId(Long id) { |
| 148 | + this.id = id; |
| 149 | + } |
| 150 | + |
| 151 | + public Set<Level2> getChildren() { |
| 152 | + return children; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Entity( name = "Level2" ) |
| 157 | + static class Level2 { |
| 158 | + @Id |
| 159 | + Long id; |
| 160 | + |
| 161 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 162 | + @JoinColumn( name = "parent_id" ) |
| 163 | + private Level1 parent; |
| 164 | + |
| 165 | + @OneToMany( fetch = FetchType.LAZY, |
| 166 | + mappedBy = "parent", |
| 167 | + cascade = CascadeType.PERSIST ) |
| 168 | + @OrderBy( "id" ) |
| 169 | + private Set<Level3> children = new LinkedHashSet<>(); |
| 170 | + |
| 171 | + public Level2() { |
| 172 | + } |
| 173 | + |
| 174 | + public Level2(Level1 parent, Long id) { |
| 175 | + this.parent = parent; |
| 176 | + this.id = id; |
| 177 | + parent.getChildren().add( this ); |
| 178 | + } |
| 179 | + |
| 180 | + public Long getId() { |
| 181 | + return id; |
| 182 | + } |
| 183 | + |
| 184 | + public void setId(Long id) { |
| 185 | + this.id = id; |
| 186 | + } |
| 187 | + |
| 188 | + public Level1 getParent() { |
| 189 | + return parent; |
| 190 | + } |
| 191 | + |
| 192 | + public void setParent(Level1 parent) { |
| 193 | + this.parent = parent; |
| 194 | + } |
| 195 | + |
| 196 | + public Set<Level3> getChildren() { |
| 197 | + return children; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Entity( name = "Level3" ) |
| 202 | + static class Level3 { |
| 203 | + @Id |
| 204 | + Long id; |
| 205 | + |
| 206 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 207 | + @JoinColumn( name = "parent_id" ) |
| 208 | + private Level2 parent; |
| 209 | + |
| 210 | + public Level3() { |
| 211 | + } |
| 212 | + |
| 213 | + public Level3(Level2 parent, Long id) { |
| 214 | + this.parent = parent; |
| 215 | + this.id = id; |
| 216 | + parent.getChildren().add( this ); |
| 217 | + } |
| 218 | + |
| 219 | + public Long getId() { |
| 220 | + return id; |
| 221 | + } |
| 222 | + |
| 223 | + public void setId(Long id) { |
| 224 | + this.id = id; |
| 225 | + } |
| 226 | + |
| 227 | + public Level2 getParent() { |
| 228 | + return parent; |
| 229 | + } |
| 230 | + |
| 231 | + public void setParent(Level2 parent) { |
| 232 | + this.parent = parent; |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments