Skip to content

Commit a1cbcb1

Browse files
cigalymbladel
authored andcommitted
HHH-18933 Test case using classes from article https://in.relation.to/2024/07/12/embeddable-inheritance/
1 parent a358441 commit a1cbcb1

File tree

1 file changed

+243
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)