|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.inheritance.joined; |
| 6 | + |
| 7 | +import jakarta.persistence.Column; |
| 8 | +import jakarta.persistence.DiscriminatorColumn; |
| 9 | +import jakarta.persistence.DiscriminatorValue; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.FetchType; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.Inheritance; |
| 14 | +import jakarta.persistence.InheritanceType; |
| 15 | +import jakarta.persistence.JoinColumn; |
| 16 | +import jakarta.persistence.ManyToOne; |
| 17 | +import jakarta.persistence.OneToMany; |
| 18 | +import jakarta.persistence.Table; |
| 19 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 20 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 21 | +import org.hibernate.testing.orm.junit.Jira; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 23 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 24 | +import org.junit.jupiter.api.AfterAll; |
| 25 | +import org.junit.jupiter.api.BeforeAll; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +@DomainModel(annotatedClasses = { |
| 33 | + JoinedDiscriminatorSameChildTableTest.EntityParent.class, |
| 34 | + JoinedDiscriminatorSameChildTableTest.EntityChildOne.class, |
| 35 | + JoinedDiscriminatorSameChildTableTest.EntityChildTwo.class, |
| 36 | + JoinedDiscriminatorSameChildTableTest.EntityRelation.class, |
| 37 | +}) |
| 38 | +@SessionFactory(useCollectingStatementInspector = true) |
| 39 | +@Jira("https://hibernate.atlassian.net/browse/HHH-19457") |
| 40 | +public class JoinedDiscriminatorSameChildTableTest { |
| 41 | + @Test |
| 42 | + public void testParents(SessionFactoryScope scope) { |
| 43 | + final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); |
| 44 | + |
| 45 | + scope.inSession( session -> { |
| 46 | + final EntityRelation relation = session.find( EntityRelation.class, "relation_1" ); |
| 47 | + inspector.clear(); |
| 48 | + assertThat( relation.getParents() ).hasSize( 2 ); |
| 49 | + } ); |
| 50 | + // no need to filter by discriminator column, as we're selecting all subtypes of EntityParent |
| 51 | + inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "disc_col", 1 ); |
| 52 | + |
| 53 | + scope.inSession( session -> { |
| 54 | + final EntityRelation relation = session.createQuery( |
| 55 | + "from EntityRelation where id = 'relation_1'", |
| 56 | + EntityRelation.class |
| 57 | + ).getSingleResult(); |
| 58 | + inspector.clear(); |
| 59 | + assertThat( relation.getParents() ).hasSize( 2 ); |
| 60 | + } ); |
| 61 | + inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "disc_col", 1 ); |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeAll |
| 65 | + public void setUp(SessionFactoryScope scope) { |
| 66 | + scope.inTransaction( session -> { |
| 67 | + final EntityRelation relation = new EntityRelation(); |
| 68 | + relation.setId( "relation_1" ); |
| 69 | + session.persist( relation ); |
| 70 | + |
| 71 | + final EntityChildOne c1 = new EntityChildOne(); |
| 72 | + c1.setId( "child_1" ); |
| 73 | + c1.setIdRelation( "relation_1" ); |
| 74 | + session.persist( c1 ); |
| 75 | + |
| 76 | + final EntityChildTwo c2 = new EntityChildTwo(); |
| 77 | + c2.setId( "child_2" ); |
| 78 | + c2.setIdRelation( "relation_1" ); |
| 79 | + session.persist( c2 ); |
| 80 | + } ); |
| 81 | + } |
| 82 | + |
| 83 | + @AfterAll |
| 84 | + public void tearDown(SessionFactoryScope scope) { |
| 85 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 86 | + } |
| 87 | + |
| 88 | + @Entity(name = "EntityParent") |
| 89 | + @Table(name = "parent_table") |
| 90 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 91 | + @DiscriminatorColumn(name = "disc_col") |
| 92 | + static abstract class EntityParent { |
| 93 | + @Id |
| 94 | + @Column(name = "id") |
| 95 | + private String id; |
| 96 | + |
| 97 | + @Column(name = "id_relation") |
| 98 | + private String idRelation; |
| 99 | + |
| 100 | + @ManyToOne(fetch = FetchType.LAZY) |
| 101 | + @JoinColumn(name = "id_relation", referencedColumnName = "id", insertable = false, updatable = false) |
| 102 | + private EntityRelation relation; |
| 103 | + |
| 104 | + public EntityRelation getRelation() { |
| 105 | + return relation; |
| 106 | + } |
| 107 | + |
| 108 | + public void setRelation(EntityRelation requisition) { |
| 109 | + this.relation = requisition; |
| 110 | + } |
| 111 | + |
| 112 | + public String getIdRelation() { |
| 113 | + return idRelation; |
| 114 | + } |
| 115 | + |
| 116 | + public void setIdRelation(String idRelation) { |
| 117 | + this.idRelation = idRelation; |
| 118 | + } |
| 119 | + |
| 120 | + public String getId() { |
| 121 | + return id; |
| 122 | + } |
| 123 | + |
| 124 | + public void setId(String id) { |
| 125 | + this.id = id; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Entity(name = "EntityRelation") |
| 130 | + static class EntityRelation { |
| 131 | + @Id |
| 132 | + private String id; |
| 133 | + |
| 134 | + @OneToMany(fetch = FetchType.LAZY, mappedBy = "relation") |
| 135 | + private List<EntityParent> parents; |
| 136 | + |
| 137 | + public void setId(String id) { |
| 138 | + this.id = id; |
| 139 | + } |
| 140 | + |
| 141 | + public String getId() { |
| 142 | + return id; |
| 143 | + } |
| 144 | + |
| 145 | + public List<EntityParent> getParents() { |
| 146 | + return parents; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Entity(name = "EntityChildOne") |
| 151 | + @Table(name = "child_table") |
| 152 | + @DiscriminatorValue("child-one") |
| 153 | + static class EntityChildOne extends EntityParent { |
| 154 | + private String name; |
| 155 | + } |
| 156 | + |
| 157 | + @Entity(name = "EntityChildTwo") |
| 158 | + @Table(name = "child_table") |
| 159 | + @DiscriminatorValue("child-two") |
| 160 | + static class EntityChildTwo extends EntityParent { |
| 161 | + private Integer age; |
| 162 | + } |
| 163 | +} |
0 commit comments