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 21, 2024
1 parent d334baa commit 3a17e80
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 @@ -32,6 +35,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.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.sample.PersistableWithIdClass;
import org.springframework.data.jpa.domain.sample.PersistableWithIdClassPK;
Expand Down Expand Up @@ -65,14 +69,17 @@ class JpaRepositoryTests {

@PersistenceContext EntityManager em;

private EntityManager spiedEntityManager;

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

@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);
jdbcOperations = new NamedParameterJdbcTemplate(dataSource);
Expand Down Expand Up @@ -219,6 +226,18 @@ void deleteDirtyManagedVersionedEntityShouldRaiseOptimisticLockException() {
jdbcOperations.update("delete from VersionedUser", Map.of());
}

@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 3a17e80

Please sign in to comment.