Skip to content

Commit f2c2222

Browse files
committed
[#2284] Test Many-to-one with MapsId and EmbeddedId
1 parent b53a164 commit f2c2222

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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.hibernate.annotations.EmbeddedColumnNaming;
13+
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
import io.vertx.junit5.VertxTestContext;
18+
import jakarta.persistence.CascadeType;
19+
import jakarta.persistence.Column;
20+
import jakarta.persistence.Embeddable;
21+
import jakarta.persistence.Embedded;
22+
import jakarta.persistence.EmbeddedId;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.EnumType;
25+
import jakarta.persistence.Enumerated;
26+
import jakarta.persistence.FetchType;
27+
import jakarta.persistence.ManyToOne;
28+
import jakarta.persistence.MapsId;
29+
import jakarta.persistence.OneToMany;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class ManyToOneMapsIdAndEmbeddedIdTest extends BaseReactiveTest {
34+
35+
@Override
36+
protected Collection<Class<?>> annotatedEntities() {
37+
return List.of( Link.class, GPSPoint.class, NetPoint.class );
38+
}
39+
40+
@BeforeEach
41+
public void populateDb(VertxTestContext context) {
42+
NetPointKey startKey = new NetPointKey( 1, NetPointType.STOP_POINT );
43+
NetPointKey endKey = new NetPointKey( 2, NetPointType.STOP_POINT );
44+
LinkKey linkKey = new LinkKey( startKey, endKey, "123" );
45+
46+
final NetPoint start = new NetPoint();
47+
fillWithBogusValues( start );
48+
start.key = startKey;
49+
50+
final NetPoint end = new NetPoint();
51+
fillWithBogusValues( end );
52+
end.key = endKey;
53+
54+
final Link link = new Link();
55+
link.key = linkKey;
56+
link.addPoint( new GPSPoint( new GPSPointKey( linkKey, 0 ), link, 1, 1, 1 ) );
57+
link.addPoint( new GPSPoint( new GPSPointKey( linkKey, 1 ), link, 1, 1, 1 ) );
58+
link.addPoint( new GPSPoint( new GPSPointKey( linkKey, 2 ), link, 1, 1, 1 ) );
59+
60+
test(
61+
context, getMutinySessionFactory().withTransaction( session -> session
62+
.persistAll( start, end, link ) )
63+
);
64+
}
65+
66+
@Test
67+
public void test(VertxTestContext context) {
68+
test( context, getMutinySessionFactory()
69+
.withTransaction( session -> session
70+
.createQuery( "from Link", Link.class ).getResultList() )
71+
.invoke( links -> assertThat( links ).hasSize( 1 ) )
72+
);
73+
}
74+
75+
void fillWithBogusValues(NetPoint start) {
76+
start.gpsLatitude = 1;
77+
start.gpsLongitude = 1;
78+
start.operatingDepartmentId = "123";
79+
start.operatingDepartmentShortName = "123 - 123";
80+
}
81+
82+
@Entity(name = "GPSPoint")
83+
public static class GPSPoint {
84+
85+
@EmbeddedId
86+
public GPSPointKey key;
87+
88+
@ManyToOne
89+
@MapsId("link")
90+
public Link link;
91+
92+
@Column(nullable = false)
93+
public Integer latitude;
94+
95+
@Column(nullable = false)
96+
public Integer longitude;
97+
98+
@Column(nullable = false)
99+
public Integer distance;
100+
101+
public GPSPoint() {
102+
}
103+
104+
public GPSPoint(GPSPointKey key, Link link, Integer latitude, Integer longitude, Integer distance) {
105+
this.key = key;
106+
this.link = link;
107+
this.latitude = latitude;
108+
this.longitude = longitude;
109+
this.distance = distance;
110+
}
111+
}
112+
113+
@Embeddable
114+
public record GPSPointKey(
115+
@Embedded
116+
LinkKey link,
117+
118+
Integer position
119+
) {
120+
121+
}
122+
123+
@Entity(name = "Link")
124+
public static class Link {
125+
126+
@EmbeddedId
127+
public LinkKey key;
128+
129+
@OneToMany(mappedBy = "link", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
130+
public List<GPSPoint> gpsPoints = new ArrayList<>();
131+
132+
public void addPoint(GPSPoint point) {
133+
gpsPoints.add( point );
134+
point.link = this;
135+
}
136+
}
137+
138+
@Embeddable
139+
public record LinkKey(
140+
@Embedded
141+
@EmbeddedColumnNaming("start_%s")
142+
NetPointKey start,
143+
144+
@Embedded
145+
@EmbeddedColumnNaming("end_%s")
146+
NetPointKey end,
147+
148+
String operatingDepartmentId
149+
) {
150+
151+
}
152+
153+
@Embeddable
154+
public record NetPointKey(
155+
Integer id,
156+
157+
@Enumerated(EnumType.ORDINAL)
158+
NetPointType type
159+
) {
160+
161+
}
162+
163+
@Entity(name = "NetPoint")
164+
public static class NetPoint {
165+
166+
@EmbeddedId
167+
public NetPointKey key;
168+
169+
@Column(nullable = false)
170+
public String operatingDepartmentId;
171+
172+
@Column(nullable = false)
173+
public String operatingDepartmentShortName;
174+
175+
@Column(nullable = false)
176+
public Integer gpsLatitude;
177+
178+
@Column(nullable = false)
179+
public Integer gpsLongitude;
180+
}
181+
182+
public enum NetPointType {
183+
@Deprecated UNSPECIFIED,
184+
STOP_POINT,
185+
DEPOT_POINT,
186+
BEACON,
187+
SECTION_POINT
188+
}
189+
}

0 commit comments

Comments
 (0)