DATAJPA-775 - JpaPersistenPropertyImpl now rejects Spring Data Commons' @Version.
JpaPersistentPropertyImpl now defensively checks whether Spring Data Commons' @Version annotation is used instead of the JPA one and throws an exception if so as the persistence providers can only deal with the latter and optimistic locking won't work correctly otherwise. Original pull request: #153.
This commit is contained in:
committed by
Oliver Gierke
parent
eda57f29f4
commit
c9fee0c4f3
@@ -53,6 +53,7 @@ import org.springframework.util.Assert;
|
||||
* {@link JpaPersistentProperty} implementation usind a JPA {@link Metamodel}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Greg Turnquist
|
||||
* @since 1.3
|
||||
*/
|
||||
class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPersistentProperty> implements
|
||||
@@ -210,13 +211,22 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
|
||||
return usePropertyAccess != null ? usePropertyAccess : super.usePropertyAccess();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isVersionProperty()
|
||||
/**
|
||||
* Lookup if the property is versioned. In the event it's versioned using Spring Data Commons and NOT JPA,
|
||||
* fail fast with a detailed error message.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean isVersionProperty() {
|
||||
return isAnnotationPresent(Version.class) || super.isVersionProperty();
|
||||
|
||||
if (super.isVersionProperty() && !isAnnotationPresent(Version.class)) {
|
||||
throw new IllegalArgumentException(this.owner.getName() + "." + this.getName() + " is annotated with " +
|
||||
org.springframework.data.annotation.Version.class.getCanonicalName() + ". With Spring Data JPA, use " +
|
||||
Version.class.getCanonicalName() + ".");
|
||||
}
|
||||
|
||||
return isAnnotationPresent(Version.class);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -44,6 +44,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
* Unit tests for {@link JpaPersistentPropertyImpl}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JpaPersistentPropertyImplUnitTests {
|
||||
@@ -159,10 +160,11 @@ public class JpaPersistentPropertyImplUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATAJPA-605
|
||||
* @see DATAJPA-775
|
||||
*/
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void detectsSpringDataVersionAnnotation() {
|
||||
assertThat(getProperty(SpringDataVersioned.class, "version").isVersionProperty(), is(true));
|
||||
getProperty(SpringDataVersioned.class, "version");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,7 +300,7 @@ public class JpaPersistentPropertyImplUnitTests {
|
||||
|
||||
static class JpaVersioned {
|
||||
|
||||
@Version long version;
|
||||
@javax.persistence.Version long version;
|
||||
}
|
||||
|
||||
static class SpecializedAssociation {
|
||||
|
||||
Reference in New Issue
Block a user