spring-batch HibernateItemWriterBuilder 源码

  • 2022-08-16
  • 浏览 (11)

spring-batch HibernateItemWriterBuilder 代码

文件路径:/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernateItemWriterBuilder.java

/*
 * 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
 *
 *      https://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;
	}

}

相关信息

spring-batch 源码目录

相关文章

spring-batch HibernateCursorItemReaderBuilder 源码

spring-batch HibernatePagingItemReaderBuilder 源码

spring-batch JdbcBatchItemWriterBuilder 源码

spring-batch JdbcCursorItemReaderBuilder 源码

spring-batch JdbcPagingItemReaderBuilder 源码

spring-batch JpaCursorItemReaderBuilder 源码

spring-batch JpaItemWriterBuilder 源码

spring-batch JpaPagingItemReaderBuilder 源码

spring-batch StoredProcedureItemReaderBuilder 源码

spring-batch package-info 源码

0  赞