Skip to content

Commit dd4da8a

Browse files
committed
Repository and template now return Lists.
Closes #1623
1 parent ad5890b commit dd4da8a

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

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

+15-9
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

+24-12
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,8 +72,8 @@ public <S extends T> S save(S instance) {
7072

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

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

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

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

9799
@Override
@@ -130,8 +132,8 @@ public void deleteAll() {
130132
}
131133

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

137139
@Override
@@ -148,21 +150,21 @@ 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");
163165

164-
return this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort),
165-
example.getProbeType());
166+
return asList(this.entityOperations.findAll(this.exampleMapper.getMappedExample(example).sort(sort),
167+
example.getProbeType()));
166168
}
167169

168170
@Override
@@ -200,4 +202,14 @@ public <S extends T, R> R findBy(Example<S> example, Function<FluentQuery.Fetcha
200202

201203
return queryFunction.apply(fluentQuery);
202204
}
205+
206+
207+
private <S extends T> List<S> asList(Iterable<S> iterable) {
208+
209+
if (iterable instanceof List<S> list) {
210+
return list;
211+
}
212+
return Streamable.of(iterable).stream().toList();
213+
}
214+
203215
}

0 commit comments

Comments
 (0)