Skip to content

Commit a358441

Browse files
cigalymbladel
authored andcommitted
HHH-18933 Test case adapted from reproducer mentioned in https://hibernate.atlassian.net/browse/HHH-18933
1 parent 0c72972 commit a358441

File tree

1 file changed

+195
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)