Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package org.springframework.data.jdbc.core;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.Nullable;

/**
* Specifies a operations one can perform on a database, based on an <em>Domain Type</em>.
*
* @author Jens Schauder
* @author Thomas Lang
* @author Milan Milanov
*/
public interface JdbcAggregateOperations {

Expand Down Expand Up @@ -128,4 +132,24 @@ public interface JdbcAggregateOperations {
* @return whether the aggregate exists.
*/
<T> boolean existsById(Object id, Class<T> domainType);

/**
* Load all aggregates of a given type, sorted.
*
* @param domainType the type of the aggregate roots. Must not be {@code null}.
* @param <T> the type of the aggregate roots. Must not be {@code null}.
* @param sort the sorting information. Must not be {@code null}.
* @return Guaranteed to be not {@code null}.
*/
<T> Iterable<T> findAll(Class<T> domainType, Sort sort);

/**
* Load a page of (potentially sorted) aggregates of a given type.
*
* @param domainType the type of the aggregate roots. Must not be {@code null}.
* @param <T> the type of the aggregate roots. Must not be {@code null}.
* @param pageable the pagination information. Must not be {@code null}.
* @return Guaranteed to be not {@code null}.
*/
<T> Page<T> findAll(Class<T> domainType, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.mapping.IdentifierAccessor;
Expand All @@ -45,6 +51,7 @@
* @author Mark Paluch
* @author Thomas Lang
* @author Christoph Strobl
* @author Milan Milanov
*/
public class JdbcAggregateTemplate implements JdbcAggregateOperations {

Expand Down Expand Up @@ -223,6 +230,35 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
return accessStrategy.existsById(id, domainType);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Sort)
*/
@Override
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {

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

Iterable<T> all = accessStrategy.findAll(domainType, sort);
return triggerAfterLoad(all);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Pageable)
*/
@Override
public <T> Page<T> findAll(Class<T> domainType, Pageable pageable) {

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

Iterable<T> items = triggerAfterLoad(accessStrategy.findAll(domainType, pageable));
long totalCount = accessStrategy.count(domainType);

return new PageImpl<>(StreamSupport.stream(items.spliterator(), false).collect(Collectors.toList()), pageable,
totalCount);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.function.Consumer;
import java.util.function.Function;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.domain.Identifier;
Expand All @@ -33,6 +35,7 @@
* @author Jens Schauder
* @author Mark Paluch
* @author Tyler Van Gorder
* @author Milan Milanov
* @since 1.1
*/
public class CascadingDataAccessStrategy implements DataAccessStrategy {
Expand Down Expand Up @@ -188,6 +191,24 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
return collect(das -> das.existsById(id, domainType));
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Sort)
*/
@Override
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
return collect(das -> das.findAll(domainType, sort));
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Pageable)
*/
@Override
public <T> Iterable<T> findAll(Class<T> domainType, Pageable pageable) {
return collect(das -> das.findAll(domainType, pageable));
}

private <T> T collect(Function<DataAccessStrategy, T> function) {

// Keep <T> as Eclipse fails to compile if <> is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Map;

import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand All @@ -32,6 +34,7 @@
*
* @author Jens Schauder
* @author Tyler Van Gorder
* @author Milan Milanov
*/
public interface DataAccessStrategy extends RelationResolver {

Expand Down Expand Up @@ -215,4 +218,24 @@ default Iterable<Object> findAllByPath(Identifier identifier,
* @return {@code true} if a matching row exists, otherwise {@code false}.
*/
<T> boolean existsById(Object id, Class<T> domainType);

/**
* Loads all entities of the given type, sorted.
*
* @param domainType the type of entities to load. Must not be {@code null}.
* @param <T> the type of entities to load.
* @param sort the sorting information. Must not be {@code null}.
* @return Guaranteed to be not {@code null}.
*/
<T> Iterable<T> findAll(Class<T> domainType, Sort sort);

/**
* Loads all entities of the given type, paged and sorted.
*
* @param domainType the type of entities to load. Must not be {@code null}.
* @param <T> the type of entities to load.
* @param pageable the pagination information. Must not be {@code null}.
* @return Guaranteed to be not {@code null}.
*/
<T> Iterable<T> findAll(Class<T> domainType, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.support.JdbcUtil;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
Expand Down Expand Up @@ -60,6 +62,7 @@
* @author Christoph Strobl
* @author Tom Hombergs
* @author Tyler Van Gorder
* @author Milan Milanov
* @since 1.1
*/
public class DefaultDataAccessStrategy implements DataAccessStrategy {
Expand Down Expand Up @@ -374,6 +377,26 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
return result;
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Sort)
*/
@Override
@SuppressWarnings("unchecked")
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
return operations.query(sql(domainType).getFindAll(sort), (RowMapper<T>) getEntityRowMapper(domainType));
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Pageable)
*/
@Override
@SuppressWarnings("unchecked")
public <T> Iterable<T> findAll(Class<T> domainType, Pageable pageable) {
return operations.query(sql(domainType).getFindAll(pageable), (RowMapper<T>) getEntityRowMapper(domainType));
}

private <S, T> SqlIdentifierParameterSource getParameterSource(@Nullable S instance,
RelationalPersistentEntity<S> persistentEntity, String prefix,
Predicate<RelationalPersistentProperty> skipProperty, IdentifierProcessing identifierProcessing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Map;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.domain.Identifier;
Expand All @@ -29,6 +31,7 @@
*
* @author Jens Schauder
* @author Tyler Van Gorder
* @author Milan Milanov
* @since 1.1
*/
public class DelegatingDataAccessStrategy implements DataAccessStrategy {
Expand Down Expand Up @@ -187,6 +190,24 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
return delegate.existsById(id, domainType);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Sort)
*/
@Override
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
return delegate.findAll(domainType, sort);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jdbc.core.JdbcAggregateOperations#findAll(java.lang.Class, org.springframework.data.domain.Pageable)
*/
@Override
public <T> Iterable<T> findAll(Class<T> domainType, Pageable pageable) {
return delegate.findAll(domainType, pageable);
}

/**
* Must be called exactly once before calling any of the other methods.
*
Expand Down
Loading