001/* 002 * Copyright 2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.item.database.builder; 017 018import javax.persistence.EntityManagerFactory; 019 020import org.springframework.batch.item.database.JpaItemWriter; 021import org.springframework.util.Assert; 022 023/** 024 * A builder for the {@link JpaItemWriter}. 025 * 026 * @author Mahmoud Ben Hassine 027 * @since 4.1 028 * @see JpaItemWriter 029 */ 030public class JpaItemWriterBuilder<T> { 031 032 private EntityManagerFactory entityManagerFactory; 033 034 /** 035 * The JPA {@link EntityManagerFactory} to obtain an entity manager from. Required. 036 * 037 * @param entityManagerFactory the {@link EntityManagerFactory} 038 * @return this instance for method chaining 039 * @see JpaItemWriter#setEntityManagerFactory(EntityManagerFactory) 040 */ 041 public JpaItemWriterBuilder<T> entityManagerFactory(EntityManagerFactory entityManagerFactory) { 042 this.entityManagerFactory = entityManagerFactory; 043 044 return this; 045 } 046 047 /** 048 * Returns a fully built {@link JpaItemWriter}. 049 * 050 * @return the writer 051 */ 052 public JpaItemWriter<T> build() { 053 Assert.state(this.entityManagerFactory != null, 054 "EntityManagerFactory must be provided"); 055 056 JpaItemWriter<T> writer = new JpaItemWriter<>(); 057 writer.setEntityManagerFactory(this.entityManagerFactory); 058 059 return writer; 060 } 061}