|
| 1 | +package org.hibernate.orm.test.entitygraph; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.hibernate.annotations.OnDelete; |
| 9 | +import org.hibernate.annotations.OnDeleteAction; |
| 10 | + |
| 11 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import jakarta.persistence.CascadeType; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.EntityGraph; |
| 20 | +import jakarta.persistence.FetchType; |
| 21 | +import jakarta.persistence.GeneratedValue; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.JoinColumn; |
| 24 | +import jakarta.persistence.ManyToOne; |
| 25 | +import jakarta.persistence.OneToMany; |
| 26 | + |
| 27 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 28 | + |
| 29 | +@Jpa( |
| 30 | + annotatedClasses = { |
| 31 | + EntityGraphAttributeNodesTest.Human.class, |
| 32 | + EntityGraphAttributeNodesTest.House.class, |
| 33 | + EntityGraphAttributeNodesTest.Address.class, |
| 34 | + } |
| 35 | +) |
| 36 | +@JiraKey("HHH-16885") |
| 37 | +public class EntityGraphAttributeNodesTest { |
| 38 | + |
| 39 | + private final static Integer HUMAN_ID = 1; |
| 40 | + |
| 41 | + @BeforeAll |
| 42 | + public void setUp(EntityManagerFactoryScope scope) { |
| 43 | + scope.inTransaction( |
| 44 | + entityManager -> { |
| 45 | + Human human = new Human( HUMAN_ID ); |
| 46 | + entityManager.persist( human ); |
| 47 | + } |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testIt(EntityManagerFactoryScope scope) { |
| 53 | + |
| 54 | + scope.inEntityManager( |
| 55 | + entityManager -> { |
| 56 | + EntityGraph<?> entityGraph = entityManager.createEntityGraph( Human.class ); |
| 57 | + entityGraph.addSubgraph( "houses" ).addAttributeNodes( "address" ); |
| 58 | + |
| 59 | + List results = entityManager.createQuery( "SELECT h FROM Human h WHERE h.id = ?1" ) |
| 60 | + .setParameter( 1, HUMAN_ID ) |
| 61 | + .setHint( "javax.persistence.fetchgraph", entityGraph ) |
| 62 | + .getResultList(); |
| 63 | + |
| 64 | + assertThat( results.size() ).isEqualTo( 1 ); |
| 65 | + assertThat( results.get( 0 ) ).isNotNull(); |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Entity(name = "Human") |
| 71 | + public static class Human { |
| 72 | + @Id |
| 73 | + private Integer id; |
| 74 | + |
| 75 | + private String name; |
| 76 | + |
| 77 | + @OneToMany(mappedBy = "human", cascade = CascadeType.ALL) |
| 78 | + @OnDelete(action = OnDeleteAction.CASCADE) |
| 79 | + private Collection<House> houses; |
| 80 | + |
| 81 | + public Human() { |
| 82 | + } |
| 83 | + |
| 84 | + public Human(Integer id) { |
| 85 | + this.id = id; |
| 86 | + this.houses = new ArrayList<>(); |
| 87 | + } |
| 88 | + |
| 89 | + public Human(Integer id, String name, Collection<House> houses) { |
| 90 | + this.id = id; |
| 91 | + this.name = name; |
| 92 | + this.houses = houses; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Entity(name = "House") |
| 97 | + public static class House { |
| 98 | + @Id |
| 99 | + @ManyToOne(fetch = FetchType.LAZY) |
| 100 | + @JoinColumn(name = "human_fk", nullable = false, updatable = false) |
| 101 | + private Human human; |
| 102 | + |
| 103 | + @Id |
| 104 | + @ManyToOne(fetch = FetchType.LAZY) |
| 105 | + @JoinColumn(name = "address_fk", nullable = false, updatable = false) |
| 106 | + private Address address; |
| 107 | + |
| 108 | + private String name; |
| 109 | + } |
| 110 | + |
| 111 | + @Entity(name = "Address") |
| 112 | + public static class Address { |
| 113 | + |
| 114 | + @Id |
| 115 | + @GeneratedValue |
| 116 | + private BigInteger id; |
| 117 | + |
| 118 | + private String street; |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments