|
| 1 | +package org.hibernate.orm.test.entitygraph; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import org.hibernate.Hibernate; |
| 6 | +import org.hibernate.Session; |
| 7 | + |
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.Jira; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 12 | +import org.junit.jupiter.api.AfterAll; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Embeddable; |
| 17 | +import jakarta.persistence.Embedded; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.EntityGraph; |
| 20 | +import jakarta.persistence.FetchType; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.ManyToOne; |
| 23 | + |
| 24 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 25 | +import static org.hibernate.jpa.SpecHints.HINT_SPEC_FETCH_GRAPH; |
| 26 | +import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOAD_GRAPH; |
| 27 | + |
| 28 | +@DomainModel( annotatedClasses = { |
| 29 | + EntityGraphEmbeddedAttributesTest.TrackedProduct.class, |
| 30 | + EntityGraphEmbeddedAttributesTest.Tracking.class, |
| 31 | + EntityGraphEmbeddedAttributesTest.UserForTracking.class, |
| 32 | +} ) |
| 33 | +@SessionFactory |
| 34 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18391" ) |
| 35 | +public class EntityGraphEmbeddedAttributesTest { |
| 36 | + private static final int TRACKED_PRODUCT_ID = 1; |
| 37 | + |
| 38 | + @Test |
| 39 | + void testFetchGraphFind(SessionFactoryScope scope) { |
| 40 | + executeTest( scope, HINT_SPEC_FETCH_GRAPH, true ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testFetchGraphQuery(SessionFactoryScope scope) { |
| 45 | + executeTest( scope, HINT_SPEC_FETCH_GRAPH, false ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testLoadGraphFind(SessionFactoryScope scope) { |
| 50 | + executeTest( scope, HINT_SPEC_LOAD_GRAPH, true ); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testLoadGraphQuery(SessionFactoryScope scope) { |
| 55 | + executeTest( scope, HINT_SPEC_LOAD_GRAPH, false ); |
| 56 | + } |
| 57 | + |
| 58 | + static void executeTest(SessionFactoryScope scope, String hint, boolean find) { |
| 59 | + scope.inTransaction( session -> { |
| 60 | + final EntityGraph<TrackedProduct> graph = createGraph( session ); |
| 61 | + final TrackedProduct product = find ? session.find( |
| 62 | + TrackedProduct.class, |
| 63 | + TRACKED_PRODUCT_ID, |
| 64 | + Map.of( hint, graph ) |
| 65 | + ) : session.createQuery( |
| 66 | + "from TrackedProduct where id = :id", |
| 67 | + TrackedProduct.class |
| 68 | + ).setParameter( "id", TRACKED_PRODUCT_ID ).setHint( hint, graph ).getSingleResult(); |
| 69 | + assertThat( Hibernate.isInitialized( product.tracking.creator ) ).isTrue(); |
| 70 | + assertThat( Hibernate.isInitialized( product.tracking.modifier ) ).isFalse(); |
| 71 | + } ); |
| 72 | + } |
| 73 | + |
| 74 | + static EntityGraph<TrackedProduct> createGraph(Session session) { |
| 75 | + final EntityGraph<TrackedProduct> graph = session.createEntityGraph( TrackedProduct.class ); |
| 76 | + graph.addSubgraph( "tracking" ).addAttributeNodes( "creator" ); |
| 77 | + return graph; |
| 78 | + } |
| 79 | + |
| 80 | + @BeforeAll |
| 81 | + void setUp(SessionFactoryScope scope) { |
| 82 | + scope.inTransaction( session -> { |
| 83 | + final UserForTracking creator = new UserForTracking( 1, "foo" ); |
| 84 | + session.persist( creator ); |
| 85 | + final UserForTracking modifier = new UserForTracking( 2, "bar" ); |
| 86 | + session.persist( modifier ); |
| 87 | + final TrackedProduct product = new TrackedProduct( TRACKED_PRODUCT_ID, new Tracking( creator, modifier ) ); |
| 88 | + session.persist( product ); |
| 89 | + } ); |
| 90 | + } |
| 91 | + |
| 92 | + @AfterAll |
| 93 | + public void tearDown(SessionFactoryScope scope) { |
| 94 | + scope.inTransaction( session -> session.getSessionFactory().getSchemaManager().truncateMappedObjects() ); |
| 95 | + } |
| 96 | + |
| 97 | + @Entity( name = "TrackedProduct" ) |
| 98 | + static class TrackedProduct { |
| 99 | + @Id |
| 100 | + private Integer id; |
| 101 | + |
| 102 | + @Embedded |
| 103 | + private Tracking tracking; |
| 104 | + |
| 105 | + public TrackedProduct() { |
| 106 | + } |
| 107 | + |
| 108 | + public TrackedProduct(Integer id, Tracking tracking) { |
| 109 | + this.id = id; |
| 110 | + this.tracking = tracking; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Embeddable |
| 115 | + static class Tracking { |
| 116 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 117 | + private UserForTracking creator; |
| 118 | + |
| 119 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 120 | + private UserForTracking modifier; |
| 121 | + |
| 122 | + public Tracking() { |
| 123 | + } |
| 124 | + |
| 125 | + public Tracking(UserForTracking creator, UserForTracking modifier) { |
| 126 | + this.creator = creator; |
| 127 | + this.modifier = modifier; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Entity( name = "UserForTracking" ) |
| 132 | + static class UserForTracking { |
| 133 | + @Id |
| 134 | + private Integer id; |
| 135 | + |
| 136 | + private String login; |
| 137 | + |
| 138 | + public UserForTracking() { |
| 139 | + } |
| 140 | + |
| 141 | + public UserForTracking(Integer id, String login) { |
| 142 | + this.id = id; |
| 143 | + this.login = login; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments