From e18a89651ea363a04f01cb4ff532fc506797869c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 6 May 2015 10:44:36 +0200 Subject: [PATCH] DATAJPA-716 - JpaPersistentPropertyImpl now considers read-only properties. We now inspect the updateable attribute of @Column and @OrderColumn to expose its configuration via PersistentProperty.isWritable(). --- .../mapping/JpaPersistentPropertyImpl.java | 42 ++++++++++++++++++- .../JpaPersistentPropertyImplUnitTests.java | 13 ++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java index 0189a2e73..d02781aec 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java @@ -25,6 +25,7 @@ import java.util.Set; import javax.persistence.Access; import javax.persistence.AccessType; +import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Embedded; import javax.persistence.EmbeddedId; @@ -33,6 +34,7 @@ import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; +import javax.persistence.OrderColumn; import javax.persistence.Transient; import javax.persistence.Version; import javax.persistence.metamodel.Metamodel; @@ -58,6 +60,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty> ASSOCIATION_ANNOTATIONS; private static final Collection> ID_ANNOTATIONS; + private static final Collection> UPDATEABLE_ANNOTATIONS; static { @@ -74,12 +77,19 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty>(); + annotations.add(Column.class); + annotations.add(OrderColumn.class); + + UPDATEABLE_ANNOTATIONS = Collections.unmodifiableSet(annotations); } private final Metamodel metamodel; private final Boolean usePropertyAccess; private final TypeInformation associationTargetType; + private final boolean updateable; /** * Creates a new {@link JpaPersistentPropertyImpl} @@ -100,6 +110,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty annotationType : UPDATEABLE_ANNOTATIONS) { + + Annotation annotation = findAnnotation(annotationType); + + if (annotation != null && AnnotationUtils.getValue(annotation, "updateable") == Boolean.TRUE) { + return true; + } + } + + return false; + } } diff --git a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java index a8d00e1ee..58af4a16c 100644 --- a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java @@ -22,6 +22,7 @@ import java.util.Collections; import javax.persistence.Access; import javax.persistence.AccessType; +import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Embedded; import javax.persistence.ManyToOne; @@ -181,6 +182,14 @@ public class JpaPersistentPropertyImplUnitTests { assertThat(entityType.iterator().next(), is((TypeInformation) ClassTypeInformation.from(Implementation.class))); } + /** + * @see DATAJPA-716 + */ + @Test + public void considersNonUpdateablePropertyNotWriteable() { + assertThat(getProperty(WithReadOnly.class, "name").isWritable(), is(false)); + } + private JpaPersistentProperty getProperty(Class ownerType, String propertyName) { JpaPersistentEntity entity = context.getPersistentEntity(ownerType); @@ -299,4 +308,8 @@ public class JpaPersistentPropertyImplUnitTests { static interface Api {} static class Implementation {} + + static class WithReadOnly { + @Column(updatable = false) String name; + } }