Skip to content

Commit eb80974

Browse files
cigalymbellade
authored andcommitted
HHH-18933 Test case using classes from article https://in.relation.to/2024/07/12/embeddable-inheritance/ - showing the issue has been resolved
1 parent ec1188f commit eb80974

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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.AfterAll;
11+
import org.junit.jupiter.api.Test;
12+
13+
import jakarta.persistence.DiscriminatorColumn;
14+
import jakarta.persistence.Embeddable;
15+
import jakarta.persistence.Embedded;
16+
import jakarta.persistence.Entity;
17+
import jakarta.persistence.Id;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.fail;
21+
22+
@DomainModel(
23+
annotatedClasses = {
24+
EmbeddableInheritanceHierarchyOrderTest.Animal.class,
25+
EmbeddableInheritanceHierarchyOrderTest.Cat.class,
26+
EmbeddableInheritanceHierarchyOrderTest.Dog.class,
27+
EmbeddableInheritanceHierarchyOrderTest.Fish.class,
28+
EmbeddableInheritanceHierarchyOrderTest.Mammal.class,
29+
// If Mammal is moved right under Animal (before Dog and Cat), test will pass
30+
EmbeddableInheritanceHierarchyOrderTest.Owner.class
31+
}
32+
)
33+
@SessionFactory
34+
public class EmbeddableInheritanceHierarchyOrderTest {
35+
36+
@AfterAll
37+
static void clean(SessionFactoryScope scope) {
38+
scope.inTransaction( session -> session.createMutationQuery( "delete from Owner" ).executeUpdate() );
39+
}
40+
41+
@Test
42+
public void test(SessionFactoryScope scope) {
43+
scope.inTransaction( session -> {
44+
session.persist( new Owner( 1L, new Animal( 2, "Agapius" ) ) );
45+
session.persist( new Owner( 2L, new Cat( 3, "Bercharius", "Blaesilla" ) ) );
46+
session.persist( new Owner( 3L, new Dog( 4, "Censurius", "Caesarea" ) ) );
47+
session.persist( new Owner( 4L, new Fish( 5, "Dionysius", 3 ) ) );
48+
session.persist( new Owner( 5L, new Mammal( 6, "Epagraphas", "Eanswida" ) ) );
49+
} );
50+
scope.inSession( session -> {
51+
final Owner animalOwner = session.find( Owner.class, 1L );
52+
assertEquals( 2, animalOwner.getPet().getAge() );
53+
assertEquals( "Agapius", animalOwner.getPet().getName() );
54+
55+
final Owner fishOwner = session.find( Owner.class, 4L );
56+
if ( fishOwner.getPet() instanceof Fish ) {
57+
final Fish fish = (Fish) fishOwner.getPet();
58+
assertEquals( 5, fish.getAge() );
59+
assertEquals( "Dionysius", fish.getName() );
60+
assertEquals( 3, fish.getFins() );
61+
}
62+
else {
63+
fail( "Not fish owner" );
64+
}
65+
66+
final Owner mammalOwner = session.find( Owner.class, 5L );
67+
if ( mammalOwner.getPet() instanceof Mammal ) {
68+
final Mammal mammal = (Mammal) mammalOwner.getPet();
69+
assertEquals( 6, mammal.getAge() );
70+
assertEquals( "Epagraphas", mammal.getName() );
71+
assertEquals( "Eanswida", mammal.getMother() );
72+
}
73+
else {
74+
fail( "Not mammal owner" );
75+
}
76+
77+
final Owner catOwner = session.find( Owner.class, 2L );
78+
if ( catOwner.getPet() instanceof Cat ) {
79+
final Cat cat = (Cat) catOwner.getPet();
80+
assertEquals( 3, cat.getAge() );
81+
assertEquals( "Bercharius", cat.getName() );
82+
assertEquals( "Blaesilla", cat.getMother() );
83+
}
84+
else {
85+
fail( "Not cat owner" );
86+
}
87+
88+
final Owner dogOwner = session.find( Owner.class, 3L );
89+
if ( dogOwner.getPet() instanceof Dog ) {
90+
final Dog dog = (Dog) dogOwner.getPet();
91+
assertEquals( 4, dog.getAge() );
92+
assertEquals( "Censurius", dog.getName() );
93+
assertEquals( "Caesarea", dog.getMother() );
94+
}
95+
else {
96+
fail( "Not dog owner" );
97+
}
98+
} );
99+
}
100+
101+
@Embeddable
102+
@DiscriminatorColumn(name = "animal_type", length = 64)
103+
static
104+
class Animal {
105+
private int age;
106+
107+
private String name;
108+
109+
public Animal() {
110+
}
111+
112+
public Animal(int age, String name) {
113+
this.age = age;
114+
this.name = name;
115+
}
116+
117+
public int getAge() {
118+
return age;
119+
}
120+
121+
public void setAge(int age) {
122+
this.age = age;
123+
}
124+
125+
public String getName() {
126+
return name;
127+
}
128+
129+
public void setName(String name) {
130+
this.name = name;
131+
}
132+
}
133+
134+
@Embeddable
135+
static
136+
class Cat extends Mammal {
137+
//private int mouse;
138+
// [...]
139+
140+
141+
public Cat() {
142+
super();
143+
}
144+
145+
public Cat(int age, String name, String mother) {
146+
super( age, name, mother );
147+
}
148+
}
149+
150+
@Embeddable
151+
static
152+
class Dog extends Mammal {
153+
//private int bone;
154+
// [...]
155+
156+
public Dog() {
157+
}
158+
159+
public Dog(int age, String name, String mother) {
160+
super( age, name, mother );
161+
}
162+
}
163+
164+
@Embeddable
165+
static
166+
class Fish extends Animal {
167+
private int fins;
168+
169+
public Fish() {
170+
}
171+
172+
public Fish(int age, String name, int fins) {
173+
super( age, name );
174+
this.fins = fins;
175+
}
176+
177+
public int getFins() {
178+
return fins;
179+
}
180+
181+
public void setFins(int fins) {
182+
this.fins = fins;
183+
}
184+
}
185+
186+
@Embeddable
187+
static
188+
class Mammal extends Animal {
189+
private String mother;
190+
191+
public Mammal() {
192+
}
193+
194+
public Mammal(int age, String name, String mother) {
195+
super( age, name );
196+
this.mother = mother;
197+
}
198+
199+
public String getMother() {
200+
return mother;
201+
}
202+
203+
public void setMother(String mother) {
204+
this.mother = mother;
205+
}
206+
}
207+
208+
@Entity(name = "Owner")
209+
static
210+
class Owner {
211+
@Id
212+
private Long id;
213+
214+
@Embedded
215+
private Animal pet;
216+
217+
public Owner() {
218+
}
219+
220+
public Owner(Long id, Animal pet) {
221+
this.id = id;
222+
this.pet = pet;
223+
}
224+
225+
public Long getId() {
226+
return id;
227+
}
228+
229+
public void setId(Long id) {
230+
this.id = id;
231+
}
232+
233+
public Animal getPet() {
234+
return pet;
235+
}
236+
237+
public void setPet(Animal pet) {
238+
this.pet = pet;
239+
}
240+
}
241+
}

0 commit comments

Comments
 (0)