Skip to content

Commit cb0a429

Browse files
committed
[#1598] Add test for joined inheritance with ManyToOne
1 parent 371f800 commit cb0a429

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.issue;
7+
8+
import java.util.Collection;
9+
import java.util.Objects;
10+
import java.util.Set;
11+
import java.util.concurrent.CompletionStage;
12+
13+
import org.hibernate.reactive.BaseReactiveTest;
14+
import org.hibernate.reactive.util.impl.CompletionStages;
15+
16+
import org.junit.Test;
17+
18+
import io.vertx.ext.unit.TestContext;
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.FetchType;
21+
import jakarta.persistence.GeneratedValue;
22+
import jakarta.persistence.Id;
23+
import jakarta.persistence.Inheritance;
24+
import jakarta.persistence.InheritanceType;
25+
import jakarta.persistence.ManyToOne;
26+
import jakarta.persistence.Table;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.atIndex;
30+
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
31+
32+
public class JoinedSubclassInheritanceWithManyToOneTest extends BaseReactiveTest {
33+
34+
@Override
35+
protected Collection<Class<?>> annotatedEntities() {
36+
return Set.of( ItemInstance.class, ClothingItemInstance.class, ClothingItem.class, Item.class );
37+
}
38+
39+
@Override
40+
protected CompletionStage<Void> cleanDb() {
41+
// Cleaning the db after the test fails on CI sometimes.
42+
// In this particular case is not necessary (there is only one test).
43+
return voidFuture();
44+
}
45+
46+
/**
47+
* Test for issue <a href="https://github.com/hibernate/hibernate-reactive/issues/1598">hibernate-reactive#1598</a>
48+
*/
49+
@Test
50+
public void testSubclassListAll(TestContext context) {
51+
ClothingItem clothingItem = new ClothingItem();
52+
clothingItem.name = "Clothing item";
53+
54+
ClothingItemInstance itemInstance = new ClothingItemInstance();
55+
itemInstance.name = "Clothing item instance";
56+
itemInstance.item = clothingItem;
57+
58+
test( context, getMutinySessionFactory()
59+
.withTransaction( session -> session.persistAll( clothingItem, itemInstance ) )
60+
.chain( () -> getMutinySessionFactory()
61+
.withSession( session -> session
62+
.createQuery( "from ItemInstance", ItemInstance.class ).getResultList()
63+
.invoke( list -> assertThat( list ).hasSize( 1 )
64+
.satisfies( entry -> assertThat( entry )
65+
.isInstanceOf( ClothingItemInstance.class )
66+
.hasFieldOrPropertyWithValue( "name", itemInstance.name ),
67+
atIndex( 0 )
68+
) ) )
69+
)
70+
);
71+
}
72+
73+
74+
@Entity(name = "Item")
75+
@Table(name = "item")
76+
@Inheritance(strategy = InheritanceType.JOINED)
77+
public static abstract class Item {
78+
79+
@Id
80+
@GeneratedValue
81+
public Long id;
82+
83+
public String name;
84+
85+
@Override
86+
public String toString() {
87+
return id + ":" + name;
88+
}
89+
}
90+
91+
@Entity(name = "ClothingItem")
92+
@Table(name = "clothingitem")
93+
public static class ClothingItem extends Item {
94+
}
95+
96+
97+
@Entity(name = "ItemInstance")
98+
@Table(name = "iteminstance")
99+
@Inheritance(strategy = InheritanceType.JOINED)
100+
public static abstract class ItemInstance {
101+
102+
@Id
103+
@GeneratedValue
104+
public Long id;
105+
106+
public String name;
107+
108+
@ManyToOne(fetch = FetchType.EAGER)
109+
public Item item;
110+
111+
@Override
112+
public String toString() {
113+
return getClass() + ":" + id + ":" + name;
114+
}
115+
116+
117+
@Override
118+
public boolean equals(Object o) {
119+
if ( this == o ) {
120+
return true;
121+
}
122+
if ( o == null || getClass() != o.getClass() ) {
123+
return false;
124+
}
125+
ItemInstance that = (ItemInstance) o;
126+
return Objects.equals( name, that.name ) && Objects.equals( item, that.item );
127+
}
128+
129+
@Override
130+
public int hashCode() {
131+
return Objects.hash( name, item );
132+
}
133+
}
134+
135+
136+
@Entity(name = "ClothingItemInstance")
137+
@Table(name = "clothingiteminstance")
138+
public static class ClothingItemInstance extends ItemInstance {
139+
140+
}
141+
}

0 commit comments

Comments
 (0)