|
| 1 | +package org.hibernate.orm.test.entitygraph; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 8 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 9 | +import org.hibernate.testing.orm.junit.Jpa; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import jakarta.persistence.CascadeType; |
| 14 | +import jakarta.persistence.Column; |
| 15 | +import jakarta.persistence.Entity; |
| 16 | +import jakarta.persistence.EntityGraph; |
| 17 | +import jakarta.persistence.FetchType; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.ManyToOne; |
| 20 | +import jakarta.persistence.MapsId; |
| 21 | +import jakarta.persistence.OneToMany; |
| 22 | +import jakarta.persistence.OneToOne; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 25 | + |
| 26 | +@Jpa( |
| 27 | + annotatedClasses = { |
| 28 | + FindWithEntityGraphTest.Person.class, |
| 29 | + FindWithEntityGraphTest.PersonContact.class |
| 30 | + } |
| 31 | +) |
| 32 | +@JiraKey("HHH-16960") |
| 33 | +public class FindWithEntityGraphTest { |
| 34 | + |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + public void setUp(EntityManagerFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + entityManager -> { |
| 40 | + Person person = new Person( 1L, "test" ); |
| 41 | + |
| 42 | + Person child1 = new Person( 2L, "child1" ); |
| 43 | + Person child2 = new Person( 2L, "child2" ); |
| 44 | + child1.addParent( person ); |
| 45 | + child2.addParent( person ); |
| 46 | + entityManager.persist( person ); |
| 47 | + } |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testFind(EntityManagerFactoryScope scope) { |
| 53 | + scope.inTransaction( |
| 54 | + entityManager -> { |
| 55 | + EntityGraph<?> personGraph = entityManager.createEntityGraph( Person.class ); |
| 56 | + personGraph.addAttributeNodes( "children" ); |
| 57 | + |
| 58 | + Person loadedPerson = entityManager.find( |
| 59 | + Person.class, |
| 60 | + 1l, |
| 61 | + Map.of( "javax.persistence.fetchgraph", personGraph ) |
| 62 | + ); |
| 63 | + |
| 64 | + PersonContact personContact = loadedPerson.getPersonContact(); |
| 65 | + assertNull( personContact ); |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Entity(name = "Person") |
| 71 | + public static class Person { |
| 72 | + |
| 73 | + @Id |
| 74 | + private Long id; |
| 75 | + |
| 76 | + private String name; |
| 77 | + |
| 78 | + @ManyToOne |
| 79 | + private Person parent; |
| 80 | + |
| 81 | + @OneToOne(mappedBy = "person", orphanRemoval = true, cascade = CascadeType.ALL) |
| 82 | + private PersonContact personContact; |
| 83 | + |
| 84 | + @OneToMany(mappedBy = "parent") |
| 85 | + private Set<Person> children = new HashSet<>( 0 ); |
| 86 | + |
| 87 | + public Person() { |
| 88 | + } |
| 89 | + |
| 90 | + public Person(Long id, String name) { |
| 91 | + this.id = id; |
| 92 | + this.name = name; |
| 93 | + } |
| 94 | + |
| 95 | + public Long getId() { |
| 96 | + return id; |
| 97 | + } |
| 98 | + |
| 99 | + public void setId(Long id) { |
| 100 | + this.id = id; |
| 101 | + } |
| 102 | + |
| 103 | + public String getName() { |
| 104 | + return name; |
| 105 | + } |
| 106 | + |
| 107 | + public void setName(String name) { |
| 108 | + this.name = name; |
| 109 | + } |
| 110 | + |
| 111 | + public PersonContact getPersonContact() { |
| 112 | + return personContact; |
| 113 | + } |
| 114 | + |
| 115 | + public void setPersonContact(PersonContact personContact) { |
| 116 | + this.personContact = personContact; |
| 117 | + } |
| 118 | + |
| 119 | + public Person getParent() { |
| 120 | + return parent; |
| 121 | + } |
| 122 | + |
| 123 | + public void setParent(Person parent) { |
| 124 | + this.parent = parent; |
| 125 | + } |
| 126 | + |
| 127 | + public void addParent(Person parent) { |
| 128 | + this.parent = parent; |
| 129 | + parent.getChildren().add( this ); |
| 130 | + } |
| 131 | + |
| 132 | + public Set<Person> getChildren() { |
| 133 | + return children; |
| 134 | + } |
| 135 | + |
| 136 | + public void setChildren(Set<Person> children) { |
| 137 | + this.children = children; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Entity(name = "PersonContact") |
| 142 | + public static class PersonContact { |
| 143 | + @Id |
| 144 | + private Long id; |
| 145 | + |
| 146 | + @Column |
| 147 | + private String address; |
| 148 | + |
| 149 | + public PersonContact() { |
| 150 | + } |
| 151 | + |
| 152 | + public PersonContact(String address) { |
| 153 | + this.address = address; |
| 154 | + } |
| 155 | + |
| 156 | + @OneToOne(optional = false, fetch = FetchType.LAZY) |
| 157 | + @MapsId |
| 158 | + private Person person; |
| 159 | + |
| 160 | + public Long getId() { |
| 161 | + return id; |
| 162 | + } |
| 163 | + |
| 164 | + public void setId(Long id) { |
| 165 | + this.id = id; |
| 166 | + } |
| 167 | + |
| 168 | + public String getAddress() { |
| 169 | + return address; |
| 170 | + } |
| 171 | + |
| 172 | + public void setAddress(String address) { |
| 173 | + this.address = address; |
| 174 | + } |
| 175 | + |
| 176 | + public Person getPerson() { |
| 177 | + return person; |
| 178 | + } |
| 179 | + |
| 180 | + public void setPerson(Person person) { |
| 181 | + this.person = person; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | +} |
0 commit comments