Skip to content

Unable to set "usePersist" parameter with JpaItemWriterBuilder #3655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class JpaItemWriterBuilder<T> {

private EntityManagerFactory entityManagerFactory;
private boolean usePersist = false;

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

/**
* Set whether the entity manager should perform a persist instead of a merge.
*
* @param usePersist defaults to false
* @return this instance for method chaining
* @see JpaItemWriter#setUsePersist(boolean)
*/
public JpaItemWriterBuilder<T> usePersist(boolean usePersist) {
this.usePersist = usePersist;

return this;
}

/**
* Returns a fully built {@link JpaItemWriter}.
*
Expand All @@ -55,6 +69,7 @@ public JpaItemWriter<T> build() {

JpaItemWriter<T> writer = new JpaItemWriter<>();
writer.setEntityManagerFactory(this.entityManagerFactory);
writer.setUsePersist(this.usePersist);

return writer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,22 @@ public void testValidation() {
assertEquals("Incorrect message", "EntityManagerFactory must be provided", ise.getMessage());
}
}

@Test
public void testPersist() throws Exception {
JpaItemWriter<String> itemWriter = new JpaItemWriterBuilder<String>()
.entityManagerFactory(this.entityManagerFactory)
.usePersist(true)
.build();

itemWriter.afterPropertiesSet();

List<String> items = Arrays.asList("foo", "bar");

itemWriter.write(items);

verify(this.entityManager).persist(items.get(0));
verify(this.entityManager).persist(items.get(1));
}

}