|
| 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.inheritance; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.Hibernate; |
| 12 | +import org.hibernate.annotations.FetchProfile; |
| 13 | + |
| 14 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.Jira; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import jakarta.persistence.DiscriminatorColumn; |
| 24 | +import jakarta.persistence.DiscriminatorValue; |
| 25 | +import jakarta.persistence.Entity; |
| 26 | +import jakarta.persistence.EntityGraph; |
| 27 | +import jakarta.persistence.FetchType; |
| 28 | +import jakarta.persistence.GeneratedValue; |
| 29 | +import jakarta.persistence.Id; |
| 30 | +import jakarta.persistence.Inheritance; |
| 31 | +import jakarta.persistence.InheritanceType; |
| 32 | +import jakarta.persistence.JoinColumn; |
| 33 | +import jakarta.persistence.ManyToOne; |
| 34 | +import jakarta.persistence.NamedAttributeNode; |
| 35 | +import jakarta.persistence.NamedEntityGraph; |
| 36 | +import jakarta.persistence.OneToMany; |
| 37 | +import jakarta.persistence.OneToOne; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | +import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOAD_GRAPH; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Jan Schatteman |
| 44 | + * @author Marco Belladelli |
| 45 | + */ |
| 46 | +@DomainModel( annotatedClasses = { |
| 47 | + InheritedTypeAssociationJoinedFetchTest.Animal.class, |
| 48 | + InheritedTypeAssociationJoinedFetchTest.Tiger.class, |
| 49 | + InheritedTypeAssociationJoinedFetchTest.Elephant.class, |
| 50 | + InheritedTypeAssociationJoinedFetchTest.Zoo.class, |
| 51 | +} ) |
| 52 | +@SessionFactory( useCollectingStatementInspector = true ) |
| 53 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17328" ) |
| 54 | +public class InheritedTypeAssociationJoinedFetchTest { |
| 55 | + @BeforeAll |
| 56 | + public void setUp(SessionFactoryScope scope) { |
| 57 | + scope.inTransaction( session -> { |
| 58 | + final Zoo zoo = new Zoo( 1L ); |
| 59 | + session.persist( zoo ); |
| 60 | + session.persist( new Tiger( "tiger", zoo ) ); |
| 61 | + session.persist( new Elephant( "elephant", zoo ) ); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterAll |
| 66 | + public void tearDown(SessionFactoryScope scope) { |
| 67 | + scope.inTransaction( session -> { |
| 68 | + session.createMutationQuery( "delete from Animal" ).executeUpdate(); |
| 69 | + session.createMutationQuery( "delete from Zoo" ).executeUpdate(); |
| 70 | + } ); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testExplicitJoinFetch(SessionFactoryScope scope) { |
| 75 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 76 | + inspector.clear(); |
| 77 | + |
| 78 | + scope.inTransaction( session -> { |
| 79 | + final Zoo zoo = session.createQuery( |
| 80 | + "from Zoo z join fetch z.tiger join fetch z.elephants", |
| 81 | + Zoo.class |
| 82 | + ).getSingleResult(); |
| 83 | + assertResult( zoo, inspector ); |
| 84 | + } ); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testEntityGraph(SessionFactoryScope scope) { |
| 89 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 90 | + inspector.clear(); |
| 91 | + |
| 92 | + scope.inTransaction( session -> { |
| 93 | + final EntityGraph<?> entityGraph = session.getEntityGraph( "graph-with-all-animals" ); |
| 94 | + final Zoo zoo = session.createQuery( "from Zoo", Zoo.class ) |
| 95 | + .setHint( HINT_SPEC_LOAD_GRAPH, entityGraph ) |
| 96 | + .getSingleResult(); |
| 97 | + assertResult( zoo, inspector ); |
| 98 | + } ); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testFetchProfile(SessionFactoryScope scope) { |
| 103 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 104 | + inspector.clear(); |
| 105 | + |
| 106 | + scope.inTransaction( session -> { |
| 107 | + session.enableFetchProfile( "profile-with-all-animals" ); |
| 108 | + final Zoo zoo = session.createQuery( "from Zoo", Zoo.class ) |
| 109 | + .getSingleResult(); |
| 110 | + assertResult( zoo, inspector ); |
| 111 | + } ); |
| 112 | + } |
| 113 | + |
| 114 | + private void assertResult(final Zoo zoo, final SQLStatementInspector inspector) { |
| 115 | + assertThat( Hibernate.isInitialized( zoo.getTiger() ) ).isTrue(); |
| 116 | + assertThat( zoo.getTiger().getName() ).isEqualTo( "tiger" ); |
| 117 | + assertThat( Hibernate.isInitialized( zoo.getElephants() ) ).isTrue(); |
| 118 | + assertThat( zoo.getElephants() ).hasSize( 1 ); |
| 119 | + assertThat( zoo.getElephants().get( 0 ).getName() ).isEqualTo( "elephant" ); |
| 120 | + |
| 121 | + inspector.assertExecutedCount( 1 ); |
| 122 | + inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "animal_type", 2 ); |
| 123 | + } |
| 124 | + |
| 125 | + @Entity( name = "Animal" ) |
| 126 | + @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) |
| 127 | + @DiscriminatorColumn( name = "animal_type" ) |
| 128 | + public static class Animal { |
| 129 | + @Id |
| 130 | + @GeneratedValue |
| 131 | + private Long id; |
| 132 | + |
| 133 | + private String name; |
| 134 | + |
| 135 | + @ManyToOne |
| 136 | + @JoinColumn( name = "zoo_id" ) |
| 137 | + private Zoo zoo; |
| 138 | + |
| 139 | + public Animal() { |
| 140 | + } |
| 141 | + |
| 142 | + public Animal(String name, Zoo zoo) { |
| 143 | + this.name = name; |
| 144 | + this.zoo = zoo; |
| 145 | + } |
| 146 | + |
| 147 | + public String getName() { |
| 148 | + return name; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Entity( name = "Tiger" ) |
| 153 | + @DiscriminatorValue( "Tiger" ) |
| 154 | + public static class Tiger extends Animal { |
| 155 | + public Tiger() { |
| 156 | + } |
| 157 | + |
| 158 | + public Tiger(String name, Zoo zoo) { |
| 159 | + super( name, zoo ); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Entity( name = "Elephant" ) |
| 164 | + @DiscriminatorValue( "Elephant" ) |
| 165 | + public static class Elephant extends Animal { |
| 166 | + public Elephant() { |
| 167 | + } |
| 168 | + |
| 169 | + public Elephant(String name, Zoo zoo) { |
| 170 | + super( name, zoo ); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Entity( name = "Zoo" ) |
| 175 | + @NamedEntityGraph( name = "graph-with-all-animals", attributeNodes = { |
| 176 | + @NamedAttributeNode( value = "tiger" ), |
| 177 | + @NamedAttributeNode( value = "elephants" ) |
| 178 | + } ) |
| 179 | + @FetchProfile( name = "profile-with-all-animals", fetchOverrides = { |
| 180 | + @FetchProfile.FetchOverride( association = "tiger", entity = Zoo.class, fetch = FetchType.EAGER ), |
| 181 | + @FetchProfile.FetchOverride( association = "elephants", entity = Zoo.class, fetch = FetchType.EAGER ), |
| 182 | + } ) |
| 183 | + public static class Zoo { |
| 184 | + @Id |
| 185 | + private Long id; |
| 186 | + |
| 187 | + @OneToOne( mappedBy = "zoo", fetch = FetchType.LAZY ) |
| 188 | + private Tiger tiger; |
| 189 | + |
| 190 | + @OneToMany( mappedBy = "zoo", fetch = FetchType.LAZY ) |
| 191 | + private List<Elephant> elephants; |
| 192 | + |
| 193 | + public Zoo() { |
| 194 | + } |
| 195 | + |
| 196 | + public Zoo(Long id) { |
| 197 | + this.id = id; |
| 198 | + } |
| 199 | + |
| 200 | + public Tiger getTiger() { |
| 201 | + return tiger; |
| 202 | + } |
| 203 | + |
| 204 | + public List<Elephant> getElephants() { |
| 205 | + return elephants; |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments