Skip to content

Commit c4f62e9

Browse files
schaudermp911de
authored andcommitted
Return List instead of Iterable in JDBC Repositories and JdbcAggregateOperations.
Closes #1623 Original pull request: #1897
1 parent 4d5a382 commit c4f62e9

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.core;
1717

18+
import java.util.List;
1819
import java.util.Optional;
1920

2021
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
@@ -58,7 +59,7 @@ public interface JdbcAggregateOperations {
5859
* resulting update does not update any rows.
5960
* @since 3.0
6061
*/
61-
<T> Iterable<T> saveAll(Iterable<T> instances);
62+
<T> List<T> saveAll(Iterable<T> instances);
6263

6364
/**
6465
* Dedicated insert function. This skips the test if the aggregate root is new and makes an insert.
@@ -103,7 +104,7 @@ public interface JdbcAggregateOperations {
103104
* @return the saved instances.
104105
* @since 3.1
105106
*/
106-
<T> Iterable<T> updateAll(Iterable<T> instances);
107+
<T> List<T> updateAll(Iterable<T> instances);
107108

108109
/**
109110
* Counts the number of aggregates of a given type.
@@ -162,7 +163,7 @@ public interface JdbcAggregateOperations {
162163
* @param <T> the type of the aggregate roots. Must not be {@code null}.
163164
* @return Guaranteed to be not {@code null}.
164165
*/
165-
<T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType);
166+
<T> List<T> findAllById(Iterable<?> ids, Class<T> domainType);
166167

167168
/**
168169
* Load all aggregates of a given type.
@@ -171,7 +172,7 @@ public interface JdbcAggregateOperations {
171172
* @param <T> the type of the aggregate roots. Must not be {@code null}.
172173
* @return Guaranteed to be not {@code null}.
173174
*/
174-
<T> Iterable<T> findAll(Class<T> domainType);
175+
<T> List<T> findAll(Class<T> domainType);
175176

176177
/**
177178
* Load all aggregates of a given type, sorted.
@@ -182,7 +183,7 @@ public interface JdbcAggregateOperations {
182183
* @return Guaranteed to be not {@code null}.
183184
* @since 2.0
184185
*/
185-
<T> Iterable<T> findAll(Class<T> domainType, Sort sort);
186+
<T> List<T> findAll(Class<T> domainType, Sort sort);
186187

187188
/**
188189
* Load a page of (potentially sorted) aggregates of a given type.
@@ -207,15 +208,15 @@ public interface JdbcAggregateOperations {
207208
<T> Optional<T> findOne(Query query, Class<T> domainType);
208209

209210
/**
210-
* Execute a {@code SELECT} query and convert the resulting items to a {@link Iterable} that is sorted.
211+
* Execute a {@code SELECT} query and convert the resulting items to a {@link List} that is sorted.
211212
*
212213
* @param query must not be {@literal null}.
213214
* @param domainType the entity type must not be {@literal null}.
214215
* @return a non-null sorted list with all the matching results.
215216
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
216217
* @since 3.0
217218
*/
218-
<T> Iterable<T> findAll(Query query, Class<T> domainType);
219+
<T> List<T> findAll(Query query, Class<T> domainType);
219220

