Adds support for saveAllAndFlush.

Closes #1883
Original pull request #2148
This commit is contained in:
Sander Krabbenborg
2021-02-03 10:17:32 +01:00
committed by Jens Schauder
parent 7edf0ecb0e
commit ed6e8ff4fc
3 changed files with 51 additions and 0 deletions

View File

@@ -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<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
@@ -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

View File

@@ -73,6 +73,7 @@ import org.springframework.util.Assert;
* @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
*/
@@ -620,6 +621,20 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
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()

View File

@@ -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<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() {