Skip to content

Commit 0a62375

Browse files
committed
[hibernate#2060] Add test for ClassCastException when using embeddable ids
1 parent 077cf11 commit 0a62375

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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;
7+
8+
import java.util.ArrayList;
9+
import java.util.Collection;
10+
import java.util.List;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import io.vertx.junit5.VertxTestContext;
16+
import jakarta.persistence.Column;
17+
import jakarta.persistence.Embeddable;
18+
import jakarta.persistence.EmbeddedId;
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.FetchType;
21+
import jakarta.persistence.JoinColumn;
22+
import jakarta.persistence.ManyToOne;
23+
import jakarta.persistence.MappedSuperclass;
24+
import jakarta.persistence.OneToMany;
25+
import jakarta.persistence.Table;
26+
27+
public class EmbeddedIdWithManyTest extends BaseReactiveTest {
28+
29+
@Override
30+
protected Collection<Class<?>> annotatedEntities() {
31+
return List.of( Flower.class, Fruit.class );
32+
}
33+
34+
@BeforeEach
35+
public void populateDb(VertxTestContext context) {
36+
Seed seed1 = new Seed( 1 );
37+
Flower rose = new Flower( seed1, "Rose" );
38+
39+
Fruit cherry = new Fruit( seed1, "Cherry" );
40+
cherry.addFriend( rose );
41+
42+
Seed seed2 = new Seed( 2 );
43+
Flower sunflower = new Flower( seed2, "Sunflower" );
44+
45+
Fruit apple = new Fruit( seed2, "Apple" );
46+
apple.addFriend( sunflower );
47+
48+
Seed seed3 = new Seed( 3 );
49+
Flower chrysanthemum = new Flower( seed3, "Chrysanthemum" );
50+
51+
Fruit banana = new Fruit( seed3, "Banana" );
52+
banana.addFriend( chrysanthemum );
53+
54+
test(
55+
context,
56+
getMutinySessionFactory().withTransaction( s -> s
57+
.persistAll( cherry, rose, sunflower, apple, chrysanthemum, banana )
58+
)
59+
);
60+
}
61+
62+
@Test
63+
public void test(VertxTestContext context) {
64+
test(
65+
context, getMutinySessionFactory().withTransaction( s -> s
66+
.createSelectionQuery( "from Flower", Flower.class )
67+
.getResultList()
68+
)
69+
);
70+
}
71+
72+
@Embeddable
73+
public static class Seed {
74+
75+
@Column(nullable = false, updatable = false)
76+
private Integer id;
77+
78+
public Seed() {
79+
}
80+
81+
public Seed(Integer id) {
82+
this.id = id;
83+
}
84+
85+
public Integer getId() {
86+
return id;
87+
}
88+
89+
public void setId(Integer id) {
90+
this.id = id;
91+
}
92+
}
93+
94+
@MappedSuperclass
95+
public static abstract class Plant {
96+
97+
@EmbeddedId
98+
private Seed seed;
99+
100+
@Column(length = 40, unique = true)
101+
private String name;
102+
103+
protected Plant() {
104+
}
105+
106+
protected Plant(Seed seed, String name) {
107+
this.seed = seed;
108+
this.name = name;
109+
}
110+
111+
public Seed getSeed() {
112+
return seed;
113+
}
114+
115+
public void setSeed(Seed seed) {
116+
this.seed = seed;
117+
}
118+
119+
public String getName() {
120+
return name;
121+
}
122+
123+
public void setName(String name) {
124+
this.name = name;
125+
}
126+
}
127+
128+
@Entity(name = "Fruit")
129+
@Table(name = "known_fruits")
130+
public static class Fruit extends Plant {
131+
132+
@OneToMany(mappedBy = "friend", fetch = FetchType.LAZY)
133+
private List<Flower> friends = new ArrayList<>();
134+
135+
public Fruit() {
136+
}
137+
138+
public Fruit(Seed seed, String name) {
139+
super( seed, name );
140+
}
141+
142+
public void addFriend(Flower flower) {
143+
this.friends.add( flower );
144+
flower.friend = this;
145+
}
146+
147+
public List<Flower> getFriends() {
148+
return friends;
149+
}
150+
151+
@Override
152+
public String toString() {
153+
return "Fruit{" + getSeed().getId() + "," + getName() + '}';
154+
}
155+
156+
}
157+
158+
@Entity(name = "Flower")
159+
@Table(name = "known_flowers")
160+
public static class Flower extends Plant {
161+
162+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
163+
@JoinColumn(name = "friend", referencedColumnName = "id", nullable = false)
164+
private Fruit friend;
165+
166+
public Flower() {
167+
}
168+
169+
public Flower(Seed seed, String name) {
170+
super( seed, name );
171+
}
172+
173+
@Override
174+
public String toString() {
175+
return "Flower{" + getSeed().getId() + "," + getName() + '}';
176+
}
177+
178+
}
179+
180+
}

0 commit comments

Comments
 (0)