diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java index 2c0331365..aabadceb4 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaItemWriter.java @@ -50,6 +50,7 @@ public class JpaItemWriter implements ItemWriter, InitializingBean { protected static final Log logger = LogFactory.getLog(JpaItemWriter.class); private EntityManagerFactory entityManagerFactory; + private boolean usePersist = false; /** * Set the EntityManager to be used internally. @@ -59,6 +60,15 @@ public class JpaItemWriter implements ItemWriter, InitializingBean { public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { this.entityManagerFactory = entityManagerFactory; } + + /** + * Set whether the EntityManager should perform a persist instead of a merge. + * + * @param usePersist whether to use persist instead of merge. + */ + public void setUsePersist(boolean usePersist) { + this.usePersist = usePersist; + } /** * Check mandatory properties - there must be an entityManagerFactory. @@ -98,16 +108,21 @@ public class JpaItemWriter implements ItemWriter, InitializingBean { } if (!items.isEmpty()) { - long mergeCount = 0; + long addedToContextCount = 0; for (T item : items) { if (!entityManager.contains(item)) { - entityManager.merge(item); - mergeCount++; + if(usePersist) { + entityManager.persist(item); + } + else { + entityManager.merge(item); + } + addedToContextCount++; } } if (logger.isDebugEnabled()) { - logger.debug(mergeCount + " entities merged."); - logger.debug((items.size() - mergeCount) + " entities found in persistence context."); + logger.debug(addedToContextCount + " entities " + (usePersist ? " persisted." : "merged.")); + logger.debug((items.size() - addedToContextCount) + " entities found in persistence context."); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterPersistTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterPersistTests.java new file mode 100644 index 000000000..92b16e706 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterPersistTests.java @@ -0,0 +1,80 @@ +/* + * Copyright 2006-2008 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.orm.jpa.EntityManagerHolder; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +/** + * @author Chris Cranford + * + */ +public class JpaItemWriterPersistTests { + + EntityManagerFactory emf; + + JpaItemWriter writer; + + @Before + public void setUp() throws Exception { + if (TransactionSynchronizationManager.isSynchronizationActive()) { + TransactionSynchronizationManager.clearSynchronization(); + } + writer = new JpaItemWriter(); + writer.setUsePersist(true); + emf = mock(EntityManagerFactory.class,"emf"); + writer.setEntityManagerFactory(emf); + } + + @Test + public void testAfterPropertiesSet() throws Exception { + writer = new JpaItemWriter(); + try { + writer.afterPropertiesSet(); + fail("Expected IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + // expected + assertTrue("Wrong message for exception: " + e.getMessage(), + e.getMessage().indexOf("EntityManagerFactory") >= 0); + } + } + + @Test + public void testPersist() throws Exception { + EntityManager em = mock(EntityManager.class, "em"); + TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); + List items = Arrays.asList(new String[] { "persist1", "persist2" }); + writer.write(items); + TransactionSynchronizationManager.unbindResource(emf); + } + +}