|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.embeddable; |
| 6 | + |
| 7 | +import jakarta.persistence.AttributeOverride; |
| 8 | +import jakarta.persistence.AttributeOverrides; |
| 9 | +import jakarta.persistence.Column; |
| 10 | +import jakarta.persistence.Embeddable; |
| 11 | +import jakarta.persistence.Embedded; |
| 12 | +import jakarta.persistence.Entity; |
| 13 | +import jakarta.persistence.Id; |
| 14 | +import jakarta.persistence.Tuple; |
| 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 static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +@DomainModel(annotatedClasses = { |
| 26 | + EmbeddableSameNestedNameSelectionTest.Material.class, |
| 27 | + EmbeddableSameNestedNameSelectionTest.Weight.class, |
| 28 | + EmbeddableSameNestedNameSelectionTest.Length.class |
| 29 | +}) |
| 30 | +@SessionFactory |
| 31 | +@Jira("https://hibernate.atlassian.net/browse/HHH-19962") |
| 32 | +@Jira("https://hibernate.atlassian.net/browse/HHH-19985") |
| 33 | +public class EmbeddableSameNestedNameSelectionTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void testPlainEntity(SessionFactoryScope scope) { |
| 37 | + scope.inTransaction( session -> { |
| 38 | + final var result = session.createSelectionQuery( "from Material", Material.class ) |
| 39 | + .getSingleResult(); |
| 40 | + assertThat( result.getWeight().getValue() ).isEqualTo( "10" ); |
| 41 | + assertThat( result.getLength().getValue() ).isEqualTo( "2" ); |
| 42 | + } ); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testSubqueryEmbedded(SessionFactoryScope scope) { |
| 47 | + scope.inTransaction( session -> { |
| 48 | + final var result = session.createSelectionQuery( |
| 49 | + "select q.weight as weight, q.length as length from (select m.weight as weight, m.length as length from Material m) q", |
| 50 | + Tuple.class |
| 51 | + ).getSingleResult(); |
| 52 | + assertEmbeddedTuple( result ); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testEmbedded(SessionFactoryScope scope) { |
| 58 | + scope.inTransaction( session -> { |
| 59 | + final var result = session.createSelectionQuery( |
| 60 | + "select m.weight as weight, m.length as length from Material m", |
| 61 | + Tuple.class |
| 62 | + ).getSingleResult(); |
| 63 | + assertEmbeddedTuple( result ); |
| 64 | + } ); |
| 65 | + } |
| 66 | + |
| 67 | + private void assertEmbeddedTuple(Tuple result) { |
| 68 | + final var weight = result.get( "weight", Weight.class ); |
| 69 | + assertThat( weight.getValue() ).isEqualTo( "10" ); |
| 70 | + assertThat( weight.getUnit() ).isEqualTo( WeightUnit.KILOGRAM ); |
| 71 | + final var length = result.get( "length", Length.class ); |
| 72 | + assertThat( length.getValue() ).isEqualTo( "2" ); |
| 73 | + assertThat( length.getUnit() ).isEqualTo( LengthUnit.METER ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testSubqueryScalar(SessionFactoryScope scope) { |
| 78 | + scope.inTransaction( session -> { |
| 79 | + final var result = session.createSelectionQuery( |
| 80 | + "select q.weight, q.weight_unit, q.length, q.length_unit from " |
| 81 | + + "(select m.weight.value as weight, m.weight.unit as weight_unit, m.length.value as length, m.length.unit as length_unit from Material m) q", |
| 82 | + Tuple.class |
| 83 | + ).getSingleResult(); |
| 84 | + assertScalarTuple( result ); |
| 85 | + } ); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testScalar(SessionFactoryScope scope) { |
| 90 | + scope.inTransaction( session -> { |
| 91 | + final var result = session.createSelectionQuery( |
| 92 | + "select m.weight.value, m.weight.unit, m.length.value, m.length.unit from Material m", |
| 93 | + Tuple.class |
| 94 | + ).getSingleResult(); |
| 95 | + assertScalarTuple( result ); |
| 96 | + } ); |
| 97 | + } |
| 98 | + |
| 99 | + private void assertScalarTuple(Tuple result) { |
| 100 | + assertThat( result.get( 0, String.class ) ).isEqualTo( "10" ); |
| 101 | + assertThat( result.get( 1, WeightUnit.class ) ).isEqualTo( WeightUnit.KILOGRAM ); |
| 102 | + assertThat( result.get( 2, String.class ) ).isEqualTo( "2" ); |
| 103 | + assertThat( result.get( 3, LengthUnit.class ) ).isEqualTo( LengthUnit.METER ); |
| 104 | + } |
| 105 | + |
| 106 | + @BeforeAll |
| 107 | + public void setUp(SessionFactoryScope scope) { |
| 108 | + scope.inTransaction( session -> session.persist( |
| 109 | + new Material( 1L, new Weight( "10", WeightUnit.KILOGRAM ), new Length( "2", LengthUnit.METER ) ) |
| 110 | + ) ); |
| 111 | + } |
| 112 | + |
| 113 | + @AfterAll |
| 114 | + public void tearDown(SessionFactoryScope scope) { |
| 115 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 116 | + } |
| 117 | + |
| 118 | + @Entity(name = "Material") |
| 119 | + static class Material { |
| 120 | + @Id |
| 121 | + private Long id; |
| 122 | + |
| 123 | + @Embedded |
| 124 | + @AttributeOverrides({ |
| 125 | + @AttributeOverride(name = "value", column = @Column(name = "weight_value")), |
| 126 | + @AttributeOverride(name = "unit", column = @Column(name = "weight_unit")), |
| 127 | + }) |
| 128 | + private Weight weight; |
| 129 | + |
| 130 | + @Embedded |
| 131 | + @AttributeOverrides({ |
| 132 | + @AttributeOverride(name = "value", column = @Column(name = "length_value")), |
| 133 | + @AttributeOverride(name = "unit", column = @Column(name = "length_unit")), |
| 134 | + }) |
| 135 | + private Length length; |
| 136 | + |
| 137 | + public Material() { |
| 138 | + } |
| 139 | + |
| 140 | + public Material(Long id, Weight weight, Length length) { |
| 141 | + this.id = id; |
| 142 | + this.weight = weight; |
| 143 | + this.length = length; |
| 144 | + } |
| 145 | + |
| 146 | + public Long getId() { |
| 147 | + return id; |
| 148 | + } |
| 149 | + |
| 150 | + public Weight getWeight() { |
| 151 | + return weight; |
| 152 | + } |
| 153 | + |
| 154 | + public void setWeight(Weight weight) { |
| 155 | + this.weight = weight; |
| 156 | + } |
| 157 | + |
| 158 | + public Length getLength() { |
| 159 | + return length; |
| 160 | + } |
| 161 | + |
| 162 | + public void setLength(Length length) { |
| 163 | + this.length = length; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Embeddable |
| 168 | + static class Weight { |
| 169 | + private String value; |
| 170 | + |
| 171 | + private WeightUnit unit; |
| 172 | + |
| 173 | + public Weight() { |
| 174 | + } |
| 175 | + |
| 176 | + public Weight(String value, WeightUnit unit) { |
| 177 | + this.value = value; |
| 178 | + this.unit = unit; |
| 179 | + } |
| 180 | + |
| 181 | + public String getValue() { |
| 182 | + return value; |
| 183 | + } |
| 184 | + |
| 185 | + public WeightUnit getUnit() { |
| 186 | + return unit; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + enum WeightUnit { |
| 191 | + KILOGRAM, |
| 192 | + POUND |
| 193 | + } |
| 194 | + |
| 195 | + @Embeddable |
| 196 | + static class Length { |
| 197 | + private String value; |
| 198 | + |
| 199 | + private LengthUnit unit; |
| 200 | + |
| 201 | + public Length() { |
| 202 | + } |
| 203 | + |
| 204 | + public Length(String value, LengthUnit unit) { |
| 205 | + this.value = value; |
| 206 | + this.unit = unit; |
| 207 | + } |
| 208 | + |
| 209 | + public String getValue() { |
| 210 | + return value; |
| 211 | + } |
| 212 | + |
| 213 | + public LengthUnit getUnit() { |
| 214 | + return unit; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + enum LengthUnit { |
| 219 | + METER, |
| 220 | + FOOT |
| 221 | + } |
| 222 | +} |
0 commit comments