Skip to content

Commit 2c0aee0

Browse files
committed
HHH-19792 Add test for issue
1 parent d5a6b59 commit 2c0aee0

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.cid;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Embeddable;
9+
import jakarta.persistence.EmbeddedId;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.Table;
12+
import org.hibernate.cfg.AvailableSettings;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.Jira;
15+
import org.hibernate.testing.orm.junit.ServiceRegistry;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.hibernate.testing.orm.junit.Setting;
19+
import org.junit.jupiter.api.AfterAll;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
@DomainModel(
29+
annotatedClasses = {
30+
EmbeddedIdInParameterBindingTest.Delivery.class
31+
}
32+
)
33+
@SessionFactory
34+
@ServiceRegistry(settings = @Setting(name = AvailableSettings.DIALECT_NATIVE_PARAM_MARKERS, value = "true"))
35+
@Jira( "HHH-19792" )
36+
public class EmbeddedIdInParameterBindingTest {
37+
38+
LocationId verbania = new LocationId( "Italy", "Verbania" );
39+
Delivery pizza = new Delivery( verbania, "Pizza Margherita" );
40+
41+
LocationId hallein = new LocationId( "Austria", "Hallein" );
42+
Delivery schnitzel = new Delivery( hallein, "Wiener Schnitzel" );
43+
44+
@BeforeAll
45+
public void setUp(SessionFactoryScope scope) {
46+
scope.inTransaction(
47+
session -> {
48+
session.persist( pizza );
49+
session.persist( schnitzel );
50+
}
51+
);
52+
}
53+
54+
@AfterAll
55+
public void tearDown(SessionFactoryScope scope) {
56+
scope.getSessionFactory().getSchemaManager().truncate();
57+
}
58+
59+
@Test
60+
public void testQueryWithWhereClauseContainingInOperator(SessionFactoryScope scope) {
61+
scope.inTransaction(
62+
session -> {
63+
List<Delivery> deliveries = session.createQuery( "from Delivery d where d.locationId in (?1)",
64+
Delivery.class )
65+
.setParameter( 1, verbania )
66+
.getResultList();
67+
assertThat( deliveries.size() ).isEqualTo( 1 );
68+
assertThat( deliveries ).contains( pizza );
69+
}
70+
);
71+
}
72+
73+
@Test
74+
public void testQueryWithWhereClauseContainingInOperatorWithListOfParametersValues(SessionFactoryScope scope) {
75+
scope.inTransaction(
76+
session -> {
77+
List<Delivery> deliveries = session.createQuery( "from Delivery d where d.locationId in ?1",
78+
Delivery.class )
79+
.setParameter( 1, List.of( verbania ) )
80+
.getResultList();
81+
assertThat( deliveries.size() ).isEqualTo( 1 );
82+
assertThat( deliveries ).contains( pizza );
83+
}
84+
);
85+
86+
scope.inTransaction(
87+
session -> {
88+
List<Delivery> deliveries = session.createQuery( "from Delivery d where d.locationId in ?1",
89+
Delivery.class )
90+
.setParameter( 1, List.of( verbania, schnitzel ) )
91+
.getResultList();
92+
assertThat( deliveries.size() ).isEqualTo( 2 );
93+
assertThat( deliveries ).contains( pizza );
94+
assertThat( deliveries ).contains( schnitzel );
95+
}
96+
);
97+
}
98+
99+
@Test
100+
public void testMoreComplexWhereClause(SessionFactoryScope scope) {
101+
scope.inTransaction(
102+
session -> {
103+
List<Delivery> deliveries = session.createQuery(
104+
"from Delivery d where d.field2Copy = ?3 and d.locationId in (?1,?2) and d.field = ?3",
105+
Delivery.class )
106+
.setParameter( 1, verbania )
107+
.setParameter( 2, hallein )
108+
.setParameter( 3, "Pizza Margherita" )
109+
.getResultList();
110+
assertThat( deliveries.size() ).isEqualTo( 1 );
111+
assertThat( deliveries ).contains( pizza );
112+
}
113+
);
114+
}
115+
116+
@Test
117+
public void testQueryWithWhereClauseContainingInOperatorAndTwoParamaters(SessionFactoryScope scope) {
118+
scope.inTransaction(
119+
session -> {
120+
List<Delivery> deliveries = session.createQuery( "from Delivery d where d.locationId in (?1,?2)",
121+
Delivery.class )
122+
.setParameter( 1, verbania )
123+
.setParameter( 2, hallein )
124+
.getResultList();
125+
assertThat( deliveries.size() ).isEqualTo( 2 );
126+
assertThat( deliveries ).contains( pizza );
127+
assertThat( deliveries ).contains( schnitzel );
128+
}
129+
);
130+
}
131+
132+
@Entity(name = "Delivery")
133+
@Table(name = "Delivery")
134+
public static class Delivery {
135+
136+
@EmbeddedId
137+
private LocationId locationId;
138+
139+
@Column(name = "field")
140+
private String field;
141+
142+
@Column(name = "field2")
143+
private String field2Copy;
144+
145+
public Delivery() {
146+
}
147+
148+
public Delivery(LocationId locationId, String field) {
149+
this.locationId = locationId;
150+
this.field = field;
151+
this.field2Copy = field;
152+
}
153+
154+
public LocationId getLocationId() {
155+
return locationId;
156+
}
157+
158+
public void setLocationId(LocationId locationId) {
159+
this.locationId = locationId;
160+
}
161+
162+
public String getField() {
163+
return field;
164+
}
165+
166+
public void setField(String field) {
167+
this.field = field;
168+
}
169+
170+
@Override
171+
public String toString() {
172+
return locationId + ":" + field;
173+
}
174+
175+
@Override
176+
public boolean equals(Object o) {
177+
if ( this == o ) {
178+
return true;
179+
}
180+
if ( o == null || getClass() != o.getClass() ) {
181+
return false;
182+
}
183+
Delivery table = (Delivery) o;
184+
return Objects.equals( locationId, table.locationId ) && Objects.equals( field, table.field );
185+
}
186+
187+
@Override
188+
public int hashCode() {
189+
return Objects.hash( locationId, field );
190+
}
191+
}
192+
193+
194+
@Embeddable
195+
public static class LocationId {
196+
197+
@Column(name = "sp_country")
198+
private String country;
199+
200+
@Column(name = "sp_city")
201+
private String city;
202+
203+
public LocationId(String country, String city) {
204+
this.country = country;
205+
this.city = city;
206+
}
207+
208+
public LocationId() {
209+
}
210+
211+
public String getCountry() {
212+
return country;
213+
}
214+
215+
public String getCity() {
216+
return city;
217+
}
218+
219+
public void setCountry(String country) {
220+
this.country = country;
221+
}
222+
223+
public void setCity(String city) {
224+
this.city = city;
225+
}
226+
227+
@Override
228+
public String toString() {
229+
return "[" + country + "-" + city + "]";
230+
}
231+
232+
@Override
233+
public boolean equals(Object o) {
234+
if ( this == o ) {
235+
return true;
236+
}
237+
if ( o == null || getClass() != o.getClass() ) {
238+
return false;
239+
}
240+
LocationId tableId = (LocationId) o;
241+
return Objects.equals( country, tableId.country ) && Objects.equals( city, tableId.city );
242+
}
243+
244+
@Override
245+
public int hashCode() {
246+
return Objects.hash( country, city );
247+
}
248+
}
249+
}

0 commit comments

Comments
 (0)