Skip to content

Commit 1aafc3c

Browse files
committed
HHH-15552 Add test for issue
1 parent cfc9b9c commit 1aafc3c

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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.annotations.embeddables;
8+
9+
import java.util.List;
10+
11+
import org.hibernate.testing.TestForIssue;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
import jakarta.persistence.Column;
19+
import jakarta.persistence.Embeddable;
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.Id;
22+
import jakarta.persistence.MappedSuperclass;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
@DomainModel(
27+
annotatedClasses = {
28+
EmbeddableAndMappedSuperClassWithGenericsTest.PopularBook.class,
29+
EmbeddableAndMappedSuperClassWithGenericsTest.RareBook.class
30+
}
31+
)
32+
@SessionFactory
33+
@TestForIssue(jiraKey = "HHH-15552")
34+
public class EmbeddableAndMappedSuperClassWithGenericsTest {
35+
36+
private final static long POPULAR_BOOK_ID = 1l;
37+
private final static long RARE_BOOK_ID = 2l;
38+
39+
@BeforeEach
40+
public void setUp(SessionFactoryScope scope) {
41+
scope.inTransaction(
42+
session -> {
43+
Edition popularEdition = new Edition( "Popular" );
44+
PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition, "POP" );
45+
46+
Edition rareEdition = new Edition( "Rare" );
47+
RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition, 123 );
48+
49+
session.persist( popularBook );
50+
session.persist( rareBook );
51+
}
52+
);
53+
}
54+
55+
@Test
56+
public void test(SessionFactoryScope scope) {
57+
scope.inTransaction(
58+
session -> {
59+
List<Integer> rareBookCodes = session.createQuery(
60+
"select b.code from RareBook b where b.id = :id",
61+
Integer.class
62+
).setParameter( "id", RARE_BOOK_ID ).list();
63+
64+
assertThat( rareBookCodes.size() ).isEqualTo( 1 );
65+
66+
Integer code = rareBookCodes.get( 0 );
67+
assertThat( code ).isEqualTo( 123 );
68+
}
69+
);
70+
71+
scope.inTransaction(
72+
session -> {
73+
List<String> populareBookCodes = session.createQuery(
74+
"select b.code from PopularBook b where b.id = :id",
75+
String.class
76+
).setParameter( "id", POPULAR_BOOK_ID ).list();
77+
78+
assertThat( populareBookCodes.size() ).isEqualTo( 1 );
79+
80+
String code = populareBookCodes.get( 0 );
81+
assertThat( code ).isEqualTo( "POP" );
82+
}
83+
);
84+
}
85+
86+
@Embeddable
87+
public static class Edition {
88+
private String name;
89+
90+
public Edition() {
91+
}
92+
93+
public Edition(String name) {
94+
this.name = name;
95+
}
96+
}
97+
98+
@MappedSuperclass
99+
public static abstract class Book<T> {
100+
private Edition edition;
101+
102+
@Column(name = "CODE_COLUMN")
103+
private T code;
104+
105+
public Book() {
106+
}
107+
108+
public Book(Edition edition, T code) {
109+
this.edition = edition;
110+
this.code = code;
111+
}
112+
}
113+
114+
115+
@Entity(name = "PopularBook")
116+
public static class PopularBook extends Book<String> {
117+
118+
@Id
119+
private Long id;
120+
121+
public PopularBook() {
122+
}
123+
124+
public PopularBook(Long id, Edition edition, String code) {
125+
super( edition, code );
126+
this.id = id;
127+
}
128+
}
129+
130+
@Entity(name = "RareBook")
131+
public static class RareBook extends Book<Integer> {
132+
133+
@Id
134+
private Long id;
135+
136+
public RareBook() {
137+
}
138+
139+
public RareBook(Long id, Edition edition, Integer code) {
140+
super( edition, code );
141+
this.id = id;
142+
}
143+
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package org.hibernate.orm.test.annotations.embeddables;
2+
3+
import java.util.List;
4+
5+
import org.hibernate.testing.orm.junit.DomainModel;
6+
import org.hibernate.testing.orm.junit.SessionFactory;
7+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
import jakarta.persistence.Column;
12+
import jakarta.persistence.DiscriminatorColumn;
13+
import jakarta.persistence.Embeddable;
14+
import jakarta.persistence.Entity;
15+
import jakarta.persistence.Id;
16+
import jakarta.persistence.Inheritance;
17+
import jakarta.persistence.InheritanceType;
18+
import jakarta.persistence.MappedSuperclass;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
@DomainModel(
23+
annotatedClasses = {
24+
EmbeddableWithGenericAndMappedSuperClassTest.PopularBook.class,
25+
EmbeddableWithGenericAndMappedSuperClassTest.RareBook.class
26+
}
27+
)
28+
@SessionFactory
29+
public class EmbeddableWithGenericAndMappedSuperClassTest {
30+
31+
private final static long POPULAR_BOOK_ID = 1l;
32+
private final static String POPULAR_BOOK_CODE = "POP";
33+
private final static long RARE_BOOK_ID = 2l;
34+
private final static Integer RARE_BOOK_CODE = 123;
35+
36+
@BeforeEach
37+
public void setUp(SessionFactoryScope scope) {
38+
scope.inTransaction(
39+
session -> {
40+
Edition popularEdition = new Edition<>( "Popular", POPULAR_BOOK_CODE );
41+
PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition );
42+
43+
Edition rareEdition = new Edition<>( "Rare", RARE_BOOK_CODE );
44+
RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition );
45+
46+
session.persist( popularBook );
47+
session.persist( rareBook );
48+
}
49+
);
50+
}
51+
52+
@Test
53+
public void test(SessionFactoryScope scope) {
54+
scope.inTransaction(
55+
session -> {
56+
List<Integer> rareBookCodes = session.createQuery(
57+
"select b.edition.code from RareBook b where b.id = :id",
58+
Integer.class
59+
).setParameter( "id", RARE_BOOK_ID ).list();
60+
61+
assertThat( rareBookCodes.size() ).isEqualTo( 1 );
62+
63+
Integer code = rareBookCodes.get( 0 );
64+
assertThat( code ).isEqualTo( RARE_BOOK_CODE );
65+
}
66+
);
67+
68+
scope.inTransaction(
69+
session -> {
70+
List<Integer> rareBookCodes = session.createQuery(
71+
"select c.code from Base b left join treat(b as RareBook).edition c where b.id = :id",
72+
Integer.class
73+
).setParameter( "id", RARE_BOOK_ID ).list();
74+
75+
assertThat( rareBookCodes.size() ).isEqualTo( 1 );
76+
77+
Integer code = rareBookCodes.get( 0 );
78+
assertThat( code ).isEqualTo( RARE_BOOK_CODE );
79+
}
80+
);
81+
82+
scope.inTransaction(
83+
session -> {
84+
List<String> populareBookCodes = session.createQuery(
85+
"select b.edition.code from PopularBook b where b.id = :id",
86+
String.class
87+
).setParameter( "id", POPULAR_BOOK_ID ).list();
88+
89+
assertThat( populareBookCodes.size() ).isEqualTo( 1 );
90+
91+
String code = populareBookCodes.get( 0 );
92+
assertThat( code ).isEqualTo( POPULAR_BOOK_CODE );
93+
}
94+
);
95+
96+
scope.inTransaction(
97+
session -> {
98+
List<String> rareBookCodes = session.createQuery(
99+
"select c.code from Base b left join treat(b as PopularBook).edition c where b.id = :id",
100+
String.class
101+
).setParameter( "id", POPULAR_BOOK_ID ).list();
102+
103+
assertThat( rareBookCodes.size() ).isEqualTo( 1 );
104+
105+
String code = rareBookCodes.get( 0 );
106+
assertThat( code ).isEqualTo( POPULAR_BOOK_CODE );
107+
}
108+
);
109+
}
110+
111+
@Embeddable
112+
public static class Edition<T> {
113+
private String editorName;
114+
115+
@Column(name = "CODE_COLUMN")
116+
private T code;
117+
118+
public Edition() {
119+
}
120+
121+
public Edition(String editorName, T code) {
122+
this.editorName = editorName;
123+
this.code = code;
124+
}
125+
}
126+
127+
@Entity(name = "Base")
128+
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
129+
@DiscriminatorColumn(name = "PROP_TYPE")
130+
public abstract static class Base {
131+
@Id
132+
private Long id;
133+
134+
135+
public Base() {
136+
}
137+
138+
public Base(Long id) {
139+
this.id = id;
140+
}
141+
}
142+
143+
@MappedSuperclass
144+
public static abstract class Book<T> extends Base {
145+
private Edition<T> edition;
146+
147+
public Book() {
148+
}
149+
150+
public Book(Long id, Edition<T> edition) {
151+
super( id );
152+
this.edition = edition;
153+
}
154+
}
155+
156+
157+
@Entity(name = "PopularBook")
158+
public static class PopularBook extends Book<String> {
159+
160+
161+
public PopularBook() {
162+
}
163+
164+
public PopularBook(Long id, Edition<String> edition) {
165+
super( id, edition );
166+
}
167+
}
168+
169+
@Entity(name = "RareBook")
170+
public static class RareBook extends Book<Integer> {
171+
172+
public RareBook() {
173+
}
174+
175+
public RareBook(Long id, Edition<Integer> edition) {
176+
super( id, edition );
177+
}
178+
179+
}
180+
}

0 commit comments

Comments
 (0)