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 c4b9b3bf..cdba2ed3 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 @@ -152,6 +152,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { */ @Override public long count(Class domainType) { + + Assert.notNull(domainType, "Domain type must not be null"); + return accessStrategy.count(domainType); } @@ -162,6 +165,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public T findById(Object id, Class domainType) { + Assert.notNull(id, "Id must not be null"); + Assert.notNull(domainType, "Domain type must not be null"); + T entity = accessStrategy.findById(id, domainType); if (entity != null) { publishAfterLoad(id, entity); @@ -175,6 +181,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { */ @Override public boolean existsById(Object id, Class domainType) { + + Assert.notNull(id, "Id must not be null"); + Assert.notNull(domainType, "Domain type must not be null"); + return accessStrategy.existsById(id, domainType); } @@ -185,6 +195,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public Iterable findAll(Class domainType) { + Assert.notNull(domainType, "Domain type must not be null"); + Iterable all = accessStrategy.findAll(domainType); publishAfterLoad(all); return all; @@ -197,6 +209,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public Iterable findAllById(Iterable ids, Class domainType) { + Assert.notNull(ids, "Ids must not be null"); + Assert.notNull(domainType, "Domain type must not be null"); + Iterable allById = accessStrategy.findAllById(ids, domainType); publishAfterLoad(allById); return allById; @@ -209,6 +224,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public void delete(S aggregateRoot, Class domainType) { + Assert.notNull(aggregateRoot, "Aggregate root must not be null"); + Assert.notNull(domainType, "Domain type must not be null"); + IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType) .getIdentifierAccessor(aggregateRoot); @@ -221,6 +239,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { */ @Override public void deleteById(Object id, Class domainType) { + + Assert.notNull(id, "Id must not be null"); + Assert.notNull(domainType, "Domain type must not be null"); + deleteTree(id, null, domainType); } @@ -231,6 +253,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public void deleteAll(Class domainType) { + Assert.notNull(domainType, "Domain type must not be null"); + AggregateChange change = createDeletingChange(domainType); change.executeWith(interpreter, context, converter); } diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java index 6c7f2bde..ceb4fcfa 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java @@ -18,6 +18,7 @@ package org.springframework.data.jdbc.core.convert; import java.sql.JDBCType; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -241,6 +242,10 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { @SuppressWarnings("unchecked") public Iterable findAllById(Iterable ids, Class domainType) { + if (!ids.iterator().hasNext()) { + return Collections.emptyList(); + } + RelationalPersistentProperty idProperty = getRequiredPersistentEntity(domainType).getRequiredIdProperty(); MapSqlParameterSource parameterSource = new MapSqlParameterSource(); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java index 8c35785f..332794d7 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java @@ -35,7 +35,6 @@ import org.junit.Assume; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; @@ -597,6 +596,24 @@ public class JdbcAggregateTemplateIntegrationTests { }); } + @Test // DATAJDBC-378 + public void findAllByIdMustNotAcceptNullArgumentForType() { + + assertThatThrownBy(() -> template.findAllById(singleton(23L), null)).isInstanceOf(IllegalArgumentException.class); + } + + @Test // DATAJDBC-378 + public void findAllByIdMustNotAcceptNullArgumentForIds() { + + assertThatThrownBy(() -> template.findAllById(null, LegoSet.class)).isInstanceOf(IllegalArgumentException.class); + } + + @Test // DATAJDBC-378 + public void findAllByIdWithEmpthListMustReturnEmptyResult() { + + assertThat(template.findAllById(emptyList(), LegoSet.class)).isEmpty(); + } + private static NoIdMapChain4 createNoIdMapTree() { NoIdMapChain4 chain4 = new NoIdMapChain4();