Skip to content

Commit 688eeca

Browse files
schauderchristophstrobl
authored andcommitted
DATAJDBC-378 - Proper handling of null and empty collections in JdbcAggregateTemplate.
Original Pull Request: #155
1 parent b93c4c8 commit 688eeca

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 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.Collections;
1819
import java.util.Optional;
1920

2021
import org.springframework.context.ApplicationEventPublisher;
@@ -122,6 +123,9 @@ public <T> T save(T instance) {
122123
*/
123124
@Override
124125
public long count(Class<?> domainType) {
126+
127+
Assert.notNull(domainType, "Domain type must not be null");
128+
125129
return accessStrategy.count(domainType);
126130
}
127131

@@ -132,6 +136,9 @@ public long count(Class<?> domainType) {
132136
@Override
133137
public <T> T findById(Object id, Class<T> domainType) {
134138

139+
Assert.notNull(id, "Id must not be null");
140+
Assert.notNull(domainType, "Domain type must not be null");
141+
135142
T entity = accessStrategy.findById(id, domainType);
136143
if (entity != null) {
137144
publishAfterLoad(id, entity);
@@ -145,6 +152,10 @@ public <T> T findById(Object id, Class<T> domainType) {
145152
*/
146153
@Override
147154
public <T> boolean existsById(Object id, Class<T> domainType) {
155+
156+
Assert.notNull(id, "Id must not be null");
157+
Assert.notNull(domainType, "Domain type must not be null");
158+
148159
return accessStrategy.existsById(id, domainType);
149160
}
150161

@@ -155,6 +166,8 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
155166
@Override
156167
public <T> Iterable<T> findAll(Class<T> domainType) {
157168

169+
Assert.notNull(domainType, "Domain type must not be null");
170+
158171
Iterable<T> all = accessStrategy.findAll(domainType);
159172
publishAfterLoad(all);
160173
return all;
@@ -167,6 +180,13 @@ public <T> Iterable<T> findAll(Class<T> domainType) {
167180
@Override
168181
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
169182

183+
Assert.notNull(ids, "Ids must not be null");
184+
Assert.notNull(domainType, "Domain type must not be null");
185+
186+
if (!ids.iterator().hasNext()) {
187+
return Collections.emptyList();
188+
}
189+
170190
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
171191
publishAfterLoad(allById);
172192
return allById;
@@ -179,6 +199,9 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
179199
@Override
180200
public <S> void delete(S aggregateRoot, Class<S> domainType) {
181201

202+
Assert.notNull(aggregateRoot, "Aggregate root must not be null");
203+
Assert.notNull(domainType, "Domain type must not be null");
204+
182205
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
183206
.getIdentifierAccessor(aggregateRoot);
184207

@@ -191,6 +214,10 @@ public <S> void delete(S aggregateRoot, Class<S> domainType) {
191214
*/
192215
@Override
193216
public <S> void deleteById(Object id, Class<S> domainType) {
217+
218+
Assert.notNull(id, "Id must not be null");
219+
Assert.notNull(domainType, "Domain type must not be null");
220+
194221
deleteTree(id, null, domainType);
195222
}
196223

@@ -201,6 +228,8 @@ public <S> void deleteById(Object id, Class<S> domainType) {
201228
@Override
202229
public void deleteAll(Class<?> domainType) {
203230

231+
Assert.notNull(domainType, "Domain type must not be null");
232+
204233
AggregateChange<?> change = createDeletingChange(domainType);
205234
change.executeWith(interpreter, context, converter);
206235
}

0 commit comments

Comments
 (0)