From ad5890bef1e5647cdbf98403731404aa8f3b0149 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 25 Sep 2024 08:47:32 +0200 Subject: [PATCH 1/3] 1623-return-lists - Prepare branch --- pom.xml | 2 +- spring-data-jdbc-distribution/pom.xml | 2 +- spring-data-jdbc/pom.xml | 4 ++-- spring-data-r2dbc/pom.xml | 4 ++-- spring-data-relational/pom.xml | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 335f5969b87..ebb3a170b5c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-relational-parent - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT pom Spring Data Relational Parent diff --git a/spring-data-jdbc-distribution/pom.xml b/spring-data-jdbc-distribution/pom.xml index 84eeb178e07..3a145aec740 100644 --- a/spring-data-jdbc-distribution/pom.xml +++ b/spring-data-jdbc-distribution/pom.xml @@ -14,7 +14,7 @@ org.springframework.data spring-data-relational-parent - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT ../pom.xml diff --git a/spring-data-jdbc/pom.xml b/spring-data-jdbc/pom.xml index 266af71b96c..532c4867404 100644 --- a/spring-data-jdbc/pom.xml +++ b/spring-data-jdbc/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-jdbc - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT Spring Data JDBC Spring Data module for JDBC repositories. @@ -15,7 +15,7 @@ org.springframework.data spring-data-relational-parent - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT diff --git a/spring-data-r2dbc/pom.xml b/spring-data-r2dbc/pom.xml index e335b56501b..cf8a30a3056 100644 --- a/spring-data-r2dbc/pom.xml +++ b/spring-data-r2dbc/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-r2dbc - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT Spring Data R2DBC Spring Data module for R2DBC @@ -15,7 +15,7 @@ org.springframework.data spring-data-relational-parent - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT diff --git a/spring-data-relational/pom.xml b/spring-data-relational/pom.xml index 6be1155d38e..9bbdff0b547 100644 --- a/spring-data-relational/pom.xml +++ b/spring-data-relational/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-relational - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT Spring Data Relational Spring Data Relational support @@ -14,7 +14,7 @@ org.springframework.data spring-data-relational-parent - 3.4.0-SNAPSHOT + 3.4.0-1623-return-lists-SNAPSHOT From dd4da8a30c1913da6995157291606eb350a2553b Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 25 Sep 2024 09:07:04 +0200 Subject: [PATCH 2/3] Repository and template now return Lists. Closes #1623 --- .../data/jdbc/core/JdbcAggregateTemplate.java | 24 ++++++++----- .../support/SimpleJdbcRepository.java | 36 ++++++++++++------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java index ad690dc2738..dbef6d1e1a9 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java @@ -52,6 +52,7 @@ import org.springframework.data.relational.core.mapping.event.*; import org.springframework.data.relational.core.query.Query; import org.springframework.data.support.PageableExecutionUtils; +import org.springframework.data.util.Streamable; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -171,7 +172,7 @@ public T save(T instance) { } @Override - public Iterable saveAll(Iterable instances) { + public List saveAll(Iterable instances) { Assert.notNull(instances, "Aggregate instances must not be null"); @@ -204,7 +205,7 @@ public T insert(T instance) { } @Override - public Iterable insertAll(Iterable instances) { + public List insertAll(Iterable instances) { Assert.notNull(instances, "Aggregate instances must not be null"); @@ -239,7 +240,7 @@ public T update(T instance) { } @Override - public Iterable updateAll(Iterable instances) { + public List updateAll(Iterable instances) { Assert.notNull(instances, "Aggregate instances must not be null"); @@ -298,7 +299,7 @@ public T findById(Object id, Class domainType) { } @Override - public Iterable findAll(Class domainType, Sort sort) { + public List findAll(Class domainType, Sort sort) { Assert.notNull(domainType, "Domain type must not be null"); @@ -323,8 +324,13 @@ public Optional findOne(Query query, Class domainType) { } @Override - public Iterable findAll(Query query, Class domainType) { - return accessStrategy.findAll(query, domainType); + public List findAll(Query query, Class domainType) { + + Iterable all = accessStrategy.findAll(query, domainType); + if (all instanceof List list) { + return list; + } + return Streamable.of(all).toList(); } @Override @@ -337,7 +343,7 @@ public Page findAll(Query query, Class domainType, Pageable pageable) } @Override - public Iterable findAll(Class domainType) { + public List findAll(Class domainType) { Assert.notNull(domainType, "Domain type must not be null"); @@ -346,7 +352,7 @@ public Iterable findAll(Class domainType) { } @Override - public Iterable findAllById(Iterable ids, Class domainType) { + public List findAllById(Iterable ids, Class domainType) { Assert.notNull(ids, "Ids must not be null"); Assert.notNull(domainType, "Domain type must not be null"); @@ -607,7 +613,7 @@ private MutableAggregateChange createDeletingChange(Class domainType) { return aggregateChange; } - private Iterable triggerAfterConvert(Iterable all) { + private List triggerAfterConvert(Iterable all) { List result = new ArrayList<>(); diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java index 628c37afbf5..8a55f9e6686 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java @@ -15,6 +15,7 @@ */ package org.springframework.data.jdbc.repository.support; +import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -30,6 +31,7 @@ import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.FluentQuery; import org.springframework.data.repository.query.QueryByExampleExecutor; +import org.springframework.data.util.Streamable; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; @@ -70,8 +72,8 @@ public S save(S instance) { @Transactional @Override - public Iterable saveAll(Iterable entities) { - return entityOperations.saveAll(entities); + public List saveAll(Iterable entities) { + return asList(entityOperations.saveAll(entities)); } @Override @@ -85,13 +87,13 @@ public boolean existsById(ID id) { } @Override - public Iterable findAll() { - return entityOperations.findAll(entity.getType()); + public List findAll() { + return asList(entityOperations.findAll(entity.getType())); } @Override - public Iterable findAllById(Iterable ids) { - return entityOperations.findAllById(ids, entity.getType()); + public List findAllById(Iterable ids) { + return asList(entityOperations.findAllById(ids, entity.getType())); } @Override @@ -130,8 +132,8 @@ public void deleteAll() { } @Override - public Iterable findAll(Sort sort) { - return entityOperations.findAll(entity.getType(), sort); + public List findAll(Sort sort) { + return asList(entityOperations.findAll(entity.getType(), sort)); } @Override @@ -148,7 +150,7 @@ public Optional findOne(Example example) { } @Override - public Iterable findAll(Example example) { + public List findAll(Example example) { Assert.notNull(example, "Example must not be null"); @@ -156,13 +158,13 @@ public Iterable findAll(Example example) { } @Override - public Iterable findAll(Example example, Sort sort) { + public List findAll(Example example, Sort sort) { Assert.notNull(example, "Example must not be null"); Assert.notNull(sort, "Sort must not be null"); - return this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort), - example.getProbeType()); + return asList(this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort), + example.getProbeType())); } @Override @@ -200,4 +202,14 @@ public R findBy(Example example, Function List asList(Iterable iterable) { + + if (iterable instanceof List list) { + return list; + } + return Streamable.of(iterable).stream().toList(); + } + } From 3753f63d85d777c28a5e92e13833dc31fd9d6c60 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 25 Sep 2024 09:34:19 +0200 Subject: [PATCH 3/3] JdbcAggregateOperations returns List as well. --- .../jdbc/core/JdbcAggregateOperations.java | 15 +++++++------ .../support/SimpleJdbcRepository.java | 22 +++++-------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java index c0c9caf997b..89d60baa39c 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java @@ -15,6 +15,7 @@ */ package org.springframework.data.jdbc.core; +import java.util.List; import java.util.Optional; import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException; @@ -58,7 +59,7 @@ public interface JdbcAggregateOperations { * resulting update does not update any rows. * @since 3.0 */ - Iterable saveAll(Iterable instances); + List saveAll(Iterable instances); /** * Dedicated insert function. This skips the test if the aggregate root is new and makes an insert. @@ -103,7 +104,7 @@ public interface JdbcAggregateOperations { * @return the saved instances. * @since 3.1 */ - Iterable updateAll(Iterable instances); + List updateAll(Iterable instances); /** * Counts the number of aggregates of a given type. @@ -162,7 +163,7 @@ public interface JdbcAggregateOperations { * @param the type of the aggregate roots. Must not be {@code null}. * @return Guaranteed to be not {@code null}. */ - Iterable findAllById(Iterable ids, Class domainType); + List findAllById(Iterable ids, Class domainType); /** * Load all aggregates of a given type. @@ -171,7 +172,7 @@ public interface JdbcAggregateOperations { * @param the type of the aggregate roots. Must not be {@code null}. * @return Guaranteed to be not {@code null}. */ - Iterable findAll(Class domainType); + List findAll(Class domainType); /** * Load all aggregates of a given type, sorted. @@ -182,7 +183,7 @@ public interface JdbcAggregateOperations { * @return Guaranteed to be not {@code null}. * @since 2.0 */ - Iterable findAll(Class domainType, Sort sort); + List findAll(Class domainType, Sort sort); /** * Load a page of (potentially sorted) aggregates of a given type. @@ -207,7 +208,7 @@ public interface JdbcAggregateOperations { Optional findOne(Query query, Class domainType); /** - * Execute a {@code SELECT} query and convert the resulting items to a {@link Iterable} that is sorted. + * Execute a {@code SELECT} query and convert the resulting items to a {@link List} that is sorted. * * @param query must not be {@literal null}. * @param domainType the entity type must not be {@literal null}. @@ -215,7 +216,7 @@ public interface JdbcAggregateOperations { * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found. * @since 3.0 */ - Iterable findAll(Query query, Class domainType); + List findAll(Query query, Class domainType); /** * Returns a {@link Page} of entities matching the given {@link Query}. In case no match could be found, an empty diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java index 8a55f9e6686..08d4eb4f464 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java @@ -73,7 +73,7 @@ public S save(S instance) { @Transactional @Override public List saveAll(Iterable entities) { - return asList(entityOperations.saveAll(entities)); + return entityOperations.saveAll(entities); } @Override @@ -88,12 +88,12 @@ public boolean existsById(ID id) { @Override public List findAll() { - return asList(entityOperations.findAll(entity.getType())); + return entityOperations.findAll(entity.getType()); } @Override public List findAllById(Iterable ids) { - return asList(entityOperations.findAllById(ids, entity.getType())); + return entityOperations.findAllById(ids, entity.getType()); } @Override @@ -133,7 +133,7 @@ public void deleteAll() { @Override public List findAll(Sort sort) { - return asList(entityOperations.findAll(entity.getType(), sort)); + return entityOperations.findAll(entity.getType(), sort); } @Override @@ -163,8 +163,8 @@ public List findAll(Example example, Sort sort) { Assert.notNull(example, "Example must not be null"); Assert.notNull(sort, "Sort must not be null"); - return asList(this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort), - example.getProbeType())); + return this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort), + example.getProbeType()); } @Override @@ -202,14 +202,4 @@ public R findBy(Example example, Function List asList(Iterable iterable) { - - if (iterable instanceof List list) { - return list; - } - return Streamable.of(iterable).stream().toList(); - } - }