|
| 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.inheritance; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.hibernate.Hibernate; |
| 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.Column; |
| 24 | +import jakarta.persistence.DiscriminatorValue; |
| 25 | +import jakarta.persistence.Entity; |
| 26 | +import jakarta.persistence.GeneratedValue; |
| 27 | +import jakarta.persistence.Id; |
| 28 | +import jakarta.persistence.Inheritance; |
| 29 | +import jakarta.persistence.InheritanceType; |
| 30 | +import jakarta.persistence.JoinColumn; |
| 31 | +import jakarta.persistence.JoinTable; |
| 32 | +import jakarta.persistence.ManyToMany; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Marco Belladelli |
| 38 | + */ |
| 39 | +@DomainModel( annotatedClasses = { |
| 40 | + ManyToManyJoinTableAndInheritanceTest.RootEntity.class, |
| 41 | + ManyToManyJoinTableAndInheritanceTest.ParentEntity.class, |
| 42 | + ManyToManyJoinTableAndInheritanceTest.Sub1.class, |
| 43 | +} ) |
| 44 | +@SessionFactory( useCollectingStatementInspector = true ) |
| 45 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17679" ) |
| 46 | +public class ManyToManyJoinTableAndInheritanceTest { |
| 47 | + @BeforeAll |
| 48 | + public void setUp(SessionFactoryScope scope) { |
| 49 | + scope.inTransaction( session -> { |
| 50 | + final Sub1 sub1 = new Sub1( 1L, 1, 1 ); |
| 51 | + session.persist( sub1 ); |
| 52 | + final RootEntity root = new RootEntity(); |
| 53 | + root.getNodesPoly().add( sub1 ); |
| 54 | + session.persist( root ); |
| 55 | + } ); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterAll |
| 59 | + public void tearDown(SessionFactoryScope scope) { |
| 60 | + scope.inTransaction( session -> { |
| 61 | + session.createMutationQuery( "delete from RootEntity" ).executeUpdate(); |
| 62 | + session.createMutationQuery( "delete from ParentEntity" ).executeUpdate(); |
| 63 | + } ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testLeftJoinSelectParent(SessionFactoryScope scope) { |
| 68 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 69 | + inspector.clear(); |
| 70 | + |
| 71 | + scope.inTransaction( session -> { |
| 72 | + final RootEntity result = session.createQuery( |
| 73 | + "select root from RootEntity root left join root.nodesPoly _collection", |
| 74 | + RootEntity.class |
| 75 | + ).getSingleResult(); |
| 76 | + inspector.assertExecutedCount( 1 ); |
| 77 | + inspector.assertNumberOfOccurrenceInQuery( 0, "join", 1 ); |
| 78 | + assertThat( Hibernate.isInitialized( result.getNodesPoly() ) ).isFalse(); |
| 79 | + assertThat( result.getNodesPoly() ).hasSize( 1 ); |
| 80 | + assertThat( result.getNodesPoly().iterator().next().getId() ).isEqualTo( 1L ); |
| 81 | + } ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testLeftJoinSelectId(SessionFactoryScope scope) { |
| 86 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 87 | + inspector.clear(); |
| 88 | + |
| 89 | + scope.inTransaction( session -> { |
| 90 | + final Long result = session.createQuery( |
| 91 | + "select _collection.id from RootEntity root left join root.nodesPoly _collection", |
| 92 | + Long.class |
| 93 | + ).getSingleResult(); |
| 94 | + assertThat( result ).isEqualTo( 1L ); |
| 95 | + inspector.assertExecutedCount( 1 ); |
| 96 | + inspector.assertNumberOfOccurrenceInQuery( 0, "join", 1 ); |
| 97 | + } ); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testLeftJoinSelectElement(SessionFactoryScope scope) { |
| 102 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 103 | + inspector.clear(); |
| 104 | + |
| 105 | + scope.inTransaction( session -> { |
| 106 | + final Sub1 result = session.createQuery( |
| 107 | + "select _collection from RootEntity root left join root.nodesPoly _collection", |
| 108 | + Sub1.class |
| 109 | + ).getSingleResult(); |
| 110 | + inspector.assertExecutedCount( 1 ); |
| 111 | + inspector.assertNumberOfOccurrenceInQuery( 0, "join", 2 ); |
| 112 | + assertThat( result.getId() ).isEqualTo( 1L ); |
| 113 | + assertThat( result.getNumber() ).isEqualTo( 1 ); |
| 114 | + assertThat( result.getSub1Value() ).isEqualTo( 1 ); |
| 115 | + } ); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testLeftJoinFetch(SessionFactoryScope scope) { |
| 120 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 121 | + inspector.clear(); |
| 122 | + |
| 123 | + scope.inTransaction( session -> { |
| 124 | + final RootEntity result = session.createQuery( |
| 125 | + "select root from RootEntity root left join fetch root.nodesPoly _collection", |
| 126 | + RootEntity.class |
| 127 | + ).getSingleResult(); |
| 128 | + inspector.assertExecutedCount( 1 ); |
| 129 | + inspector.assertNumberOfOccurrenceInQuery( 0, "join", 2 ); |
| 130 | + assertThat( Hibernate.isInitialized( result.getNodesPoly() ) ).isTrue(); |
| 131 | + assertThat( result.getNodesPoly() ).hasSize( 1 ); |
| 132 | + assertThat( result.getNodesPoly().iterator().next().getId() ).isEqualTo( 1L ); |
| 133 | + } ); |
| 134 | + } |
| 135 | + |
| 136 | + @Entity( name = "RootEntity" ) |
| 137 | + public static class RootEntity { |
| 138 | + @Id |
| 139 | + @GeneratedValue |
| 140 | + private Long id; |
| 141 | + |
| 142 | + @ManyToMany |
| 143 | + @JoinTable( |
| 144 | + name = "set_one_to_many_poly", |
| 145 | + joinColumns = @JoinColumn( name = "root_id" ), |
| 146 | + inverseJoinColumns = @JoinColumn( name = "poly_id" ) |
| 147 | + ) |
| 148 | + private Set<Sub1> nodesPoly = new HashSet<>(); |
| 149 | + |
| 150 | + public Set<Sub1> getNodesPoly() { |
| 151 | + return nodesPoly; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Entity( name = "ParentEntity" ) |
| 156 | + @DiscriminatorValue( "0" ) |
| 157 | + @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) |
| 158 | + public static class ParentEntity { |
| 159 | + @Id |
| 160 | + private Long id; |
| 161 | + |
| 162 | + @Column( name = "number_col" ) |
| 163 | + private Integer number; |
| 164 | + |
| 165 | + public ParentEntity() { |
| 166 | + } |
| 167 | + |
| 168 | + public ParentEntity(Long id, Integer number) { |
| 169 | + this.id = id; |
| 170 | + this.number = number; |
| 171 | + } |
| 172 | + |
| 173 | + public Long getId() { |
| 174 | + return id; |
| 175 | + } |
| 176 | + |
| 177 | + public Integer getNumber() { |
| 178 | + return number; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @Entity( name = "Sub1" ) |
| 183 | + @DiscriminatorValue( "1" ) |
| 184 | + public static class Sub1 extends ParentEntity { |
| 185 | + private Integer sub1Value; |
| 186 | + |
| 187 | + public Sub1() { |
| 188 | + } |
| 189 | + |
| 190 | + public Sub1(Long id, Integer number, Integer sub1Value) { |
| 191 | + super( id, number ); |
| 192 | + this.sub1Value = sub1Value; |
| 193 | + } |
| 194 | + |
| 195 | + public Integer getSub1Value() { |
| 196 | + return sub1Value; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments