|
| 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.embeddable; |
| 8 | + |
| 9 | +import org.hibernate.annotations.DiscriminatorFormula; |
| 10 | +import org.hibernate.annotations.Imported; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.DiscriminatorType; |
| 20 | +import jakarta.persistence.DiscriminatorValue; |
| 21 | +import jakarta.persistence.Embeddable; |
| 22 | +import jakarta.persistence.Embedded; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Marco Belladelli |
| 30 | + */ |
| 31 | +@DomainModel( annotatedClasses = { |
| 32 | + EmbeddableInheritanceDiscriminatorFormulaTest.TestEntity.class, |
| 33 | + EmbeddableInheritanceDiscriminatorFormulaTest.FormulaEmbeddable.class, |
| 34 | + EmbeddableInheritanceDiscriminatorFormulaTest.ChildOneEmbeddable.class, |
| 35 | + EmbeddableInheritanceDiscriminatorFormulaTest.SubChildOneEmbeddable.class, |
| 36 | + EmbeddableInheritanceDiscriminatorFormulaTest.ChildTwoEmbeddable.class, |
| 37 | +} ) |
| 38 | +@SessionFactory |
| 39 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18382" ) |
| 40 | +public class EmbeddableInheritanceDiscriminatorFormulaTest { |
| 41 | + @Test |
| 42 | + public void testFind(SessionFactoryScope scope) { |
| 43 | + scope.inTransaction( session -> { |
| 44 | + final TestEntity result = session.find( TestEntity.class, 1L ); |
| 45 | + assertThat( result.embeddable.kind ).isEqualTo( "kind_1" ); |
| 46 | + assertThat( result.embeddable ).isExactlyInstanceOf( ChildOneEmbeddable.class ); |
| 47 | + assertThat( ( (ChildOneEmbeddable) result.embeddable ).childOneProp ).isEqualTo( 1 ); |
| 48 | + } ); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testQueryEntity(SessionFactoryScope scope) { |
| 53 | + scope.inTransaction( session -> { |
| 54 | + final TestEntity result = session.createQuery( |
| 55 | + "from TestEntity where id = 2", |
| 56 | + TestEntity.class |
| 57 | + ).getSingleResult(); |
| 58 | + assertThat( result.embeddable.kind ).isEqualTo( "kind_2" ); |
| 59 | + assertThat( result.embeddable ).isExactlyInstanceOf( ChildTwoEmbeddable.class ); |
| 60 | + assertThat( ( (ChildTwoEmbeddable) result.embeddable ).childTwoProp ).isEqualTo( "2" ); |
| 61 | + } ); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testQueryEmbeddable(SessionFactoryScope scope) { |
| 66 | + scope.inTransaction( session -> { |
| 67 | + final FormulaEmbeddable result = session.createQuery( |
| 68 | + "select embeddable from TestEntity where id = 3", |
| 69 | + FormulaEmbeddable.class |
| 70 | + ).getSingleResult(); |
| 71 | + assertThat( result.kind ).isEqualTo( "sub_kind_1" ); |
| 72 | + assertThat( result ).isExactlyInstanceOf( SubChildOneEmbeddable.class ); |
| 73 | + assertThat( ( (SubChildOneEmbeddable) result ).subChildOneProp ).isEqualTo( 3.0 ); |
| 74 | + } ); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testType(SessionFactoryScope scope) { |
| 79 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 80 | + "select id from TestEntity where type(embeddable) = ChildOneEmbeddable", |
| 81 | + Long.class |
| 82 | + ).getSingleResult() ).isEqualTo( 1L ) ); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testTreat(SessionFactoryScope scope) { |
| 87 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 88 | + "select id from TestEntity where treat(embeddable as ChildTwoEmbeddable).childTwoProp = '2'", |
| 89 | + Long.class |
| 90 | + ).getSingleResult() ).isEqualTo( 2L ) ); |
| 91 | + } |
| 92 | + |
| 93 | + @BeforeAll |
| 94 | + public void setUp(SessionFactoryScope scope) { |
| 95 | + scope.inTransaction( session -> { |
| 96 | + session.persist( new TestEntity( 1L, new ChildOneEmbeddable( 1 ) ) ); |
| 97 | + session.persist( new TestEntity( 2L, new ChildTwoEmbeddable( "2" ) ) ); |
| 98 | + session.persist( new TestEntity( 3L, new SubChildOneEmbeddable( 3, 3.0 ) ) ); |
| 99 | + } ); |
| 100 | + } |
| 101 | + |
| 102 | + @Entity( name = "TestEntity" ) |
| 103 | + static class TestEntity { |
| 104 | + @Id |
| 105 | + private Long id; |
| 106 | + |
| 107 | + @Embedded |
| 108 | + private FormulaEmbeddable embeddable; |
| 109 | + |
| 110 | + public TestEntity() { |
| 111 | + } |
| 112 | + |
| 113 | + public TestEntity(Long id, FormulaEmbeddable embeddable) { |
| 114 | + this.embeddable = embeddable; |
| 115 | + this.id = id; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Embeddable |
| 120 | + @DiscriminatorFormula( |
| 121 | + value = "case when kind = 'kind_1' then 1 when kind = 'sub_kind_1' then 11 else 2 end", |
| 122 | + discriminatorType = DiscriminatorType.INTEGER |
| 123 | + ) |
| 124 | + static class FormulaEmbeddable { |
| 125 | + protected String kind; |
| 126 | + |
| 127 | + public FormulaEmbeddable() { |
| 128 | + } |
| 129 | + |
| 130 | + public FormulaEmbeddable(String kind) { |
| 131 | + this.kind = kind; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Embeddable |
| 136 | + @DiscriminatorValue( "1" ) |
| 137 | + @Imported( rename = "ChildOneEmbeddable" ) |
| 138 | + static class ChildOneEmbeddable extends FormulaEmbeddable { |
| 139 | + protected Integer childOneProp; |
| 140 | + |
| 141 | + public ChildOneEmbeddable() { |
| 142 | + } |
| 143 | + |
| 144 | + public ChildOneEmbeddable(Integer childOneProp) { |
| 145 | + super( "kind_1" ); |
| 146 | + this.childOneProp = childOneProp; |
| 147 | + } |
| 148 | + |
| 149 | + protected ChildOneEmbeddable(String kind, Integer childOneProp) { |
| 150 | + super( kind ); |
| 151 | + this.childOneProp = childOneProp; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Embeddable |
| 156 | + @DiscriminatorValue( "11" ) |
| 157 | + static class SubChildOneEmbeddable extends ChildOneEmbeddable { |
| 158 | + protected Double subChildOneProp; |
| 159 | + |
| 160 | + public SubChildOneEmbeddable() { |
| 161 | + } |
| 162 | + |
| 163 | + public SubChildOneEmbeddable(Integer childOneProp, Double subChildOneProp) { |
| 164 | + super( "sub_kind_1", childOneProp ); |
| 165 | + this.subChildOneProp = subChildOneProp; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Embeddable |
| 170 | + @DiscriminatorValue( "2" ) |
| 171 | + @Imported( rename = "ChildTwoEmbeddable" ) |
| 172 | + static class ChildTwoEmbeddable extends FormulaEmbeddable { |
| 173 | + protected String childTwoProp; |
| 174 | + |
| 175 | + public ChildTwoEmbeddable() { |
| 176 | + } |
| 177 | + |
| 178 | + public ChildTwoEmbeddable(String childTwoProp) { |
| 179 | + super( "kind_2" ); |
| 180 | + this.childTwoProp = childTwoProp; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments