Skip to content

Commit ed6e8ff

Browse files
skrabbenborgschauder
authored andcommitted
Adds support for saveAllAndFlush.
Closes #1883 Original pull request #2148
1 parent 7edf0ec commit ed6e8ff

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/main/java/org/springframework/data/jpa/repository/JpaRepository.java

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Oliver Gierke
3232
* @author Christoph Strobl
3333
* @author Mark Paluch
34+
* @author Sander Krabbenborg
3435
*/
3536
@NoRepositoryBean
3637
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
@@ -76,6 +77,14 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
7677
*/
7778
<S extends T> S saveAndFlush(S entity);
7879

80+
/**
81+
* Saves all entities and flushes changes instantly.
82+
*
83+
* @param entities
84+
* @return the saved entities
85+
*/
86+
<S extends T> List<S> saveAllAndFlush(Iterable<S> entities);
87+
7988
/**
8089
* Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs
8190
* first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* @author Jens Schauder
7474
* @author David Madden
7575
* @author Moritz Becker
76+
* @author Sander Krabbenborg
7677
* @param <T> the type of the entity to handle
7778
* @param <ID> the type of the entity's identifier
7879
*/
@@ -620,6 +621,20 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
620621
return result;
621622
}
622623

624+
/*
625+
* (non-Javadoc)
626+
* @see org.springframework.data.jpa.repository.JpaRepository#saveAllAndFlush(java.lang.Iterable)
627+
*/
628+
@Transactional
629+
@Override
630+
public <S extends T> List<S> saveAllAndFlush(Iterable<S> entities) {
631+
632+
List<S> result = saveAll(entities);
633+
flush();
634+
635+
return result;
636+
}
637+
623638
/*
624639
* (non-Javadoc)
625640
* @see org.springframework.data.jpa.repository.JpaRepository#flush()

src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+27
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
* @author Kevin Peters
9292
* @author Jens Schauder
9393
* @author Andrey Kovalev
94+
* @author Sander Krabbenborg
9495
*/
9596
@ExtendWith(SpringExtension.class)
9697
@ContextConfiguration("classpath:application-context.xml")
@@ -170,11 +171,23 @@ void savesCollectionCorrectly() throws Exception {
170171
secondUser, thirdUser);
171172
}
172173

174+
@Test // DATAJPA-1574
175+
void savesAndFlushesCollectionCorrectly() {
176+
177+
assertThat(repository.saveAllAndFlush(asList(firstUser, secondUser, thirdUser))).hasSize(3).contains(firstUser,
178+
secondUser, thirdUser);
179+
}
180+
173181
@Test
174182
void savingEmptyCollectionIsNoOp() throws Exception {
175183
assertThat(repository.saveAll(new ArrayList<>())).isEmpty();
176184
}
177185

186+
@Test // DATAJPA-1574
187+
void savingAndFlushingEmptyCollectionIsNoOp() {
188+
assertThat(repository.saveAllAndFlush(new ArrayList<>())).isEmpty();
189+
}
190+
178191
@Test
179192
void testUpdate() {
180193

@@ -1101,6 +1114,20 @@ void saveAndFlushShouldSupportReturningSubTypesOfRepositoryEntity() {
11011114
assertThat(user.getEmailAddress()).isEqualTo(savedUser.getEmailAddress());
11021115
}
11031116

1117+
@Test // DATAJPA-1574
1118+
void saveAllAndFlushShouldSupportReturningSubTypesOfRepositoryEntity() {
1119+
1120+
repository.deleteAll();
1121+
SpecialUser user = new SpecialUser();
1122+
user.setFirstname("Thomas");
1123+
user.setEmailAddress("[email protected]");
1124+
1125+
List<SpecialUser> savedUsers = repository.saveAllAndFlush(Collections.singletonList(user));
1126+
1127+
assertThat(user.getFirstname()).isEqualTo(savedUsers.get(0).getFirstname());
1128+
assertThat(user.getEmailAddress()).isEqualTo(savedUsers.get(0).getEmailAddress());
1129+
}
1130+
11041131
@Test // DATAJPA-218
11051132
void findAllByUntypedExampleShouldReturnSubTypesOfRepositoryEntity() {
11061133

0 commit comments

Comments
 (0)