|
24 | 24 | import static org.springframework.test.context.TestExecutionListeners.MergeMode.*;
|
25 | 25 |
|
26 | 26 | import java.time.LocalDateTime;
|
| 27 | +import java.util.*; |
27 | 28 | import java.util.ArrayList;
|
28 |
| -import java.util.Collections; |
29 |
| -import java.util.HashMap; |
30 |
| -import java.util.HashSet; |
31 |
| -import java.util.Iterator; |
32 |
| -import java.util.List; |
33 |
| -import java.util.Map; |
34 |
| -import java.util.Objects; |
35 |
| -import java.util.Set; |
36 | 29 | import java.util.function.Function;
|
37 | 30 | import java.util.stream.IntStream;
|
38 | 31 |
|
|
44 | 37 | import org.springframework.context.annotation.Bean;
|
45 | 38 | import org.springframework.context.annotation.Configuration;
|
46 | 39 | import org.springframework.context.annotation.Import;
|
| 40 | +import org.springframework.dao.IncorrectResultSizeDataAccessException; |
47 | 41 | import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
|
48 | 42 | import org.springframework.dao.OptimisticLockingFailureException;
|
49 | 43 | import org.springframework.data.annotation.Id;
|
|
67 | 61 | import org.springframework.data.relational.core.mapping.MappedCollection;
|
68 | 62 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
69 | 63 | import org.springframework.data.relational.core.mapping.Table;
|
| 64 | +import org.springframework.data.relational.core.query.Criteria; |
| 65 | +import org.springframework.data.relational.core.query.CriteriaDefinition; |
| 66 | +import org.springframework.data.relational.core.query.Query; |
70 | 67 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
71 | 68 | import org.springframework.test.context.ActiveProfiles;
|
72 | 69 | import org.springframework.test.context.ContextConfiguration;
|
@@ -233,6 +230,62 @@ void findAllById() {
|
233 | 230 | .containsExactlyInAnyOrder(tuple(entity.id, "entity"), tuple(yetAnother.id, "yetAnother"));
|
234 | 231 | }
|
235 | 232 |
|
| 233 | + @Test // GH-1601 |
| 234 | + void findAllByQuery() { |
| 235 | + |
| 236 | + template.save(SimpleListParent.of("one", "one_1")); |
| 237 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 238 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 239 | + |
| 240 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").is(two.id)); |
| 241 | + Query query = Query.query(criteria); |
| 242 | + Iterable<SimpleListParent> reloadedById = template.findAll(query, SimpleListParent.class); |
| 243 | + |
| 244 | + assertThat(reloadedById).extracting(e -> e.id, e -> e.content.size()).containsExactly(tuple(two.id, 2)); |
| 245 | + } |
| 246 | + |
| 247 | + @Test // GH-1601 |
| 248 | + void findOneByQuery() { |
| 249 | + |
| 250 | + template.save(SimpleListParent.of("one", "one_1")); |
| 251 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 252 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 253 | + |
| 254 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").is(two.id)); |
| 255 | + Query query = Query.query(criteria); |
| 256 | + Optional<SimpleListParent> reloadedById = template.findOne(query, SimpleListParent.class); |
| 257 | + |
| 258 | + assertThat(reloadedById).get().extracting(e -> e.id, e -> e.content.size()).containsExactly(two.id, 2); |
| 259 | + } |
| 260 | + |
| 261 | + @Test // GH-1601 |
| 262 | + void findOneByQueryNothingFound() { |
| 263 | + |
| 264 | + template.save(SimpleListParent.of("one", "one_1")); |
| 265 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 266 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 267 | + |
| 268 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").is(4711)); |
| 269 | + Query query = Query.query(criteria); |
| 270 | + Optional<SimpleListParent> reloadedById = template.findOne(query, SimpleListParent.class); |
| 271 | + |
| 272 | + assertThat(reloadedById).isEmpty(); |
| 273 | + } |
| 274 | + |
| 275 | + @Test // GH-1601 |
| 276 | + void findOneByQueryToManyResults() { |
| 277 | + |
| 278 | + template.save(SimpleListParent.of("one", "one_1")); |
| 279 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 280 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 281 | + |
| 282 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").not(two.id)); |
| 283 | + Query query = Query.query(criteria); |
| 284 | + |
| 285 | + assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class) |
| 286 | + .isThrownBy(() -> template.findOne(query, SimpleListParent.class)); |
| 287 | + } |
| 288 | + |
236 | 289 | @Test // DATAJDBC-112
|
237 | 290 | @EnabledOnFeature(SUPPORTS_QUOTED_IDS)
|
238 | 291 | void saveAndLoadAnEntityWithReferencedEntityById() {
|
@@ -1276,6 +1329,29 @@ static class ChildNoId {
|
1276 | 1329 | private String content;
|
1277 | 1330 | }
|
1278 | 1331 |
|
| 1332 | + @SuppressWarnings("unused") |
| 1333 | + static class SimpleListParent { |
| 1334 | + |
| 1335 | + @Id private Long id; |
| 1336 | + String name; |
| 1337 | + List<ElementNoId> content = new ArrayList<>(); |
| 1338 | + |
| 1339 | + static SimpleListParent of(String name, String... contents) { |
| 1340 | + |
| 1341 | + SimpleListParent parent = new SimpleListParent(); |
| 1342 | + parent.name = name; |
| 1343 | + |
| 1344 | + for (String content : contents) { |
| 1345 | + |
| 1346 | + ElementNoId element = new ElementNoId(); |
| 1347 | + element.content = content; |
| 1348 | + parent.content.add(element); |
| 1349 | + } |
| 1350 | + |
| 1351 | + return parent; |
| 1352 | + } |
| 1353 | + } |
| 1354 | + |
1279 | 1355 | @Table("LIST_PARENT")
|
1280 | 1356 | @SuppressWarnings("unused")
|
1281 | 1357 | static class ListParent {
|
|
0 commit comments