|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.mapping.identifier; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.Hibernate; |
| 12 | + |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.Jira; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import jakarta.persistence.Embeddable; |
| 22 | +import jakarta.persistence.EmbeddedId; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.FetchType; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.ManyToOne; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Marco Belladelli |
| 32 | + */ |
| 33 | +@DomainModel( annotatedClasses = { |
| 34 | + EmbeddedIdManyToOneFetchTest.EntityC.class, |
| 35 | + EmbeddedIdManyToOneFetchTest.EntityA.class, |
| 36 | + EmbeddedIdManyToOneFetchTest.EntityB.class, |
| 37 | + EmbeddedIdManyToOneFetchTest.AnotherEntity.class, |
| 38 | +} ) |
| 39 | +@SessionFactory |
| 40 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18330" ) |
| 41 | +public class EmbeddedIdManyToOneFetchTest { |
| 42 | + @Test |
| 43 | + public void testInnerJoinFetch(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
| 45 | + final EntityA result = session.createQuery( |
| 46 | + "select a from EntityA a " + |
| 47 | + "join fetch a.id.entityB b " + |
| 48 | + "join fetch b.id.entityC c", EntityA.class ).getSingleResult(); |
| 49 | + final EntityB entityB = result.id.entityB; |
| 50 | + assertThat( entityB ).satisfies( Hibernate::isInitialized ); |
| 51 | + assertThat( entityB.id.entityC ).satisfies( Hibernate::isInitialized ); |
| 52 | + } ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testLeftJoinFetch(SessionFactoryScope scope) { |
| 57 | + scope.inTransaction( session -> { |
| 58 | + final EntityA result = session.createQuery( |
| 59 | + "select a from EntityA a " + |
| 60 | + "left join fetch a.id.entityB b " + |
| 61 | + "left join fetch b.id.entityC c", EntityA.class ).getSingleResult(); |
| 62 | + final EntityB entityB = result.id.entityB; |
| 63 | + assertThat( entityB ).satisfies( Hibernate::isInitialized ); |
| 64 | + assertThat( entityB.id.entityC ).satisfies( Hibernate::isInitialized ); |
| 65 | + } ); |
| 66 | + } |
| 67 | + |
| 68 | + @BeforeAll |
| 69 | + public void setUp(SessionFactoryScope scope) { |
| 70 | + scope.inTransaction( session -> { |
| 71 | + final EntityC entityC = new EntityC( "entity_c" ); |
| 72 | + session.persist( entityC ); |
| 73 | + final EntityB entityB = new EntityB( new EntityBId( entityC, "entity_b" ) ); |
| 74 | + session.persist( entityB ); |
| 75 | + final AnotherEntity anotherEntity = new AnotherEntity( 1L ); |
| 76 | + session.persist( anotherEntity ); |
| 77 | + session.persist( new EntityA( new EntityAId( entityB, anotherEntity ) ) ); |
| 78 | + } ); |
| 79 | + } |
| 80 | + |
| 81 | + @AfterAll |
| 82 | + public void tearDown(SessionFactoryScope scope) { |
| 83 | + scope.inTransaction( session -> session.getSessionFactory().getSchemaManager().truncateMappedObjects() ); |
| 84 | + } |
| 85 | + |
| 86 | + @Entity( name = "EntityA" ) |
| 87 | + static class EntityA { |
| 88 | + @EmbeddedId |
| 89 | + private EntityAId id; |
| 90 | + |
| 91 | + public EntityA() { |
| 92 | + } |
| 93 | + |
| 94 | + public EntityA(EntityAId id) { |
| 95 | + this.id = id; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Embeddable |
| 100 | + static class EntityAId { |
| 101 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 102 | + private EntityB entityB; |
| 103 | + |
| 104 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 105 | + private AnotherEntity anotherEntity; |
| 106 | + |
| 107 | + public EntityAId() { |
| 108 | + } |
| 109 | + |
| 110 | + public EntityAId(EntityB entityB, AnotherEntity anotherEntity) { |
| 111 | + this.entityB = entityB; |
| 112 | + this.anotherEntity = anotherEntity; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Entity( name = "EntityB" ) |
| 117 | + static class EntityB { |
| 118 | + @EmbeddedId |
| 119 | + private EntityBId id; |
| 120 | + |
| 121 | + public EntityB() { |
| 122 | + } |
| 123 | + |
| 124 | + public EntityB(EntityBId id) { |
| 125 | + this.id = id; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Embeddable |
| 130 | + static class EntityBId { |
| 131 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 132 | + private EntityC entityC; |
| 133 | + |
| 134 | + private String name; |
| 135 | + |
| 136 | + public EntityBId() { |
| 137 | + } |
| 138 | + |
| 139 | + public EntityBId(EntityC entityC, String name) { |
| 140 | + this.entityC = entityC; |
| 141 | + this.name = name; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Entity( name = "EntityC" ) |
| 146 | + static class EntityC { |
| 147 | + @Id |
| 148 | + private String id; |
| 149 | + |
| 150 | + public EntityC() { |
| 151 | + } |
| 152 | + |
| 153 | + public EntityC(String id) { |
| 154 | + this.id = id; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Entity( name = "AnotherEntity" ) |
| 159 | + static class AnotherEntity { |
| 160 | + @Id |
| 161 | + private Long id; |
| 162 | + |
| 163 | + public AnotherEntity() { |
| 164 | + } |
| 165 | + |
| 166 | + public AnotherEntity(Long id) { |
| 167 | + this.id = id; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments