DATAJPA-775 - Improvements to Spring Data @Version detection.

Moved the detection of an invalidly used @Version to the verification phase of the JpaPersistentEntity so that the MappingContext doesn't even bootstrap if a misconfiguration is detected.

The previous approach would've detected the invalid stage at access time which might actually occur too late.

Original pull request: #153.
This commit is contained in:
Oliver Gierke
2015-08-13 09:38:32 +02:00
parent c9fee0c4f3
commit 1eaeaa0e9d
4 changed files with 80 additions and 23 deletions

View File

@@ -19,6 +19,7 @@ import java.util.Comparator;
import javax.persistence.metamodel.Metamodel;
import org.springframework.data.annotation.Version;
import org.springframework.data.jpa.provider.PersistenceProvider;
import org.springframework.data.jpa.provider.ProxyIdAccessor;
import org.springframework.data.mapping.IdentifierAccessor;
@@ -31,10 +32,15 @@ import org.springframework.util.Assert;
* Implementation of {@link JpaPersistentEntity}.
*
* @author Oliver Gierke
* @author Greg Turnquist
* @since 1.3
*/
class JpaPersistentEntityImpl<T> extends BasicPersistentEntity<T, JpaPersistentProperty> implements
JpaPersistentEntity<T> {
class JpaPersistentEntityImpl<T> extends BasicPersistentEntity<T, JpaPersistentProperty>
implements JpaPersistentEntity<T> {
private static final String INVALID_VERSION_ANNOTATION = "%s is annotated with "
+ org.springframework.data.annotation.Version.class.getName() + " but needs to use "
+ javax.persistence.Version.class.getName() + " to trigger optimistic locking correctly!";
private final ProxyIdAccessor proxyIdAccessor;
@@ -70,6 +76,26 @@ class JpaPersistentEntityImpl<T> extends BasicPersistentEntity<T, JpaPersistentP
return new JpaProxyAwareIdentifierAccessor(this, bean, proxyIdAccessor);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.BasicPersistentEntity#verify()
*/
@Override
public void verify() {
super.verify();
JpaPersistentProperty versionProperty = getVersionProperty();
if (versionProperty == null) {
return;
}
if (versionProperty.isAnnotationPresent(Version.class)) {
throw new IllegalArgumentException(String.format(INVALID_VERSION_ANNOTATION, versionProperty));
}
}
/**
* {@link IdentifierAccessor} that tries to use a {@link ProxyIdAccessor} for id access to potentially avoid the
* initialization of JPA proxies. We're falling back to the default behavior of {@link IdPropertyIdentifierAccessor}

View File

@@ -211,21 +211,12 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
return usePropertyAccess != null ? usePropertyAccess : super.usePropertyAccess();
}
/**
* 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
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#isVersionProperty()
*/
@Override
public boolean 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);
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.mapping;
import static org.mockito.Mockito.*;
import java.util.Collections;
import javax.persistence.metamodel.Metamodel;
import org.junit.Test;
import org.springframework.data.annotation.Version;
/**
* Unit tests for {@link JpaMetamodelMappingContext}.
*
* @author Oliver Gierke
*/
public class JpaMetamodelMappingContextUnitTests {
/**
* @see DATAJPA-775
*/
@Test
public void jpaPersistentEntityRejectsSprignDataAtVersionAnnotation() {
Metamodel metamodel = mock(Metamodel.class);
JpaMetamodelMappingContext context = new JpaMetamodelMappingContext(Collections.singleton(metamodel));
context.getPersistentEntity(Sample.class);
}
static class Sample {
@Version Long version;
}
}

View File

@@ -158,15 +158,6 @@ public class JpaPersistentPropertyImplUnitTests {
assertThat(getProperty(JpaVersioned.class, "version").isVersionProperty(), is(true));
}
/**
* @see DATAJPA-605
* @see DATAJPA-775
*/
@Test(expected = IllegalArgumentException.class)
public void detectsSpringDataVersionAnnotation() {
getProperty(SpringDataVersioned.class, "version");
}
/**
* @see DATAJPA-664
*/