From 3ce4af249b62a4dc4a861719aafcf1d7d6e04f96 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 19 Mar 2024 15:07:55 +0800 Subject: [PATCH] Introduce JpaEntityInformation.getVersionAttribute() --- .../data/jpa/repository/support/JpaEntityInformation.java | 7 +++++++ .../repository/support/JpaMetamodelEntityInformation.java | 8 +++++++- .../support/JpaEntityInformationSupportUnitTests.java | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) 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..d38332ac57 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 @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.Map; +import java.util.Optional; import org.springframework.data.jpa.repository.query.JpaEntityMetadata; import org.springframework.data.repository.core.EntityInformation; @@ -30,6 +31,7 @@ * @author Oliver Gierke * @author Thomas Darimont * @author Mark Paluch + * @author Yanming Zhou */ public interface JpaEntityInformation extends EntityInformation, JpaEntityMetadata { @@ -39,6 +41,11 @@ public interface JpaEntityInformation extends EntityInformation, J @Nullable SingularAttribute getIdAttribute(); + /** + * Returns the version attribute of the entity. + */ + Optional> 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..a508a45456 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 Optional> getVersionAttribute() { + return versionAttribute; + } + @Override public boolean hasCompositeId() { return !idMetadata.hasSimpleId(); 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..df7522a65e 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 @@ -29,6 +29,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; +import java.util.Optional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -42,6 +43,7 @@ * * @author Oliver Gierke * @author Jens Schauder + * @author Yanming Zhou */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -88,6 +90,11 @@ static class DummyJpaEntityInformation extends JpaEntityInformationSuppor return null; } + @Override + public Optional> getVersionAttribute() { + return Optional.empty(); + } + @Override public ID getId(T entity) { return null;