Skip to content

DATAJPA-931 - Avoid unnecessary merging on save. #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAJPA-931-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Stefan Fussenegger
* @author Jens Schauder
* @param <T> the type of the entity to handle
* @param <ID> the type of the entity's identifier
*/
Expand Down Expand Up @@ -487,9 +488,11 @@ public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
} else if (!em.contains(entity)) {
return em.merge(entity);
}

return entity;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* Integration test for {@link AuditingEntityListener}.
*
* @author Oliver Gierke
* @author Jens Schauder
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:auditing/auditing-entity-listener.xml")
Expand Down Expand Up @@ -89,7 +90,7 @@ public void auditsTransitiveEntitiesCorrectly() {
role.setName("ADMIN");

user.addRole(role);
repository.save(user);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed? And if applied, shouldn't it be saveAndFlush(…)?

repository.flush();
role = user.getRoles().iterator().next();

assertDatesSet(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Jens Schauder
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class SimpleJpaRepositoryUnitTests {
Expand Down Expand Up @@ -131,4 +132,28 @@ public void shouldPropagateConfiguredEntityGraphToFindOne() throws Exception {

verify(em).find(User.class, id, singletonMap(EntityGraphType.LOAD.getKey(), (Object) entityGraph));
}

@Test // DATAJPA-931
public void mergeGetsCalledWhenDetached() {

User detachedUser = new User();

when(em.contains(detachedUser)).thenReturn(false);

repo.save(detachedUser);

verify(em).merge(detachedUser);
}

@Test // DATAJPA-931
public void mergeGetsNotCalledWhenAttached() {

User attachedUser = new User();

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

repo.save(attachedUser);

verify(em, never()).merge(attachedUser);
}
}