220221
/**
221222
* Returns a {@link Page} of entities matching the given {@link Query}. In case no match could be found, an empty

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.data.relational.core.mapping.event.*;
5353
import org.springframework.data.relational.core.query.Query;
5454
import org.springframework.data.support.PageableExecutionUtils;
55+
import org.springframework.data.util.Streamable;
5556
import org.springframework.lang.Nullable;
5657
import org.springframework.util.Assert;
5758
import org.springframework.util.ClassUtils;
@@ -171,7 +172,7 @@ public <T> T save(T instance) {
171172
}
172173

173174
@Override
174-
public <T> Iterable<T> saveAll(Iterable<T> instances) {
175+
public <T> List<T> saveAll(Iterable<T> instances) {
175176

176177
Assert.notNull(instances, "Aggregate instances must not be null");
177178

@@ -204,7 +205,7 @@ public <T> T insert(T instance) {
204205
}
205206

206207
@Override
207-
public <T> Iterable<T> insertAll(Iterable<T> instances) {
208+
public <T> List<T> insertAll(Iterable<T> instances) {
208209

209210
Assert.notNull(instances, "Aggregate instances must not be null");
210211

@@ -239,7 +240,7 @@ public <T> T update(T instance) {
239240
}
240241

241242
@Override
242-
public <T> Iterable<T> updateAll(Iterable<T> instances) {
243+
public <T> List<T> updateAll(Iterable<T> instances) {
243244

244245
Assert.notNull(instances, "Aggregate instances must not be null");
245246

@@ -298,7 +299,7 @@ public <T> T findById(Object id, Class<T> domainType) {
298299
}
299300

300301
@Override
301-
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
302+
public <T> List<T> findAll(Class<T> domainType, Sort sort) {
302303

303304
Assert.notNull(domainType, "Domain type must not be null");
304305

@@ -323,8 +324,13 @@ public <T> Optional<T> findOne(Query query, Class<T> domainType) {
323324
}
324325

325326
@Override
326-
public <T> Iterable<T> findAll(Query query, Class<T> domainType) {
327-
return accessStrategy.findAll(query, domainType);
327+
public <T> List<T> findAll(Query query, Class<T> domainType) {
328+
329+
Iterable<T> all = accessStrategy.findAll(query, domainType);
330+
if (all instanceof List<T> list) {
331+
return list;
332+
}
333+
return Streamable.of(all).toList();
328334
}
329335

330336
@Override
@@ -337,7 +343,7 @@ public <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable)
337343
}
338344

339345
@Override
340-
public <T> Iterable<T> findAll(Class<T> domainType) {
346+
public <T> List<T> findAll(Class<T> domainType) {
341347

342348
Assert.notNull(domainType, "Domain type must not be null");
343349

@@ -346,7 +352,7 @@ public <T> Iterable<T> findAll(Class<T> domainType) {
346352
}
347353

348354
@Override
349-
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
355+
public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType) {
350356

351357
Assert.notNull(ids, "Ids must not be null");
352358
Assert.notNull(domainType, "Domain type must not be null");
@@ -607,7 +613,7 @@ private MutableAggregateChange<?> createDeletingChange(Class<?> domainType) {
607613
return aggregateChange;
608614
}
609615

610-
private <T> Iterable<T> triggerAfterConvert(Iterable<T> all) {
616+
private <T> List<T> triggerAfterConvert(Iterable<T> all) {
611617

612618
List<T> result = new ArrayList<>();
613619

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.support;
1717

18+
import java.util.List;
1819
import java.util.Optional;
1920
import java.util.function.Function;
2021

@@ -30,6 +31,7 @@
3031
import org.springframework.data.repository.PagingAndSortingRepository;
3132
import org.springframework.data.repository.query.FluentQuery;
3233
import org.springframework.data.repository.query.QueryByExampleExecutor;
34+
import org.springframework.data.util.Streamable;
3335
import org.springframework.transaction.annotation.Transactional;
3436
import org.springframework.util.Assert;
3537

@@ -70,7 +72,7 @@ public <S extends T> S save(S instance) {
7072

7173
@Transactional
7274
@Override
73-
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
75+
public <S extends T> List<S> saveAll(Iterable<S> entities) {
7476
return entityOperations.saveAll(entities);
7577
}
7678

@@ -85,12 +87,12 @@ public boolean existsById(ID id) {
8587
}
8688

8789
@Override
88-
public Iterable<T> findAll() {
90+
public List<T> findAll() {
8991
return entityOperations.findAll(entity.getType());
9092
}
9193

9294
@Override
93-
public Iterable<T> findAllById(Iterable<ID> ids) {
95+
public List<T> findAllById(Iterable<ID> ids) {
9496
return entityOperations.findAllById(ids, entity.getType());
9597
}
9698

@@ -130,7 +132,7 @@ public void deleteAll() {
130132
}
131133

132134
@Override
133-
public Iterable<T> findAll(Sort sort) {
135+
public List<T> findAll(Sort sort) {
134136
return entityOperations.findAll(entity.getType(), sort);
135137
}
136138

@@ -148,15 +150,15 @@ public <S extends T> Optional<S> findOne(Example<S> example) {
148150
}
149151

150152
@Override
151-
public <S extends T> Iterable<S> findAll(Example<S> example) {
153+
public <S extends T> List<S> findAll(Example<S> example) {
152154

153155
Assert.notNull(example, "Example must not be null");
154156

155157
return findAll(example, Sort.unsorted());
156158
}
157159

158160
@Override
159-
public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
161+
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
160162

161163
Assert.notNull(example, "Example must not be null");
162164
Assert.notNull(sort, "Sort must not be null");

0 commit comments

Comments
 (0)