From a1a688bf4dac02ddb01918b4af88a44d73f10e35 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 18 Mar 2024 15:57:26 +0800 Subject: [PATCH] Eliminate unnecessary merge() if possible Fix GH-3401 --- .../data/jpa/repository/support/JpaEntityInformation.java | 7 +++++++ .../repository/support/JpaMetamodelEntityInformation.java | 8 +++++++- .../data/jpa/repository/support/SimpleJpaRepository.java | 3 ++- .../support/JpaEntityInformationSupportUnitTests.java | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java index 096bd77d21..134fcde47d 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java @@ -30,6 +30,7 @@ * @author Oliver Gierke * @author Thomas Darimont * @author Mark Paluch + * @author Yanming Zhou */ public interface JpaEntityInformation extends EntityInformation, JpaEntityMetadata { @@ -39,6 +40,12 @@ public interface JpaEntityInformation extends EntityInformation, J @Nullable SingularAttribute getIdAttribute(); + /** + * Returns the version attribute of the entity. + */ + @Nullable + SingularAttribute getVersionAttribute(); + /** * Returns the required identifier type. * diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java index 9979fc773b..42f938840d 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java @@ -47,7 +47,7 @@ /** * Implementation of {@link org.springframework.data.repository.core.EntityInformation} that uses JPA {@link Metamodel} - * to find the domain class' id field. + * to find the domain class' id and version field. * * @author Oliver Gierke * @author Thomas Darimont @@ -55,6 +55,7 @@ * @author Mark Paluch * @author Jens Schauder * @author Greg Turnquist + * @author Yanming Zhou */ public class JpaMetamodelEntityInformation extends JpaEntityInformationSupport { @@ -191,6 +192,11 @@ public Class getIdType() { return idMetadata.getSimpleIdAttribute(); } + @Override + public SingularAttribute getVersionAttribute() { + return versionAttribute.orElse(null); + } + @Override public boolean hasCompositeId() { return !idMetadata.hasSimpleId(); diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 31abc19187..9f1b796cb6 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -195,7 +195,8 @@ public void delete(T entity) { return; } - entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity)); + boolean versioned = entityInformation.getVersionAttribute() != null; // GH-3401 + entityManager.remove((entityManager.contains(entity) || !versioned) ? entity : entityManager.merge(entity)); } @Override diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaEntityInformationSupportUnitTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaEntityInformationSupportUnitTests.java index e66e624b12..25a3cc7739 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaEntityInformationSupportUnitTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaEntityInformationSupportUnitTests.java @@ -42,6 +42,7 @@ * * @author Oliver Gierke * @author Jens Schauder + * @author Yanming Zhou */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -88,6 +89,11 @@ static class DummyJpaEntityInformation extends JpaEntityInformationSuppor return null; } + @Override + public SingularAttribute getVersionAttribute() { + return null; + } + @Override public ID getId(T entity) { return null;