Skip to content

Commit

Permalink
Eliminate unnecessary merge() if entity is non-versioned
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Mar 19, 2024
1 parent 3ce4af2 commit f07a355
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ public void delete(T entity) {
return;
}

entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
if (entityInformation.getVersionAttribute().isPresent()) {
// call merge() to raise ObjectOptimisticLockingFailureException if entity is stale
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
}
else {
entityManager.remove(existing);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package org.springframework.data.jpa.repository.support;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;

import jakarta.persistence.EntityManager;
import jakarta.persistence.OptimisticLockException;
Expand All @@ -29,6 +32,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.data.jpa.domain.sample.PersistableWithIdClass;
import org.springframework.data.jpa.domain.sample.PersistableWithIdClassPK;
import org.springframework.data.jpa.domain.sample.SampleEntity;
Expand Down Expand Up @@ -57,13 +61,16 @@ class JpaRepositoryTests {

@PersistenceContext EntityManager em;

private EntityManager spiedEntityManager;

private JpaRepository<SampleEntity, SampleEntityPK> repository;
private CrudRepository<PersistableWithIdClass, PersistableWithIdClassPK> idClassRepository;
private JpaRepository<VersionedUser, Long> versionedUserRepository;

@BeforeEach
void setUp() {
repository = new JpaRepositoryFactory(em).getRepository(SampleEntityRepository.class);
spiedEntityManager = Mockito.spy(em);
repository = new JpaRepositoryFactory(spiedEntityManager).getRepository(SampleEntityRepository.class);
idClassRepository = new JpaRepositoryFactory(em).getRepository(SampleWithIdClassRepository.class);
versionedUserRepository = new JpaRepositoryFactory(em).getRepository(VersionedUserRepository.class);
}
Expand Down Expand Up @@ -187,6 +194,18 @@ void deleteStaleVersionedEntityShouldRaiseOptimisticLockException() {
});
}

@Test //GH-3401
void deleteNonVersionedEntityShouldNotInvokeMerge() {
SampleEntity entity = new SampleEntity("one", "eins");
repository.save(entity);
repository.flush();
em.detach(entity);

reset(spiedEntityManager);
repository.delete(entity);
then(spiedEntityManager).should(never()).merge(entity);
}

private interface SampleEntityRepository extends JpaRepository<SampleEntity, SampleEntityPK> {

}
Expand Down

0 comments on commit f07a355

Please sign in to comment.