Skip to content

Commit 3ce4af2

Browse files
committed
Introduce JpaEntityInformation.getVersionAttribute()
1 parent ac3f819 commit 3ce4af2

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaEntityInformation.java

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Collection;
2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
import org.springframework.data.jpa.repository.query.JpaEntityMetadata;
2425
import org.springframework.data.repository.core.EntityInformation;
@@ -30,6 +31,7 @@
3031
* @author Oliver Gierke
3132
* @author Thomas Darimont
3233
* @author Mark Paluch
34+
* @author Yanming Zhou
3335
*/
3436
public interface JpaEntityInformation<T, ID> extends EntityInformation<T, ID>, JpaEntityMetadata<T> {
3537

@@ -39,6 +41,11 @@ public interface JpaEntityInformation<T, ID> extends EntityInformation<T, ID>, J
3941
@Nullable
4042
SingularAttribute<? super T, ?> getIdAttribute();
4143

44+
/**
45+
* Returns the version attribute of the entity.
46+
*/
47+
Optional<SingularAttribute<? super T, ?>> getVersionAttribute();
48+
4249
/**
4350
* Returns the required identifier type.
4451
*

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@
4747

4848
/**
4949
* Implementation of {@link org.springframework.data.repository.core.EntityInformation} that uses JPA {@link Metamodel}
50-
* to find the domain class' id field.
50+
* to find the domain class' id and version field.
5151
*
5252
* @author Oliver Gierke
5353
* @author Thomas Darimont
5454
* @author Christoph Strobl
5555
* @author Mark Paluch
5656
* @author Jens Schauder
5757
* @author Greg Turnquist
58+
* @author Yanming Zhou
5859
*/
5960
public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSupport<T, ID> {
6061

@@ -191,6 +192,11 @@ public Class<ID> getIdType() {
191192
return idMetadata.getSimpleIdAttribute();
192193
}
193194

195+
@Override
196+
public Optional<SingularAttribute<? super T, ?>> getVersionAttribute() {
197+
return versionAttribute;
198+
}
199+
194200
@Override
195201
public boolean hasCompositeId() {
196202
return !idMetadata.hasSimpleId();

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaEntityInformationSupportUnitTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Collection;
3030
import java.util.Collections;
3131
import java.util.Map;
32+
import java.util.Optional;
3233

3334
import org.junit.jupiter.api.Test;
3435
import org.junit.jupiter.api.extension.ExtendWith;
@@ -42,6 +43,7 @@
4243
*
4344
* @author Oliver Gierke
4445
* @author Jens Schauder
46+
* @author Yanming Zhou
4547
*/
4648
@ExtendWith(MockitoExtension.class)
4749
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -88,6 +90,11 @@ static class DummyJpaEntityInformation<T, ID> extends JpaEntityInformationSuppor
8890
return null;
8991
}
9092

93+
@Override
94+
public Optional<SingularAttribute<? super T, ?>> getVersionAttribute() {
95+
return Optional.empty();
96+
}
97+
9198
@Override
9299
public ID getId(T entity) {
93100
return null;

0 commit comments

Comments
 (0)