|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.loading.multiLoad; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.FetchType; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.ManyToOne; |
| 11 | +import org.hibernate.Hibernate; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +@SessionFactory |
| 25 | +@DomainModel(annotatedClasses = {GetMultipleEntityGraphTest.Record.class, GetMultipleEntityGraphTest.Owner.class}) |
| 26 | +public class GetMultipleEntityGraphTest { |
| 27 | + @Test void test(SessionFactoryScope scope) { |
| 28 | + var graph = scope.getSessionFactory().createEntityGraph(Record.class); |
| 29 | + graph.addAttributeNode("owner"); |
| 30 | + scope.inStatelessTransaction(s-> { |
| 31 | + Owner gavin = new Owner("gavin"); |
| 32 | + s.insert(gavin); |
| 33 | + s.insert(new Record(123L,gavin,"hello earth")); |
| 34 | + s.insert(new Record(456L,gavin,"hello mars")); |
| 35 | + }); |
| 36 | + scope.inStatelessTransaction(s-> { |
| 37 | + List<Record> all = s.getMultiple(Record.class, List.of(456L, 123L, 2L)); |
| 38 | + assertEquals("hello mars",all.get(0).message); |
| 39 | + assertEquals("hello earth",all.get(1).message); |
| 40 | + assertNull(all.get(2)); |
| 41 | + assertFalse(Hibernate.isInitialized(all.get(0).owner)); |
| 42 | + assertFalse(Hibernate.isInitialized(all.get(1).owner)); |
| 43 | + }); |
| 44 | + scope.inStatelessTransaction(s-> { |
| 45 | + List<Record> all = s.getMultiple(graph, List.of(456L, 123L)); |
| 46 | + assertEquals("hello mars",all.get(0).message); |
| 47 | + assertEquals("hello earth",all.get(1).message); |
| 48 | + assertTrue(Hibernate.isInitialized(all.get(0).owner)); |
| 49 | + assertTrue(Hibernate.isInitialized(all.get(1).owner)); |
| 50 | + }); |
| 51 | + } |
| 52 | + @Entity |
| 53 | + static class Record { |
| 54 | + @Id Long id; |
| 55 | + String message; |
| 56 | + |
| 57 | + @ManyToOne(fetch = FetchType.LAZY) |
| 58 | + Owner owner; |
| 59 | + |
| 60 | + Record(Long id, Owner owner, String message) { |
| 61 | + this.id = id; |
| 62 | + this.owner = owner; |
| 63 | + this.message = message; |
| 64 | + } |
| 65 | + |
| 66 | + Record() { |
| 67 | + } |
| 68 | + } |
| 69 | + @Entity |
| 70 | + static class Owner { |
| 71 | + @Id String name; |
| 72 | + |
| 73 | + Owner(String name) { |
| 74 | + this.name = name; |
| 75 | + } |
| 76 | + |
| 77 | + Owner() { |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments