From ed6e8ff4fc8e8fccfa4844d37a2c7123df77561f Mon Sep 17 00:00:00 2001 From: Sander Krabbenborg Date: Wed, 3 Feb 2021 10:17:32 +0100 Subject: [PATCH] Adds support for saveAllAndFlush. Closes #1883 Original pull request #2148 --- .../data/jpa/repository/JpaRepository.java | 9 +++++++ .../support/SimpleJpaRepository.java | 15 +++++++++++ .../jpa/repository/UserRepositoryTests.java | 27 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java index b71daf34b..7de37a262 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -31,6 +31,7 @@ import org.springframework.data.repository.query.QueryByExampleExecutor; * @author Oliver Gierke * @author Christoph Strobl * @author Mark Paluch + * @author Sander Krabbenborg */ @NoRepositoryBean public interface JpaRepository extends PagingAndSortingRepository, QueryByExampleExecutor { @@ -76,6 +77,14 @@ public interface JpaRepository extends PagingAndSortingRepository, */ S saveAndFlush(S entity); + /** + * Saves all entities and flushes changes instantly. + * + * @param entities + * @return the saved entities + */ + List saveAllAndFlush(Iterable 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 diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 0e03b2238..1ae36e328 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -73,6 +73,7 @@ import org.springframework.util.Assert; * @author Jens Schauder * @author David Madden * @author Moritz Becker + * @author Sander Krabbenborg * @param the type of the entity to handle * @param the type of the entity's identifier */ @@ -620,6 +621,20 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation List saveAllAndFlush(Iterable entities) { + + List result = saveAll(entities); + flush(); + + return result; + } + /* * (non-Javadoc) * @see org.springframework.data.jpa.repository.JpaRepository#flush() diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index a7881df57..82bd3c401 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -91,6 +91,7 @@ import com.google.common.base.Optional; * @author Kevin Peters * @author Jens Schauder * @author Andrey Kovalev + * @author Sander Krabbenborg */ @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:application-context.xml") @@ -170,11 +171,23 @@ public class UserRepositoryTests { 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() { @@ -1101,6 +1114,20 @@ public class UserRepositoryTests { 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 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() {