Skip to content

Commit ec1188f

Browse files
cigalymbellade
authored andcommitted
HHH-18933 Test case adapted from reproducer mentioned in https://hibernate.atlassian.net/browse/HHH-18933 - showing the issue has been resolved
1 parent f899246 commit ec1188f

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.inheritance;
6+
7+
import org.hibernate.testing.orm.junit.DomainModel;
8+
import org.hibernate.testing.orm.junit.SessionFactory;
9+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
import jakarta.persistence.Embeddable;
14+
import jakarta.persistence.Embedded;
15+
import jakarta.persistence.Entity;
16+
import jakarta.persistence.EntityManagerFactory;
17+
import jakarta.persistence.GeneratedValue;
18+
import jakarta.persistence.Id;
19+
import jakarta.persistence.Inheritance;
20+
import jakarta.persistence.InheritanceType;
21+
import jakarta.persistence.TypedQuery;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
26+
@DomainModel(
27+
annotatedClasses = {
28+
HierarchyOrderTest.DerOA.class,
29+
HierarchyOrderTest.DerDA.class,
30+
HierarchyOrderTest.DerDB.class,
31+
HierarchyOrderTest.DerOB.class,
32+
HierarchyOrderTest.BaseD.class,
33+
HierarchyOrderTest.BaseO.class
34+
}
35+
)
36+
@SessionFactory
37+
class HierarchyOrderTest {
38+
39+
private EntityManagerFactory emf;
40+
private DerOA deroa;
41+
private DerOB derob;
42+
43+
@BeforeEach
44+
void setUp() {
45+
DerDB derba1 = new DerDB( 5 );
46+
DerDA derda1 = new DerDA( "1", "abase" );
47+
deroa = new DerOA( derda1 );
48+
derob = new DerOB( derba1 );
49+
// emf = buildEntityManagerFactory();
50+
}
51+
52+
@Test
53+
void testBaseProperty(SessionFactoryScope scope) {
54+
scope.inSession( em -> {
55+
em.getTransaction().begin();
56+
em.persist( deroa );
57+
em.persist( derob );
58+
em.getTransaction().commit();
59+
Integer ida = deroa.getId();
60+
Integer idb = derob.getId();
61+
em.clear();
62+
TypedQuery<DerOA> qa = em.createQuery( "select o from DerOA o where o.id =:id", DerOA.class );
63+
qa.setParameter( "id", ida );
64+
DerOA deroain = qa.getSingleResult();
65+
assertEquals( "abase", deroain.derda.baseprop );
66+
} );
67+
}
68+
69+
@Test
70+
void testDerivedProperty(SessionFactoryScope scope) {
71+
scope.inSession( em -> {
72+
em.getTransaction().begin();
73+
em.persist( deroa );
74+
em.persist( derob );
75+
em.getTransaction().commit();
76+
Integer idb = derob.getId();
77+
em.clear();
78+
79+
TypedQuery<DerOB> qb = em.createQuery( "select o from DerOB o where o.id =:id", DerOB.class );
80+
qb.setParameter( "id", idb );
81+
DerOB derobin = qb.getSingleResult();
82+
assertNotNull( derobin );
83+
assertEquals( 5, derobin.derdb().b );
84+
} );
85+
}
86+
87+
/*
88+
* Created on 03/12/2024 by Paul Harrison ([email protected]).
89+
*/
90+
@Entity(name = "DerOA")
91+
public static class DerOA extends BaseO {
92+
public DerOA(DerDA derda) {
93+
this.derda = derda;
94+
}
95+
96+
@Embedded
97+
// @AttributeOverrides({
98+
// @AttributeOverride(name="a",column = @Column(name = "da"))
99+
// })
100+
public BaseD derda;
101+
102+
public DerOA() {
103+
104+
}
105+
}
106+
107+
/*
108+
* Created on 03/12/2024 by Paul Harrison ([email protected]).
109+
*/
110+
@Embeddable
111+
public static class DerDB extends BaseD {
112+
public DerDB(int b) {
113+
this.b = b;
114+
}
115+
116+
public int b;
117+
118+
public DerDB() {
119+
120+
}
121+
}
122+
123+
/*
124+
* Created on 03/12/2024 by Paul Harrison ([email protected]).
125+
*/
126+
@Embeddable
127+
public static class DerDA extends BaseD {
128+
public DerDA(String a, String bprop) {
129+
super( bprop );
130+
this.a = a;
131+
}
132+
133+
public String a;
134+
135+
public DerDA() {
136+
137+
}
138+
}
139+
140+
/*
141+
* Created on 03/12/2024 by Paul Harrison ([email protected]).
142+
*/
143+
@Embeddable
144+
public abstract static class BaseD { //TODO would really like this to be abstract
145+
public String baseprop;
146+
147+
public BaseD(String baseprop) {
148+
this.baseprop = baseprop;
149+
}
150+
151+
public BaseD() {
152+
153+
}
154+
155+
public String getBaseprop() {
156+
return baseprop;
157+
}
158+
159+
}
160+
161+
@Entity(name = "BaseO")
162+
@Inheritance(strategy = InheritanceType.JOINED)
163+
public abstract static class BaseO {
164+
@Id
165+
@GeneratedValue
166+
private Integer id;
167+
168+
public Integer getId() {
169+
return id;
170+
}
171+
}
172+
173+
/*
174+
* Created on 03/12/2024 by Paul Harrison ([email protected]).
175+
*/
176+
@Entity(name = "DerOB")
177+
public static class DerOB extends BaseO {
178+
public DerOB(DerDB derdb) {
179+
this.derdb = derdb;
180+
}
181+
182+
@Embedded
183+
BaseD derdb;
184+
185+
public DerOB() {
186+
187+
}
188+
189+
public DerDB derdb() {
190+
return (DerDB) derdb;
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)