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 b71daf34bf..7de37a262b 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 @@ * @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 0e03b22385..1ae36e328e 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 @@ * @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 List saveAll(Iterable entities) { return result; } + /* + * (non-Javadoc) + * @see org.springframework.data.jpa.repository.JpaRepository#saveAllAndFlush(java.lang.Iterable) + */ + @Transactional + @Override + public 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 a7881df57e..82bd3c4018 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 @@ * @author Kevin Peters * @author Jens Schauder * @author Andrey Kovalev + * @author Sander Krabbenborg */ @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:application-context.xml") @@ -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() { @@ -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 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() {