|
| 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.joined; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import org.hibernate.cfg.AvailableSettings; |
| 13 | +import org.hibernate.tool.schema.Action; |
| 14 | + |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.Jira; |
| 17 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 20 | +import org.hibernate.testing.orm.junit.Setting; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.BeforeAll; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import jakarta.persistence.Embeddable; |
| 26 | +import jakarta.persistence.EmbeddedId; |
| 27 | +import jakarta.persistence.Entity; |
| 28 | +import jakarta.persistence.Inheritance; |
| 29 | +import jakarta.persistence.InheritanceType; |
| 30 | +import jakarta.persistence.MappedSuperclass; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Marco Belladelli |
| 36 | + */ |
| 37 | +@DomainModel( annotatedClasses = { |
| 38 | + JoinedInheritanceEmbeddedIdTest.PkEmbeddable.class, |
| 39 | + JoinedInheritanceEmbeddedIdTest.BasePk.class, |
| 40 | + JoinedInheritanceEmbeddedIdTest.BaseEntity.class, |
| 41 | + JoinedInheritanceEmbeddedIdTest.SubEntity.class, |
| 42 | +} ) |
| 43 | +@SessionFactory |
| 44 | +@ServiceRegistry( settings = @Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "create-drop" ) ) |
| 45 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17883" ) |
| 46 | +public class JoinedInheritanceEmbeddedIdTest { |
| 47 | + @Test |
| 48 | + public void testFind(SessionFactoryScope scope) { |
| 49 | + final BasePk basePk = new BasePk( 1, "lesson_1", "record_1" ); |
| 50 | + scope.inSession( session -> { |
| 51 | + final BaseEntity baseEntity = session.find( BaseEntity.class, basePk ); |
| 52 | + assertThat( baseEntity ).isNotNull().extracting( BaseEntity::getPrimaryKey ).isEqualTo( basePk ); |
| 53 | + } ); |
| 54 | + scope.inSession( session -> { |
| 55 | + final SubEntity subEntity = session.find( SubEntity.class, basePk ); |
| 56 | + assertThat( subEntity ).isNotNull().extracting( SubEntity::getName ).isEqualTo( "sub_entity_1" ); |
| 57 | + } ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testQuery(SessionFactoryScope scope) { |
| 62 | + scope.inTransaction( session -> { |
| 63 | + final SubEntity result = session.createQuery( |
| 64 | + "from SubEntity where primaryKey.siteCd = 1", |
| 65 | + SubEntity.class |
| 66 | + ).getSingleResult(); |
| 67 | + assertThat( result ).isNotNull().extracting( SubEntity::getName ).isEqualTo( "sub_entity_1" ); |
| 68 | + } ); |
| 69 | + } |
| 70 | + |
| 71 | + @BeforeAll |
| 72 | + public void setUp(SessionFactoryScope scope) { |
| 73 | + scope.inTransaction( session -> { |
| 74 | + final BasePk basePk = new BasePk( 1, "lesson_1", "record_1" ); |
| 75 | + session.persist( new SubEntity( basePk, "sub_entity_1" ) ); |
| 76 | + } ); |
| 77 | + } |
| 78 | + |
| 79 | + @AfterAll |
| 80 | + public void tearDown(SessionFactoryScope scope) { |
| 81 | + scope.inTransaction( session -> session.createMutationQuery( "delete from BaseEntity" ).executeUpdate() ); |
| 82 | + } |
| 83 | + |
| 84 | + @MappedSuperclass |
| 85 | + static class PkEmbeddable implements Serializable { |
| 86 | + private Integer siteCd; |
| 87 | + |
| 88 | + public PkEmbeddable() { |
| 89 | + } |
| 90 | + |
| 91 | + public PkEmbeddable(Integer siteCd) { |
| 92 | + this.siteCd = siteCd; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object o) { |
| 97 | + if ( this == o ) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + if ( o == null || getClass() != o.getClass() ) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + final PkEmbeddable that = (PkEmbeddable) o; |
| 105 | + return Objects.equals( siteCd, that.siteCd ); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int hashCode() { |
| 110 | + return Objects.hashCode( siteCd ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Embeddable |
| 115 | + static class BasePk extends PkEmbeddable { |
| 116 | + private String lessonCd; |
| 117 | + private String recordCd; |
| 118 | + |
| 119 | + public BasePk() { |
| 120 | + } |
| 121 | + |
| 122 | + public BasePk(Integer siteCd, String lessonCd, String recordCd) { |
| 123 | + super( siteCd ); |
| 124 | + this.lessonCd = lessonCd; |
| 125 | + this.recordCd = recordCd; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean equals(Object o) { |
| 130 | + if ( this == o ) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + if ( !super.equals( o ) ) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + final BasePk basePk = (BasePk) o; |
| 138 | + return Objects.equals( lessonCd, basePk.lessonCd ) && Objects.equals( |
| 139 | + recordCd, |
| 140 | + basePk.recordCd |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public int hashCode() { |
| 146 | + int result = super.hashCode(); |
| 147 | + result = 31 * result + Objects.hashCode( lessonCd ); |
| 148 | + result = 31 * result + Objects.hashCode( recordCd ); |
| 149 | + return result; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Entity( name = "BaseEntity" ) |
| 154 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 155 | + static class BaseEntity { |
| 156 | + @EmbeddedId |
| 157 | + private BasePk primaryKey; |
| 158 | + |
| 159 | + public BaseEntity() { |
| 160 | + } |
| 161 | + |
| 162 | + public BaseEntity(BasePk primaryKey) { |
| 163 | + this.primaryKey = primaryKey; |
| 164 | + } |
| 165 | + |
| 166 | + public BasePk getPrimaryKey() { |
| 167 | + return primaryKey; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Entity( name = "SubEntity" ) |
| 172 | + static class SubEntity extends BaseEntity { |
| 173 | + private String name; |
| 174 | + |
| 175 | + public SubEntity() { |
| 176 | + } |
| 177 | + |
| 178 | + public SubEntity(BasePk primaryKey, String name) { |
| 179 | + super( primaryKey ); |
| 180 | + this.name = name; |
| 181 | + } |
| 182 | + |
| 183 | + public String getName() { |
| 184 | + return name; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments