|
23 | 23 | import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;
|
24 | 24 |
|
25 | 25 | import java.time.LocalDateTime;
|
| 26 | +import java.util.*; |
26 | 27 | import java.util.ArrayList;
|
27 |
| -import java.util.Collections; |
28 |
| -import java.util.HashMap; |
29 |
| -import java.util.HashSet; |
30 |
| -import java.util.Iterator; |
31 |
| -import java.util.List; |
32 |
| -import java.util.Map; |
33 |
| -import java.util.Objects; |
34 |
| -import java.util.Set; |
35 | 28 | import java.util.function.Function;
|
36 | 29 | import java.util.stream.IntStream;
|
37 | 30 |
|
|
42 | 35 | import org.springframework.context.annotation.Bean;
|
43 | 36 | import org.springframework.context.annotation.Configuration;
|
44 | 37 | import org.springframework.context.annotation.Import;
|
| 38 | +import org.springframework.dao.IncorrectResultSizeDataAccessException; |
45 | 39 | import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
|
46 | 40 | import org.springframework.dao.OptimisticLockingFailureException;
|
47 | 41 | import org.springframework.data.annotation.Id;
|
|
64 | 58 | import org.springframework.data.relational.core.mapping.MappedCollection;
|
65 | 59 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
66 | 60 | import org.springframework.data.relational.core.mapping.Table;
|
| 61 | +import org.springframework.data.relational.core.query.Criteria; |
| 62 | +import org.springframework.data.relational.core.query.CriteriaDefinition; |
| 63 | +import org.springframework.data.relational.core.query.Query; |
67 | 64 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
68 | 65 | import org.springframework.test.context.ActiveProfiles;
|
69 | 66 |
|
@@ -223,6 +220,62 @@ void findAllById() {
|
223 | 220 | .containsExactlyInAnyOrder(tuple(entity.id, "entity"), tuple(yetAnother.id, "yetAnother"));
|
224 | 221 | }
|
225 | 222 |
|
| 223 | + @Test // GH-1601 |
| 224 | + void findAllByQuery() { |
| 225 | + |
| 226 | + template.save(SimpleListParent.of("one", "one_1")); |
| 227 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 228 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 229 | + |
| 230 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").is(two.id)); |
| 231 | + Query query = Query.query(criteria); |
| 232 | + Iterable<SimpleListParent> reloadedById = template.findAll(query, SimpleListParent.class); |
| 233 | + |
| 234 | + assertThat(reloadedById).extracting(e -> e.id, e -> e.content.size()).containsExactly(tuple(two.id, 2)); |
| 235 | + } |
| 236 | + |
| 237 | + @Test // GH-1601 |
| 238 | + void findOneByQuery() { |
| 239 | + |
| 240 | + template.save(SimpleListParent.of("one", "one_1")); |
| 241 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 242 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 243 | + |
| 244 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").is(two.id)); |
| 245 | + Query query = Query.query(criteria); |
| 246 | + Optional<SimpleListParent> reloadedById = template.findOne(query, SimpleListParent.class); |
| 247 | + |
| 248 | + assertThat(reloadedById).get().extracting(e -> e.id, e -> e.content.size()).containsExactly(two.id, 2); |
| 249 | + } |
| 250 | + |
| 251 | + @Test // GH-1601 |
| 252 | + void findOneByQueryNothingFound() { |
| 253 | + |
| 254 | + template.save(SimpleListParent.of("one", "one_1")); |
| 255 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 256 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 257 | + |
| 258 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").is(4711)); |
| 259 | + Query query = Query.query(criteria); |
| 260 | + Optional<SimpleListParent> reloadedById = template.findOne(query, SimpleListParent.class); |
| 261 | + |
| 262 | + assertThat(reloadedById).isEmpty(); |
| 263 | + } |
| 264 | + |
| 265 | + @Test // GH-1601 |
| 266 | + void findOneByQueryToManyResults() { |
| 267 | + |
| 268 | + template.save(SimpleListParent.of("one", "one_1")); |
| 269 | + SimpleListParent two = template.save(SimpleListParent.of("two", "two_1", "two_2")); |
| 270 | + template.save(SimpleListParent.of("three", "three_1", "three_2", "three_3")); |
| 271 | + |
| 272 | + CriteriaDefinition criteria = CriteriaDefinition.from(Criteria.where("id").not(two.id)); |
| 273 | + Query query = Query.query(criteria); |
| 274 | + |
| 275 | + assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class) |
| 276 | + .isThrownBy(() -> template.findOne(query, SimpleListParent.class)); |
| 277 | + } |
| 278 | + |
226 | 279 | @Test // DATAJDBC-112
|
227 | 280 | @EnabledOnFeature(SUPPORTS_QUOTED_IDS)
|
228 | 281 | void saveAndLoadAnEntityWithReferencedEntityById() {
|
@@ -1266,6 +1319,29 @@ static class ChildNoId {
|
1266 | 1319 | private String content;
|
1267 | 1320 | }
|
1268 | 1321 |
|
| 1322 | + @SuppressWarnings("unused") |
| 1323 | + static class SimpleListParent { |
| 1324 | + |
| 1325 | + @Id private Long id; |
| 1326 | + String name; |
| 1327 | + List<ElementNoId> content = new ArrayList<>(); |
| 1328 | + |
| 1329 | + static SimpleListParent of(String name, String... contents) { |
| 1330 | + |
| 1331 | + SimpleListParent parent = new SimpleListParent(); |
| 1332 | + parent.name = name; |
| 1333 | + |
| 1334 | + for (String content : contents) { |
| 1335 | + |
| 1336 | + ElementNoId element = new ElementNoId(); |
| 1337 | + element.content = content; |
| 1338 | + parent.content.add(element); |
| 1339 | + } |
| 1340 | + |
| 1341 | + return parent; |
| 1342 | + } |
| 1343 | + } |
| 1344 | + |
1269 | 1345 | @Table("LIST_PARENT")
|
1270 | 1346 | @SuppressWarnings("unused")
|
1271 | 1347 | static class ListParent {
|
|
0 commit comments