Skip to content

Commit 23e1357

Browse files
committed
[hibernate#1979] Test find with @EmbeddedId
1 parent 13405c7 commit 23e1357

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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.Collection;
9+
import java.util.Objects;
10+
import java.util.Set;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import io.vertx.junit5.Timeout;
16+
import io.vertx.junit5.VertxTestContext;
17+
import jakarta.persistence.Column;
18+
import jakarta.persistence.Embeddable;
19+
import jakarta.persistence.EmbeddedId;
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.Table;
22+
23+
import static java.util.concurrent.TimeUnit.MINUTES;
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
@Timeout(value = 10, timeUnit = MINUTES)
27+
public class EmbeddedIdTest extends BaseReactiveTest {
28+
29+
@Override
30+
protected Collection<Class<?>> annotatedEntities() {
31+
return Set.of( Delivery.class );
32+
}
33+
34+
LocationId verbania = new LocationId( "Italy", "Verbania" );
35+
Delivery pizza = new Delivery( verbania, "Pizza Margherita" );
36+
37+
LocationId hallein = new LocationId( "Austria", "Hallein" );
38+
Delivery schnitzel = new Delivery( hallein, "Wiener Schnitzel" );
39+
40+
@BeforeEach
41+
public void populateDb(VertxTestContext context) {
42+
test( context, getMutinySessionFactory().withTransaction( s -> s.persistAll( pizza, schnitzel ) ) );
43+
}
44+
45+
@Test
46+
public void testFindSingleId(VertxTestContext context) {
47+
test( context, getMutinySessionFactory().withTransaction( s -> s.find( Delivery.class, verbania ) )
48+
.invoke( result -> assertThat( result ).isEqualTo( pizza ) )
49+
);
50+
}
51+
52+
@Test
53+
public void testFindMultipleIds(VertxTestContext context) {
54+
test( context, getMutinySessionFactory().withTransaction( s -> s.find( Delivery.class, verbania, hallein ) )
55+
.invoke( result -> assertThat( result ).containsExactlyInAnyOrder( pizza, schnitzel ) )
56+
);
57+
}
58+
59+
@Entity(name = "Delivery")
60+
@Table(name = "Delivery")
61+
public static class Delivery {
62+
63+
@EmbeddedId
64+
private LocationId locationId;
65+
66+
@Column(name = "field")
67+
private String field;
68+
69+
public Delivery() {
70+
}
71+
72+
public Delivery(LocationId locationId, String field) {
73+
this.locationId = locationId;
74+
this.field = field;
75+
}
76+
77+
public LocationId getLocationId() {
78+
return locationId;
79+
}
80+
81+
public void setLocationId(LocationId locationId) {
82+
this.locationId = locationId;
83+
}
84+
85+
public String getField() {
86+
return field;
87+
}
88+
89+
public void setField(String field) {
90+
this.field = field;
91+
}
92+
93+
@Override
94+
public String toString() {
95+
return locationId + ":" + field;
96+
}
97+
98+
@Override
99+
public boolean equals(Object o) {
100+
if ( this == o ) {
101+
return true;
102+
}
103+
if ( o == null || getClass() != o.getClass() ) {
104+
return false;
105+
}
106+
Delivery table = (Delivery) o;
107+
return Objects.equals( locationId, table.locationId ) &&
108+
Objects.equals( field, table.field );
109+
}
110+
111+
@Override
112+
public int hashCode() {
113+
return Objects.hash( locationId, field );
114+
}
115+
}
116+
117+
118+
@Embeddable
119+
public static class LocationId {
120+
121+
@Column(name = "sp_country")
122+
private String country;
123+
124+
@Column(name = "sp_city")
125+
private String city;
126+
127+
public LocationId(String country, String city) {
128+
this.country = country;
129+
this.city = city;
130+
}
131+
132+
public LocationId() {
133+
}
134+
135+
public String getCountry() {
136+
return country;
137+
}
138+
139+
public String getCity() {
140+
return city;
141+
}
142+
143+
public void setCountry(String country) {
144+
this.country = country;
145+
}
146+
147+
public void setCity(String city) {
148+
this.city = city;
149+
}
150+
151+
@Override
152+
public String toString() {
153+
return "[" + country + "-" + city + "]";
154+
}
155+
156+
@Override
157+
public boolean equals(Object o) {
158+
if ( this == o ) {
159+
return true;
160+
}
161+
if ( o == null || getClass() != o.getClass() ) {
162+
return false;
163+
}
164+
LocationId tableId = (LocationId) o;
165+
return Objects.equals( country, tableId.country ) &&
166+
Objects.equals( city, tableId.city );
167+
}
168+
169+
@Override
170+
public int hashCode() {
171+
return Objects.hash( country, city );
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)