Make RepositoryItemWriter use CrudRepository#saveAll by default
Before this commit, the `RepositoryItemWriter` did not use `CrudRepository#saveAll` by default and one must override `doWrite` and call it manually, which is not convenient. This commit makes the writer use `CrudRepository#saveAll` by default while keeping the possibility to use another method if desired through the `methodName` parameter. Resolves #3720
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -36,6 +36,8 @@ import org.springframework.util.MethodInvoker;
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* By default, this writer will use {@link CrudRepository#saveAll(Iterable)}
|
||||
* to save items, unless another method is selected with {@link #setMethodName(java.lang.String)}.
|
||||
* It depends on {@link org.springframework.data.repository.CrudRepository#saveAll(Iterable)}
|
||||
* method to store the items for the chunk. Performance will be determined by that
|
||||
* implementation more than this writer.
|
||||
@@ -107,6 +109,11 @@ public class RepositoryItemWriter<T> implements ItemWriter<T>, InitializingBean
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Writing to the repository with " + items.size() + " items.");
|
||||
}
|
||||
|
||||
if (this.methodName == null) {
|
||||
this.repository.saveAll(items);
|
||||
return;
|
||||
}
|
||||
|
||||
MethodInvoker invoker = createMethodInvoker(repository, methodName);
|
||||
|
||||
@@ -122,6 +129,12 @@ public class RepositoryItemWriter<T> implements ItemWriter<T>, InitializingBean
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.state(repository != null, "A CrudRepository implementation is required");
|
||||
if (this.methodName != null) {
|
||||
Assert.hasText(this.methodName, "methodName must not be empty.");
|
||||
}
|
||||
else {
|
||||
logger.debug("No method name provided, CrudRepository.saveAll will be used.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2018 the original author or authors.
|
||||
* Copyright 2017-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,9 @@ package org.springframework.batch.item.data.builder;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.batch.item.data.RepositoryItemWriter;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
import org.springframework.cglib.proxy.MethodInterceptor;
|
||||
@@ -36,6 +39,8 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class RepositoryItemWriterBuilder<T> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(RepositoryItemWriterBuilder.class.getName());
|
||||
|
||||
private CrudRepository<T, ?> repository;
|
||||
|
||||
private String methodName;
|
||||
@@ -104,12 +109,16 @@ public class RepositoryItemWriterBuilder<T> {
|
||||
this.repository = this.repositoryMethodReference.getRepository();
|
||||
}
|
||||
|
||||
Assert.hasText(this.methodName, "methodName is required.");
|
||||
Assert.notNull(this.repository, "repository is required.");
|
||||
|
||||
RepositoryItemWriter<T> writer = new RepositoryItemWriter<>();
|
||||
writer.setMethodName(this.methodName);
|
||||
writer.setRepository(this.repository);
|
||||
if (this.methodName != null) {
|
||||
Assert.hasText(this.methodName, "methodName must not be empty.");
|
||||
writer.setMethodName(this.methodName);
|
||||
} else {
|
||||
logger.debug("No method name provided, CrudRepository.saveAll will be used.");
|
||||
}
|
||||
return writer;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user