Added a builder for the HibernateItemWriter

This commit adds a builder for the HibernateItemWriter.  It also removes
the previously depricated HibernateTemplate references from the
HibernateItemWriter.

Resolves BATCH-2585
This commit is contained in:
Michael Minella
2017-03-28 12:58:31 -05:00
parent 75873629af
commit 3f22b61a66
5 changed files with 218 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2017 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.
@@ -50,7 +50,6 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
protected static final Log logger = LogFactory
.getLog(HibernateItemWriter.class);
private HibernateOperations hibernateTemplate;
private SessionFactory sessionFactory;
private boolean clearSession = true;
@@ -66,18 +65,6 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
this.clearSession = clearSession;
}
/**
* Public setter for the {@link HibernateOperations} property.
*
* @param hibernateTemplate
* the hibernateTemplate to set
* @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly
*/
@Deprecated
public void setHibernateTemplate(HibernateOperations hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* Set the Hibernate SessionFactory to be used internally.
*
@@ -88,12 +75,12 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
}
/**
* Check mandatory properties - there must be a hibernateTemplate.
* Check mandatory properties - there must be a sessionFactory.
*/
@Override
public void afterPropertiesSet() {
Assert.state(!(hibernateTemplate == null && sessionFactory == null),
"Either HibernateOperations or SessionFactory must be provided");
Assert.state(sessionFactory != null,
"SessionFactory must be provided");
}
/**
@@ -104,19 +91,10 @@ public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
*/
@Override
public void write(List<? extends T> items) {
if(sessionFactory == null) {
doWrite(hibernateTemplate, items);
hibernateTemplate.flush();
if (clearSession) {
hibernateTemplate.clear();
}
}
else {
doWrite(sessionFactory, items);
sessionFactory.getCurrentSession().flush();
if(clearSession) {
sessionFactory.getCurrentSession().clear();
}
doWrite(sessionFactory, items);
sessionFactory.getCurrentSession().flush();
if(clearSession) {
sessionFactory.getCurrentSession().clear();
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.database.builder;
import org.hibernate.SessionFactory;
import org.springframework.batch.item.database.HibernateItemWriter;
import org.springframework.util.Assert;
/**
* A builder for the {@link HibernateItemWriter}
*
* @author Michael Minella
* @since 4.0
* @see HibernateItemWriter
*/
public class HibernateItemWriterBuilder<T> {
private boolean clearSession = true;
private SessionFactory sessionFactory;
/**
* If set to false, the {@link org.hibernate.Session} will not be cleared at the end
* of the chunk.
*
* @param clearSession defaults to true
* @return this instance for method chaining
* @see HibernateItemWriter#setClearSession(boolean)
*/
public HibernateItemWriterBuilder<T> clearSession(boolean clearSession) {
this.clearSession = clearSession;
return this;
}
/**
* The Hibernate {@link SessionFactory} to obtain a session from. Required.
*
* @param sessionFactory the {@link SessionFactory}
* @return this instance for method chaining
* @see HibernateItemWriter#setSessionFactory(SessionFactory)
*/
public HibernateItemWriterBuilder<T> sessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
return this;
}
/**
* Returns a fully built {@link HibernateItemWriter}
*
* @return the writer
*/
public HibernateItemWriter<T> build() {
Assert.state(this.sessionFactory != null,
"SessionFactory must be provided");
HibernateItemWriter<T> writer = new HibernateItemWriter<>();
writer.setSessionFactory(this.sessionFactory);
writer.setClearSession(this.clearSession);
return writer;
}
}