DATAJPA-605 - JpaPersistentProperty implementation now detects JPA's @Version annotation.

Added override for JpaPersistentPropertyImpl.isVersionProperty() to also consider properties annotated with JPA's @Version annotation as version property.

Related pull request: #108.
This commit is contained in:
Oliver Gierke
2014-11-10 14:37:36 +01:00
parent fb76c9d422
commit 1aac6809ea
2 changed files with 37 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.metamodel.Metamodel;
import org.springframework.data.annotation.AccessType.Type;
@@ -174,6 +175,15 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
return usePropertyAccess != null ? usePropertyAccess : super.usePropertyAccess();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isVersionProperty()
*/
@Override
public boolean isVersionProperty() {
return isAnnotationPresent(Version.class) || super.isVersionProperty();
}
/**
* Looks up both Spring Data's and JPA's access type definition annotations on the property or type level to determine
* the access type to be used. Will consider property-level annotations over type-level ones, favoring the Spring Data

View File

@@ -32,6 +32,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.annotation.Version;
/**
* Unit tests for {@link JpaPersistentPropertyImpl}.
@@ -142,6 +143,22 @@ public class JpaPersistentPropertyImplUnitTests {
assertThat(getProperty(CompetingPropertyLevelAnnotations.class, "id").usePropertyAccess(), is(false));
}
/**
* @see DATAJPA-605
*/
@Test
public void detectsJpaVersionAnnotation() {
assertThat(getProperty(JpaVersioned.class, "version").isVersionProperty(), is(true));
}
/**
* @see DATAJPA-605
*/
@Test
public void detectsSpringDataVersionAnnotation() {
assertThat(getProperty(SpringDataVersioned.class, "version").isVersionProperty(), is(true));
}
private JpaPersistentProperty getProperty(Class<?> ownerType, String propertyName) {
JpaPersistentEntity<?> entity = context.getPersistentEntity(ownerType);
@@ -241,4 +258,14 @@ public class JpaPersistentPropertyImplUnitTests {
return id;
}
}
static class SpringDataVersioned {
@Version long version;
}
static class JpaVersioned {
@Version long version;
}
}