From a73bc08c78b203be5a19806a9004ae3a20552360 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 22 Jan 2024 16:33:26 +0100 Subject: [PATCH] HHH-16960 Add test for issue --- .../entitygraph/FindWithEntityGraphTest.java | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/FindWithEntityGraphTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/FindWithEntityGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/FindWithEntityGraphTest.java new file mode 100644 index 0000000000..c0cec22aed --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/FindWithEntityGraphTest.java @@ -0,0 +1,186 @@ +package org.hibernate.orm.test.entitygraph; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityGraph; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.MapsId; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; + +import static org.junit.jupiter.api.Assertions.assertNull; + +@Jpa( + annotatedClasses = { + FindWithEntityGraphTest.Person.class, + FindWithEntityGraphTest.PersonContact.class + } +) +@JiraKey("HHH-16960") +public class FindWithEntityGraphTest { + + + @BeforeAll + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Person person = new Person( 1L, "test" ); + + Person child1 = new Person( 2L, "child1" ); + Person child2 = new Person( 2L, "child2" ); + child1.addParent( person ); + child2.addParent( person ); + entityManager.persist( person ); + } + ); + } + + @Test + public void testFind(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + EntityGraph personGraph = entityManager.createEntityGraph( Person.class ); + personGraph.addAttributeNodes( "children" ); + + Person loadedPerson = entityManager.find( + Person.class, + 1l, + Map.of( "javax.persistence.fetchgraph", personGraph ) + ); + + PersonContact personContact = loadedPerson.getPersonContact(); + assertNull( personContact ); + } + ); + } + + @Entity(name = "Person") + public static class Person { + + @Id + private Long id; + + private String name; + + @ManyToOne + private Person parent; + + @OneToOne(mappedBy = "person", orphanRemoval = true, cascade = CascadeType.ALL) + private PersonContact personContact; + + @OneToMany(mappedBy = "parent") + private Set children = new HashSet<>( 0 ); + + public Person() { + } + + public Person(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public PersonContact getPersonContact() { + return personContact; + } + + public void setPersonContact(PersonContact personContact) { + this.personContact = personContact; + } + + public Person getParent() { + return parent; + } + + public void setParent(Person parent) { + this.parent = parent; + } + + public void addParent(Person parent) { + this.parent = parent; + parent.getChildren().add( this ); + } + + public Set getChildren() { + return children; + } + + public void setChildren(Set children) { + this.children = children; + } + } + + @Entity(name = "PersonContact") + public static class PersonContact { + @Id + private Long id; + + @Column + private String address; + + public PersonContact() { + } + + public PersonContact(String address) { + this.address = address; + } + + @OneToOne(optional = false, fetch = FetchType.LAZY) + @MapsId + private Person person; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Person getPerson() { + return person; + } + + public void setPerson(Person person) { + this.person = person; + } + } + + +}