Skip to content

Commit 18864bf

Browse files
committed
HHH-15560 Add test for issue
1 parent f27b3a9 commit 18864bf

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
package org.hibernate.orm.test.annotations.embeddables;
2+
3+
import java.util.List;
4+
5+
import org.hibernate.testing.TestForIssue;
6+
import org.hibernate.testing.orm.junit.DomainModel;
7+
import org.hibernate.testing.orm.junit.FailureExpected;
8+
import org.hibernate.testing.orm.junit.SessionFactory;
9+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import jakarta.persistence.Column;
15+
import jakarta.persistence.Embeddable;
16+
import jakarta.persistence.Embedded;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.Id;
19+
import jakarta.persistence.Inheritance;
20+
import jakarta.persistence.InheritanceType;
21+
import jakarta.persistence.MappedSuperclass;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
@DomainModel(
26+
annotatedClasses = {
27+
GenericEmbeddableWthSubclassTest.PopularBook.class,
28+
GenericEmbeddableWthSubclassTest.RareBook.class,
29+
}
30+
)
31+
@SessionFactory
32+
@TestForIssue( jiraKey = "HHH-15560")
33+
public class GenericEmbeddableWthSubclassTest {
34+
private final static long POPULAR_BOOK_ID = 1l;
35+
private final static String POPULAR_BOOK_CODE = "POP";
36+
private final static Integer POPULAR_BOOK_YEAR = 1971;
37+
private final static long RARE_BOOK_ID = 2l;
38+
private final static Integer RARE_BOOK_CODE = 123;
39+
private final static String RARE_BOOK_STATE = "Good";
40+
41+
@BeforeEach
42+
public void setUp(SessionFactoryScope scope) {
43+
scope.inTransaction(
44+
session -> {
45+
PopularEdition popularEdition = new PopularEdition(
46+
"Popular",
47+
POPULAR_BOOK_CODE,
48+
POPULAR_BOOK_YEAR
49+
);
50+
PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition );
51+
52+
RareEdition rareEdition = new RareEdition( "Rare", RARE_BOOK_CODE, RARE_BOOK_STATE );
53+
RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition );
54+
55+
session.persist( popularBook );
56+
session.persist( rareBook );
57+
}
58+
);
59+
}
60+
61+
@AfterEach
62+
public void tearDown(SessionFactoryScope scope) {
63+
scope.inTransaction(
64+
session -> {
65+
session.createQuery( "delete from PopularBook" ).executeUpdate();
66+
session.createQuery( "delete from RareBook" ).executeUpdate();
67+
}
68+
);
69+
}
70+
71+
@Test
72+
@FailureExpected(jiraKey = "HHH-15560", reason = "Parametrized Embedded Subclasses not supported")
73+
public void testSelectSpecificEmbeddedAttribute(SessionFactoryScope scope) {
74+
scope.inTransaction(
75+
session -> {
76+
List<String> rareBookCodes = session.createQuery(
77+
"select b.edition.state from RareBook b where b.id = :id",
78+
String.class
79+
).setParameter( "id", RARE_BOOK_ID ).list();
80+
81+
assertThat( rareBookCodes.size() ).isEqualTo( 1 );
82+
83+
String code = rareBookCodes.get( 0 );
84+
assertThat( code ).isEqualTo( RARE_BOOK_STATE );
85+
}
86+
);
87+
88+
scope.inTransaction(
89+
session -> {
90+
List<Integer> populareBookCodes = session.createQuery(
91+
"select b.edition.year from PopularBook b where b.id = :id",
92+
Integer.class
93+
).setParameter( "id", POPULAR_BOOK_ID ).list();
94+
95+
assertThat( populareBookCodes.size() ).isEqualTo( 1 );
96+
97+
Integer code = populareBookCodes.get( 0 );
98+
assertThat( code ).isEqualTo( POPULAR_BOOK_YEAR );
99+
}
100+
);
101+
}
102+
103+
@Test
104+
@FailureExpected(jiraKey = "HHH-15560", reason = "Parametrized Embedded Subclasses not supported")
105+
public void testDowcasting(SessionFactoryScope scope) {
106+
scope.inTransaction(
107+
session -> {
108+
List<String> rareBookStates = session.createQuery(
109+
"select re.state from Base b left join treat(b.edition as RareEdition) re where b.id = :id",
110+
String.class
111+
).setParameter( "id", RARE_BOOK_ID ).list();
112+
113+
assertThat( rareBookStates.size() ).isEqualTo( 1 );
114+
115+
String state = rareBookStates.get( 0 );
116+
assertThat( state ).isEqualTo( RARE_BOOK_STATE );
117+
}
118+
);
119+
120+
scope.inTransaction(
121+
session -> {
122+
List<String> rareBookStates = session.createQuery(
123+
"select re.state from Base b left join b.edition re where b.id = :id",
124+
String.class
125+
).setParameter( "id", RARE_BOOK_ID ).list();
126+
127+
assertThat( rareBookStates.size() ).isEqualTo( 1 );
128+
129+
String state = rareBookStates.get( 0 );
130+
assertThat( state ).isEqualTo( RARE_BOOK_STATE );
131+
}
132+
);
133+
}
134+
135+
@Test
136+
public void testSelectBaseEmbeddableAttribute(SessionFactoryScope scope) {
137+
scope.inTransaction(
138+
session -> {
139+
List<Integer> rareBookCodes = session.createQuery(
140+
"select b.edition.code from RareBook b where b.id = :id",
141+
Integer.class
142+
).setParameter( "id", RARE_BOOK_ID ).list();
143+
144+
assertThat( rareBookCodes.size() ).isEqualTo( 1 );
145+
146+
Integer code = rareBookCodes.get( 0 );
147+
assertThat( code ).isEqualTo( RARE_BOOK_CODE );
148+
}
149+
);
150+
151+
scope.inTransaction(
152+
session -> {
153+
List<String> populareBookCodes = session.createQuery(
154+
"select b.edition.code from PopularBook b where b.id = :id",
155+
String.class
156+
).setParameter( "id", POPULAR_BOOK_ID ).list();
157+
158+
assertThat( populareBookCodes.size() ).isEqualTo( 1 );
159+
160+
String code = populareBookCodes.get( 0 );
161+
assertThat( code ).isEqualTo( POPULAR_BOOK_CODE );
162+
}
163+
);
164+
}
165+
166+
@MappedSuperclass
167+
public static class Edition<T> {
168+
private String editorName;
169+
170+
@Column(name = "CODE_COLUMN")
171+
private T code;
172+
173+
public Edition() {
174+
}
175+
176+
public Edition(String editorName, T code) {
177+
this.editorName = editorName;
178+
this.code = code;
179+
}
180+
}
181+
182+
@Embeddable
183+
public static class RareEdition extends Edition<Integer> {
184+
185+
private String state;
186+
187+
public RareEdition() {
188+
}
189+
190+
public RareEdition(String editorName, Integer code, String state) {
191+
super( editorName, code );
192+
this.state = state;
193+
}
194+
}
195+
196+
@Embeddable
197+
public static class PopularEdition extends Edition<String> {
198+
199+
@Column(name = "YEAR_COLUMN")
200+
private Integer year;
201+
202+
public PopularEdition() {
203+
}
204+
205+
public PopularEdition(String editorName, String code, Integer year) {
206+
super( editorName, code );
207+
this.year = year;
208+
}
209+
}
210+
211+
212+
@Entity(name = "Base")
213+
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
214+
public static class Base {
215+
@Id
216+
private Long id;
217+
218+
public Base() {
219+
}
220+
221+
public Base(Long id) {
222+
this.id = id;
223+
}
224+
}
225+
226+
227+
@MappedSuperclass
228+
public static abstract class Book<T extends Edition> extends Base {
229+
@Embedded
230+
private T edition;
231+
232+
public Book() {
233+
}
234+
235+
public Book(Long id, T edition) {
236+
super( id );
237+
this.edition = edition;
238+
}
239+
}
240+
241+
242+
@Entity(name = "PopularBook")
243+
public static class PopularBook extends Book<PopularEdition> {
244+
245+
public PopularBook() {
246+
}
247+
248+
public PopularBook(Long id, PopularEdition edition) {
249+
super( id, edition );
250+
}
251+
}
252+
253+
@Entity(name = "RareBook")
254+
public static class RareBook extends Book<RareEdition> {
255+
256+
public RareBook() {
257+
}
258+
259+
public RareBook(Long id, RareEdition edition) {
260+
super( id, edition );
261+
}
262+
263+
}
264+
}

0 commit comments

Comments
 (0)