Skip to content

Commit

Permalink
DATAJPA-1574 - Add support for saveAllAndFlush.
Browse files Browse the repository at this point in the history
  • Loading branch information
skrabbenborg committed Feb 3, 2021
1 parent ab4dd9c commit 5525041
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
* @author Sander Krabbenborg
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
Expand Down Expand Up @@ -76,6 +77,14 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
*/
<S extends T> S saveAndFlush(S entity);

/**
* Saves all entities and flushes changes instantly.
*
* @param entities
* @return the saved entities
*/
<S extends T> List<S> saveAllAndFlush(Iterable<S> entities);

/**
* Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs
* first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* @author Jens Schauder
* @author David Madden
* @author Moritz Becker
* @author Sander Krabbenborg
* @param <T> the type of the entity to handle
* @param <ID> the type of the entity's identifier
*/
Expand Down Expand Up @@ -620,6 +621,20 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
return result;
}

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#saveAllAndFlush(java.lang.Iterable)
*/
@Transactional
@Override
public <S extends T> List<S> saveAllAndFlush(Iterable<S> entities) {

List<S> result = saveAll(entities);
flush();

return result;
}

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#flush()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
* @author Kevin Peters
* @author Jens Schauder
* @author Andrey Kovalev
* @author Sander Krabbenborg
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:application-context.xml")
Expand Down Expand Up @@ -170,11 +171,23 @@ void savesCollectionCorrectly() throws Exception {
secondUser, thirdUser);
}

@Test // DATAJPA-1574
void savesAndFlushesCollectionCorrectly() {

assertThat(repository.saveAllAndFlush(asList(firstUser, secondUser, thirdUser))).hasSize(3).contains(firstUser,
secondUser, thirdUser);
}

@Test
void savingEmptyCollectionIsNoOp() throws Exception {
assertThat(repository.saveAll(new ArrayList<>())).isEmpty();
}

@Test // DATAJPA-1574
void savingAndFlushingEmptyCollectionIsNoOp() {
assertThat(repository.saveAllAndFlush(new ArrayList<>())).isEmpty();
}

@Test
void testUpdate() {

Expand Down Expand Up @@ -1101,6 +1114,20 @@ void saveAndFlushShouldSupportReturningSubTypesOfRepositoryEntity() {
assertThat(user.getEmailAddress()).isEqualTo(savedUser.getEmailAddress());
}

@Test // DATAJPA-1574
void saveAllAndFlushShouldSupportReturningSubTypesOfRepositoryEntity() {

repository.deleteAll();
SpecialUser user = new SpecialUser();
user.setFirstname("Thomas");
user.setEmailAddress("thomas@example.org");

List<SpecialUser> savedUsers = repository.saveAllAndFlush(Collections.singletonList(user));

assertThat(user.getFirstname()).isEqualTo(savedUsers.get(0).getFirstname());
assertThat(user.getEmailAddress()).isEqualTo(savedUsers.get(0).getEmailAddress());
}

@Test // DATAJPA-218
void findAllByUntypedExampleShouldReturnSubTypesOfRepositoryEntity() {

Expand Down

0 comments on commit 5525041

Please sign in to comment.