Skip to content

Commit

Permalink
Return List instead of Iterable in JDBC Repositories and `JdbcAgg…
Browse files Browse the repository at this point in the history
…regateOperations`.

Closes #1623
Original pull request: #1897
  • Loading branch information
schauder authored and mp911de committed Oct 1, 2024
1 parent 4d5a382 commit c4f62e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.core;

import java.util.List;
import java.util.Optional;

import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
Expand Down Expand Up @@ -58,7 +59,7 @@ public interface JdbcAggregateOperations {
* resulting update does not update any rows.
* @since 3.0
*/
<T> Iterable<T> saveAll(Iterable<T> instances);
<T> List<T> saveAll(Iterable<T> instances);

/**
* Dedicated insert function. This skips the test if the aggregate root is new and makes an insert.
Expand Down Expand Up @@ -103,7 +104,7 @@ public interface JdbcAggregateOperations {
* @return the saved instances.
* @since 3.1
*/
<T> Iterable<T> updateAll(Iterable<T> instances);
<T> List<T> updateAll(Iterable<T> instances);

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

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

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

/**
* Load a page of (potentially sorted) aggregates of a given type.
Expand All @@ -207,15 +208,15 @@ public interface JdbcAggregateOperations {
<T> Optional<T> findOne(Query query, Class<T> 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}.
* @return a non-null sorted list with all the matching results.
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
* @since 3.0
*/
<T> Iterable<T> findAll(Query query, Class<T> domainType);
<T> List<T> findAll(Query query, Class<T> domainType);

/**
* Returns a {@link Page} of entities matching the given {@link Query}. In case no match could be found, an empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,7 +172,7 @@ public <T> T save(T instance) {
}

@Override
public <T> Iterable<T> saveAll(Iterable<T> instances) {
public <T> List<T> saveAll(Iterable<T> instances) {

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

Expand Down Expand Up @@ -204,7 +205,7 @@ public <T> T insert(T instance) {
}

@Override
public <T> Iterable<T> insertAll(Iterable<T> instances) {
public <T> List<T> insertAll(Iterable<T> instances) {

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

Expand Down Expand Up @@ -239,7 +240,7 @@ public <T> T update(T instance) {
}

@Override
public <T> Iterable<T> updateAll(Iterable<T> instances) {
public <T> List<T> updateAll(Iterable<T> instances) {

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

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

@Override
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
public <T> List<T> findAll(Class<T> domainType, Sort sort) {

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

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

@Override
public <T> Iterable<T> findAll(Query query, Class<T> domainType) {
return accessStrategy.findAll(query, domainType);
public <T> List<T> findAll(Query query, Class<T> domainType) {

Iterable<T> all = accessStrategy.findAll(query, domainType);
if (all instanceof List<T> list) {
return list;
}
return Streamable.of(all).toList();
}

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

@Override
public <T> Iterable<T> findAll(Class<T> domainType) {
public <T> List<T> findAll(Class<T> domainType) {

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

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

@Override
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType) {

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

private <T> Iterable<T> triggerAfterConvert(Iterable<T> all) {
private <T> List<T> triggerAfterConvert(Iterable<T> all) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.repository.support;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

Expand All @@ -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;

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

@Transactional
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
public <S extends T> List<S> saveAll(Iterable<S> entities) {
return entityOperations.saveAll(entities);
}

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

@Override
public Iterable<T> findAll() {
public List<T> findAll() {
return entityOperations.findAll(entity.getType());
}

@Override
public Iterable<T> findAllById(Iterable<ID> ids) {
public List<T> findAllById(Iterable<ID> ids) {
return entityOperations.findAllById(ids, entity.getType());
}

Expand Down Expand Up @@ -130,7 +132,7 @@ public void deleteAll() {
}

@Override
public Iterable<T> findAll(Sort sort) {
public List<T> findAll(Sort sort) {
return entityOperations.findAll(entity.getType(), sort);
}

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

@Override
public <S extends T> Iterable<S> findAll(Example<S> example) {
public <S extends T> List<S> findAll(Example<S> example) {

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

return findAll(example, Sort.unsorted());
}

@Override
public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {

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

0 comments on commit c4f62e9

Please sign in to comment.