Skip to content

Commit 68fcf6e

Browse files
schauderodrotbohm
authored andcommitted
DATAJPA-1261 - Revert optimizations made for existing entities in implementation of CrudRepository.save(…).
This reverts the functional change made by DATAJPA-931. This means merge will be called again, even if the entity for which save was called is already attached to the session. Tests stay in place to verify the new old behavior. Related ticket: DATAJPA-931. Original pull request: #249.
1 parent 247f67d commit 68fcf6e

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,9 @@ public <S extends T> S save(S entity) {
489489
if (entityInformation.isNew(entity)) {
490490
em.persist(entity);
491491
return entity;
492-
} else if (!em.contains(entity)) {
492+
} else {
493493
return em.merge(entity);
494494
}
495-
496-
return entity;
497495
}
498496

499497
/*

src/test/java/org/springframework/data/jpa/repository/support/SimpleJpaRepositoryUnitTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ public void mergeGetsCalledWhenDetached() {
145145
verify(em).merge(detachedUser);
146146
}
147147

148-
@Test // DATAJPA-931
149-
public void mergeGetsNotCalledWhenAttached() {
148+
@Test // DATAJPA-931, DATAJPA-1261
149+
public void mergeGetsCalledWhenAttached() {
150150

151151
User attachedUser = new User();
152152

153153
when(em.contains(attachedUser)).thenReturn(true);
154154

155155
repo.save(attachedUser);
156156

157-
verify(em, never()).merge(attachedUser);
157+
verify(em).merge(attachedUser);
158158
}
159159
}

0 commit comments

Comments
 (0)