Skip to content

Commit 345191b

Browse files
committed
HHH-19792 Add test for issue
1 parent c496a1b commit 345191b

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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.AfterEach;
20+
import org.junit.jupiter.api.BeforeEach;
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+
EmbeddedIdTest.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 EmbeddedIdTest {
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+
@BeforeEach
45+
public void setUp(SessionFactoryScope scope) {
46+
scope.inTransaction(
47+
session -> {
48+
session.persist( pizza );
49+
session.persist( schnitzel );
50+
}
51+
);
52+
}
53+
54+
@AfterEach
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+
@Test
133+
public void testQueryWithWhereClauseContainingInOperatorAndListOf2alues(SessionFactoryScope scope) {
134+
135+
}
136+
137+
@Entity(name = "Delivery")
138+
@Table(name = "Delivery")
139+
public static class Delivery {
140+
141+
@EmbeddedId
142+
private LocationId locationId;
143+
144+
@Column(name = "field")
145+
private String field;
146+
147+
@Column(name = "field2")
148+
private String field2Copy;
149+
150+
public Delivery() {
151+
}
152+
153+
public Delivery(LocationId locationId, String field) {
154+
this.locationId = locationId;
155+
this.field = field;
156+
this.field2Copy = field;
157+
}
158+
159+
public LocationId getLocationId() {
160+
return locationId;
161+
}
162+
163+
public void setLocationId(LocationId locationId) {
164+
this.locationId = locationId;
165+
}
166+
167+
public String getField() {
168+
return field;
169+
}
170+
171+
public void setField(String field) {
172+
this.field = field;
173+
}
174+
175+
@Override
176+
public String toString() {
177+
return locationId + ":" + field;
178+
}
179+
180+
@Override
181+
public boolean equals(Object o) {
182+
if ( this == o ) {
183+
return true;
184+
}
185+
if ( o == null || getClass() != o.getClass() ) {
186+
return false;
187+
}
188+
Delivery table = (Delivery) o;
189+
return Objects.equals( locationId, table.locationId ) && Objects.equals( field, table.field );
190+
}
191+
192+
@Override
193+
public int hashCode() {
194+
return Objects.hash( locationId, field );
195+
}
196+
}
197+
198+
199+
@Embeddable
200+
public static class LocationId {
201+
202+
@Column(name = "sp_country")
203+
private String country;
204+
205+
@Column(name = "sp_city")
206+
private String city;
207+
208+
public LocationId(String country, String city) {
209+
this.country = country;
210+
this.city = city;
211+
}
212+
213+
public LocationId() {
214+
}
215+
216+
public String getCountry() {
217+
return country;
218+
}
219+
220+
public String getCity() {
221+
return city;
222+
}
223+
224+
public void setCountry(String country) {
225+
this.country = country;
226+
}
227+
228+
public void setCity(String city) {
229+
this.city = city;
230+
}
231+
232+
@Override
233+
public String toString() {
234+
return "[" + country + "-" + city + "]";
235+
}
236+
237+
@Override
238+
public boolean equals(Object o) {
239+
if ( this == o ) {
240+
return true;
241+
}
242+
if ( o == null || getClass() != o.getClass() ) {
243+
return false;
244+
}
245+
LocationId tableId = (LocationId) o;
246+
return Objects.equals( country, tableId.country ) && Objects.equals( city, tableId.city );
247+
}
248+
249+
@Override
250+
public int hashCode() {
251+
return Objects.hash( country, city );
252+
}
253+
}
254+
}

0 commit comments

Comments
 (0)