Skip to content

Commit bd016a9

Browse files
mustaev-ruslanfmbenhassine
authored andcommitted
Add usePersist chain method to JpaItemWriterBuilder
1 parent e5cafe4 commit bd016a9

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilder.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
public class JpaItemWriterBuilder<T> {
3131

3232
private EntityManagerFactory entityManagerFactory;
33+
private boolean usePersist = false;
3334

3435
/**
3536
* The JPA {@link EntityManagerFactory} to obtain an entity manager from. Required.
@@ -44,6 +45,19 @@ public JpaItemWriterBuilder<T> entityManagerFactory(EntityManagerFactory entityM
4445
return this;
4546
}
4647

48+
/**
49+
* Set whether the entity manager should perform a persist instead of a merge.
50+
*
51+
* @param usePersist defaults to false
52+
* @return this instance for method chaining
53+
* @see JpaItemWriter#setUsePersist(boolean)
54+
*/
55+
public JpaItemWriterBuilder<T> usePersist(boolean usePersist) {
56+
this.usePersist = usePersist;
57+
58+
return this;
59+
}
60+
4761
/**
4862
* Returns a fully built {@link JpaItemWriter}.
4963
*
@@ -55,6 +69,7 @@ public JpaItemWriter<T> build() {
5569

5670
JpaItemWriter<T> writer = new JpaItemWriter<>();
5771
writer.setEntityManagerFactory(this.entityManagerFactory);
72+
writer.setUsePersist(this.usePersist);
5873

5974
return writer;
6075
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilderTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,4 +84,22 @@ public void testValidation() {
8484
assertEquals("Incorrect message", "EntityManagerFactory must be provided", ise.getMessage());
8585
}
8686
}
87+
88+
@Test
89+
public void testPersist() throws Exception {
90+
JpaItemWriter<String> itemWriter = new JpaItemWriterBuilder<String>()
91+
.entityManagerFactory(this.entityManagerFactory)
92+
.usePersist(true)
93+
.build();
94+
95+
itemWriter.afterPropertiesSet();
96+
97+
List<String> items = Arrays.asList("foo", "bar");
98+
99+
itemWriter.write(items);
100+
101+
verify(this.entityManager).persist(items.get(0));
102+
verify(this.entityManager).persist(items.get(1));
103+
}
104+
87105
}

0 commit comments

Comments
 (0)