|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa.graphs.queryhint; |
| 6 | + |
| 7 | +import jakarta.persistence.Embeddable; |
| 8 | +import jakarta.persistence.Embedded; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.EntityGraph; |
| 11 | +import jakarta.persistence.FetchType; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.ManyToOne; |
| 14 | +import jakarta.persistence.OneToMany; |
| 15 | +import jakarta.persistence.Subgraph; |
| 16 | +import org.hibernate.Hibernate; |
| 17 | +import org.hibernate.Session; |
| 18 | +import org.hibernate.query.Query; |
| 19 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 20 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 31 | +import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOAD_GRAPH; |
| 32 | + |
| 33 | +@JiraKey("HHH-14349") |
| 34 | +@DomainModel( |
| 35 | + annotatedClasses = { |
| 36 | + EmbeddableQueryHintEntityGraphTest.Parent.class, |
| 37 | + EmbeddableQueryHintEntityGraphTest.Child.class, |
| 38 | + EmbeddableQueryHintEntityGraphTest.AnotherChild.class |
| 39 | + } |
| 40 | +) |
| 41 | +@SessionFactory |
| 42 | +public class EmbeddableQueryHintEntityGraphTest { |
| 43 | + |
| 44 | + private final static Long PARENT_ID = 1L; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + public void createTestData(SessionFactoryScope scope) { |
| 48 | + scope.inTransaction( |
| 49 | + session -> { |
| 50 | + Parent parent = new Parent( PARENT_ID, "parent" ); |
| 51 | + session.persist( parent ); |
| 52 | + |
| 53 | + Child child1 = new Child( 1L, "child1", parent ); |
| 54 | + session.persist( child1 ); |
| 55 | + |
| 56 | + Child child2 = new Child( 2L, "child2", parent ); |
| 57 | + session.persist( child2 ); |
| 58 | + |
| 59 | + AnotherChild anotherChild = new AnotherChild( 1L, "child3" ); |
| 60 | + session.persist( anotherChild ); |
| 61 | + |
| 62 | + AnEmbeddable embedded = new AnEmbeddable( new AnotherEmbeddable( anotherChild ), child1 ); |
| 63 | + embedded.addChild( child2 ); |
| 64 | + parent.setEmbedded( embedded ); |
| 65 | + } |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterEach |
| 70 | + public void cleanUpTestData(SessionFactoryScope scope) { |
| 71 | + scope.inTransaction( |
| 72 | + session -> |
| 73 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects() |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testFetchingFromManagedEntityEmbeddedBasicFieldLoadGraph(SessionFactoryScope scope) { |
| 79 | + scope.inTransaction( |
| 80 | + session -> { |
| 81 | + final Parent parent = session.find( Parent.class, PARENT_ID ); |
| 82 | + assertIsInitialized( parent ); |
| 83 | + assertIsNotInitialized( parent.getEmbedded().getChild() ); |
| 84 | + |
| 85 | + final EntityGraph<Parent> entityGraph = session.createEntityGraph( Parent.class ); |
| 86 | + Subgraph<Object> subGraph = entityGraph.addSubgraph( "embedded" ); |
| 87 | + subGraph.addAttributeNode( "child" ); |
| 88 | + |
| 89 | + Parent p = selectParent( entityGraph, session ); |
| 90 | + assertIsInitialized( p ); |
| 91 | + assertIsInitialized( p.getEmbedded().getChild() ); |
| 92 | + } |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testFetchingFromManagedEntityEmbeddedCollectionFieldLoadGraph(SessionFactoryScope scope) { |
| 98 | + scope.inTransaction( |
| 99 | + session -> { |
| 100 | + final Parent parent = session.find( Parent.class, PARENT_ID ); |
| 101 | + assertIsInitialized( parent ); |
| 102 | + assertIsNotInitialized( parent.getEmbedded().getChildren() ); |
| 103 | + |
| 104 | + final EntityGraph<Parent> entityGraph = session.createEntityGraph( Parent.class ); |
| 105 | + Subgraph<Object> subGraph = entityGraph.addSubgraph( "embedded" ); |
| 106 | + subGraph.addAttributeNode( "children" ); |
| 107 | + |
| 108 | + Parent p = selectParent( entityGraph, session ); |
| 109 | + assertIsInitialized( p ); |
| 110 | + assertIsInitialized( p.getEmbedded().getChildren() ); |
| 111 | + } |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testFetchingFromManagedEntityNestedEmbeddedBasicFieldLoadGraph(SessionFactoryScope scope) { |
| 117 | + scope.inTransaction( |
| 118 | + session -> { |
| 119 | + final Parent parent = session.get( Parent.class, PARENT_ID ); |
| 120 | + assertIsInitialized( parent ); |
| 121 | + assertIsNotInitialized( parent.getEmbedded().getNestedEmbedded().getAnotherChild() ); |
| 122 | + |
| 123 | + final EntityGraph<Parent> entityGraph = session.createEntityGraph( Parent.class ); |
| 124 | + Subgraph<Object> subGraph = entityGraph.addSubgraph( "embedded" ); |
| 125 | + Subgraph<Object> nestedSubgraph = subGraph.addSubgraph( "nestedEmbedded" ); |
| 126 | + nestedSubgraph.addAttributeNode( "anotherChild" ); |
| 127 | + |
| 128 | + Parent p = selectParent( entityGraph, session ); |
| 129 | + assertIsInitialized( p ); |
| 130 | + assertIsInitialized( p.getEmbedded().getNestedEmbedded().getAnotherChild() ); |
| 131 | + } |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + static Parent selectParent(EntityGraph<Parent> entityGraph, Session session) { |
| 136 | + final Query<Parent> query = session.createQuery( "select p from Parent p", Parent.class ); |
| 137 | + return query.setHint( HINT_SPEC_LOAD_GRAPH, entityGraph ).uniqueResult(); |
| 138 | + } |
| 139 | + |
| 140 | + static void assertIsInitialized(Object entity) { |
| 141 | + assertThat( Hibernate.isInitialized( entity ) ).isTrue(); |
| 142 | + } |
| 143 | + |
| 144 | + static void assertIsNotInitialized(Object entity) { |
| 145 | + assertThat( Hibernate.isInitialized( entity ) ).isFalse(); |
| 146 | + } |
| 147 | + |
| 148 | + @Entity(name = "Parent") |
| 149 | + public static class Parent { |
| 150 | + @Id |
| 151 | + private Long id; |
| 152 | + |
| 153 | + private String name; |
| 154 | + |
| 155 | + @Embedded |
| 156 | + private AnEmbeddable embedded; |
| 157 | + |
| 158 | + public Parent() { |
| 159 | + } |
| 160 | + |
| 161 | + public Parent(Long id, String name) { |
| 162 | + this.id = id; |
| 163 | + this.name = name; |
| 164 | + } |
| 165 | + |
| 166 | + public Long getId() { |
| 167 | + return id; |
| 168 | + } |
| 169 | + |
| 170 | + public String getName() { |
| 171 | + return name; |
| 172 | + } |
| 173 | + |
| 174 | + public AnEmbeddable getEmbedded() { |
| 175 | + return embedded; |
| 176 | + } |
| 177 | + |
| 178 | + public void setEmbedded(AnEmbeddable embedded) { |
| 179 | + this.embedded = embedded; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Embeddable |
| 184 | + public static class AnEmbeddable { |
| 185 | + |
| 186 | + @Embedded |
| 187 | + private AnotherEmbeddable nestedEmbedded; |
| 188 | + |
| 189 | + @ManyToOne(fetch = FetchType.LAZY) |
| 190 | + private Child child; |
| 191 | + |
| 192 | + @OneToMany(fetch = FetchType.LAZY) |
| 193 | + private List<Child> children = new ArrayList<>(); |
| 194 | + |
| 195 | + public AnEmbeddable() { |
| 196 | + } |
| 197 | + |
| 198 | + public AnEmbeddable(AnotherEmbeddable nestedEmbeddedObject, Child child) { |
| 199 | + this.nestedEmbedded = nestedEmbeddedObject; |
| 200 | + this.child = child; |
| 201 | + } |
| 202 | + |
| 203 | + public AnotherEmbeddable getNestedEmbedded() { |
| 204 | + return nestedEmbedded; |
| 205 | + } |
| 206 | + |
| 207 | + public Child getChild() { |
| 208 | + return child; |
| 209 | + } |
| 210 | + |
| 211 | + public List<Child> getChildren() { |
| 212 | + return children; |
| 213 | + } |
| 214 | + |
| 215 | + public void addChild(Child child) { |
| 216 | + this.children.add( child ); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + @Embeddable |
| 221 | + public static class AnotherEmbeddable { |
| 222 | + @ManyToOne(fetch = FetchType.LAZY) |
| 223 | + private AnotherChild anotherChild; |
| 224 | + |
| 225 | + public AnotherEmbeddable() { |
| 226 | + } |
| 227 | + |
| 228 | + public AnotherEmbeddable(AnotherChild anotherChild) { |
| 229 | + this.anotherChild = anotherChild; |
| 230 | + } |
| 231 | + |
| 232 | + public AnotherChild getAnotherChild() { |
| 233 | + return anotherChild; |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + @Entity(name = "Child") |
| 238 | + public static class Child { |
| 239 | + @Id |
| 240 | + private Long id; |
| 241 | + |
| 242 | + private String name; |
| 243 | + |
| 244 | + @ManyToOne(fetch = FetchType.LAZY) |
| 245 | + private Parent parent; |
| 246 | + |
| 247 | + public Child() { |
| 248 | + } |
| 249 | + |
| 250 | + public Child(Long id, String name) { |
| 251 | + this.id = id; |
| 252 | + this.name = name; |
| 253 | + } |
| 254 | + |
| 255 | + public Child(Long id, String name, Parent parent) { |
| 256 | + this.id = id; |
| 257 | + this.name = name; |
| 258 | + this.parent = parent; |
| 259 | + } |
| 260 | + |
| 261 | + public Long getId() { |
| 262 | + return id; |
| 263 | + } |
| 264 | + |
| 265 | + public String getName() { |
| 266 | + return name; |
| 267 | + } |
| 268 | + |
| 269 | + public Parent getParent() { |
| 270 | + return parent; |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + @Entity(name = "AnotherChild") |
| 275 | + public static class AnotherChild { |
| 276 | + @Id |
| 277 | + private Long id; |
| 278 | + |
| 279 | + private String name; |
| 280 | + |
| 281 | + public AnotherChild() { |
| 282 | + } |
| 283 | + |
| 284 | + public AnotherChild(Long id, String name) { |
| 285 | + this.id = id; |
| 286 | + this.name = name; |
| 287 | + } |
| 288 | + |
| 289 | + public Long getId() { |
| 290 | + return id; |
| 291 | + } |
| 292 | + |
| 293 | + public String getName() { |
| 294 | + return name; |
| 295 | + } |
| 296 | + } |
| 297 | +} |
0 commit comments