From 157bead8c954c2ff118d4251b4ae1f3572d50a1a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 7 Jul 2014 19:54:47 +0200 Subject: [PATCH 1/3] DATACMNS-534 - Add reading / writing property information to PersistentProperty. Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4c10da972d..c43081962c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-commons - 1.9.0.BUILD-SNAPSHOT + 1.9.0.DATACMNS-534-SNAPSHOT Spring Data Core From de02a2523133dc84474064fb48696068e4578754 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 7 Jul 2014 21:39:47 +0200 Subject: [PATCH 2/3] DATACMNS-534 - Add reading / writing property information to PersistentProperty. We introduced optional annotations for @ReadingProperty and @WritingProperty which can be used for marking properties for being persisted to or only read from the store. Both annotations can be used as meta-annotations for building special ones like eg. @Score in Spring Data Mongodb. Having none of these annotations used will behave as if both annotations where present. This means that @ReadingProperty @WritingProperty String foo; is will return the same for property.isReading() and property.isWriting() as plain String foo; Both annotations are superseded by @Transient which will force isReading() and isWriting() to false; --- .../data/annotation/ReadingProperty.java | 35 ++++++++ .../data/annotation/WritingProperty.java | 35 ++++++++ .../data/mapping/PersistentEntity.java | 17 ++++ .../data/mapping/PersistentProperty.java | 23 +++++ .../model/AbstractPersistentProperty.java | 21 ++++- .../AnnotationBasedPersistentProperty.java | 43 ++++++++++ .../mapping/model/BasicPersistentEntity.java | 30 +++++++ ...ationBasedPersistentPropertyUnitTests.java | 83 +++++++++++++++++++ 8 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/data/annotation/ReadingProperty.java create mode 100644 src/main/java/org/springframework/data/annotation/WritingProperty.java diff --git a/src/main/java/org/springframework/data/annotation/ReadingProperty.java b/src/main/java/org/springframework/data/annotation/ReadingProperty.java new file mode 100644 index 0000000000..cbed77fa7e --- /dev/null +++ b/src/main/java/org/springframework/data/annotation/ReadingProperty.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014 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.annotation; + +import static java.lang.annotation.ElementType.*; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a field to be {@literal reading} for the mapping framework. Thus the property will not be persisted unless + * additionally marked as {@link WritingProperty}. + * + * @author Christoph Strobl + * @since 1.9 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { FIELD, METHOD, ANNOTATION_TYPE }) +public @interface ReadingProperty { + +} diff --git a/src/main/java/org/springframework/data/annotation/WritingProperty.java b/src/main/java/org/springframework/data/annotation/WritingProperty.java new file mode 100644 index 0000000000..6b9e008162 --- /dev/null +++ b/src/main/java/org/springframework/data/annotation/WritingProperty.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014 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.annotation; + +import static java.lang.annotation.ElementType.*; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a field to be {@literal writing} for the mapping framework. Thus the property will not be read back unless + * additionally marked as {@link ReadingProperty}. + * + * @author Christoph Strobl + * @since 1.9 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = { FIELD, METHOD, ANNOTATION_TYPE }) +public @interface WritingProperty { + +} diff --git a/src/main/java/org/springframework/data/mapping/PersistentEntity.java b/src/main/java/org/springframework/data/mapping/PersistentEntity.java index 543fe39b71..aeb48f9841 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/PersistentEntity.java @@ -27,6 +27,7 @@ * @author Graeme Rocher * @author Jon Brisbin * @author Patryk Wasik + * @author Christoph Strobl */ public interface PersistentEntity> { @@ -153,6 +154,22 @@ public interface PersistentEntity> { void doWithProperties(SimplePropertyHandler handler); + /** + * Applies the given {@link PropertyHandler} to all {@link PersistentProperty}s that shall be written to the store. + * + * @param handler + * @since 1.9 + */ + void doWithWritingProperties(PropertyHandler

handler); + + /** + * Applies the given {@link PropertyHandler} to all {@link PersistentProperty}s that can be read from the store. + * + * @param handler + * @since 1.9 + */ + void doWithReadingProperties(PropertyHandler

handler); + /** * Applies the given {@link AssociationHandler} to all {@link Association} contained in this {@link PersistentEntity}. * diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index 164b82ac26..c9b400723a 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -22,6 +22,8 @@ import java.util.Map; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.data.annotation.ReadingProperty; +import org.springframework.data.annotation.WritingProperty; import org.springframework.data.util.TypeInformation; /** @@ -147,8 +149,29 @@ public interface PersistentProperty

> { */ boolean isTransient(); + /** + * see {@link #isWriting()} + * + * @return + */ boolean shallBePersisted(); + /** + * Returns whether the property is considered to be a {@link ReadingProperty}. + * + * @return + * @since 1.9 + */ + boolean isReading(); + + /** + * Returns whether the property is considered to be a {@link WritingProperty}.
+ * + * @return + * @since 1.9 + */ + boolean isWriting(); + /** * Returns whether the property is an {@link Association}. * diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 09204345b0..c92e6e4f4e 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -36,6 +36,7 @@ * * @author Jon Brisbin * @author Oliver Gierke + * @author Christoph Strobl */ public abstract class AbstractPersistentProperty

> implements PersistentProperty

{ @@ -204,7 +205,7 @@ public boolean isTransient() { * @see org.springframework.data.mapping.PersistentProperty#shallBePersisted() */ public boolean shallBePersisted() { - return !isTransient(); + return isWriting(); } /* @@ -215,6 +216,24 @@ public boolean isAssociation() { return field == null ? false : AnnotationUtils.getAnnotation(field, Reference.class) != null; } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#isReading() + */ + @Override + public boolean isReading() { + return !isTransient(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#isWriting() + */ + @Override + public boolean isWriting() { + return !isTransient(); + } + public Association

getAssociation() { return association; } diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java index 871344c93f..be5289d5d8 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -29,9 +29,11 @@ import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.ReadingProperty; import org.springframework.data.annotation.Reference; import org.springframework.data.annotation.Transient; import org.springframework.data.annotation.Version; +import org.springframework.data.annotation.WritingProperty; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; @@ -41,6 +43,7 @@ * Special {@link PersistentProperty} that takes annotations at a property into account. * * @author Oliver Gierke + * @author Christoph Strobl */ public abstract class AnnotationBasedPersistentProperty

> extends AbstractPersistentProperty

{ @@ -169,6 +172,46 @@ public boolean isAssociation() { return !isTransient() && isAnnotationPresent(Reference.class); } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#isReading() + */ + @Override + public boolean isReading() { + + if (isTransient()) { + return false; + } + + if (isAnnotationPresent(ReadingProperty.class)) { + return true; + } else if (isAnnotationPresent(WritingProperty.class)) { + return false; + } + + return true; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#isWriting() + */ + @Override + public boolean isWriting() { + + if (isTransient()) { + return false; + } + + if (isAnnotationPresent(WritingProperty.class)) { + return true; + } else if (isAnnotationPresent(ReadingProperty.class)) { + return false; + } + + return true; + } + /** * Returns the annotation found for the current {@link AnnotationBasedPersistentProperty}. Will prefer field * annotations over ones found at getters or setters. diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index e0e4a42444..38c82ed4e9 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -296,6 +296,36 @@ public void doWithProperties(PropertyHandler

handler) { } } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentEntity#doWithWritingProperties(org.springframework.data.mapping.PropertyHandler) + */ + @Override + public void doWithWritingProperties(PropertyHandler

handler) { + + Assert.notNull(handler, "PropertyHandler must not be 'null'."); + for (P property : properties) { + if (property.isWriting()) { + handler.doWithPersistentProperty(property); + } + } + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentEntity#doWithReadingProperties(org.springframework.data.mapping.PropertyHandler) + */ + @Override + public void doWithReadingProperties(PropertyHandler

handler) { + + Assert.notNull(handler, "PropertyHandler must not be 'null'."); + for (P property : properties) { + if (property.isReading()) { + handler.doWithPersistentProperty(property); + } + } + } + /* * (non-Javadoc) * @see org.springframework.data.mapping.PersistentEntity#doWithProperties(org.springframework.data.mapping.PropertyHandler.Simple) diff --git a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java index 2dc0cea8df..02e364df51 100644 --- a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java @@ -30,6 +30,9 @@ import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.ReadingProperty; +import org.springframework.data.annotation.Transient; +import org.springframework.data.annotation.WritingProperty; import org.springframework.data.mapping.context.SampleMappingContext; import org.springframework.data.mapping.context.SamplePersistentProperty; import org.springframework.test.util.ReflectionTestUtils; @@ -38,6 +41,7 @@ * Unit tests for {@link AnnotationBasedPersistentProperty}. * * @author Oliver Gierke + * @author Christoph Strobl */ public class AnnotationBasedPersistentPropertyUnitTests

> { @@ -158,6 +162,71 @@ public void detectsAmbiguityCausedByFieldAnAccessorAnnotation() { context.getPersistentEntity(AnotherInvalidSample.class); } + /** + * @see DATACMNS-534 + */ + @Test + public void treatsNoAnnotationAsReadProperty() { + assertThat(getProperty(ReadingWritingProperties.class, "noAnnotations").isReading(), is(true)); + } + + /** + * @see DATACMNS-534 + */ + @Test + public void treatsNoAnnotationAsWriteProperty() { + assertThat(getProperty(ReadingWritingProperties.class, "noAnnotations").isWriting(), is(true)); + } + + /** + * @see DATACMNS-534 + */ + @Test + public void treatsTransientAsNotExisting() { + assertThat(getProperty(ReadingWritingProperties.class, "transientAnnotated"), nullValue()); + } + + /** + * @see DATACMNS-534 + */ + @Test + public void treatsReadingPropertyCorrectly() { + + SamplePersistentProperty property = getProperty(ReadingWritingProperties.class, "readingProperty"); + assertThat(property.isReading(), is(true)); + assertThat(property.isWriting(), is(false)); + } + + /** + * @see DATACMNS-534 + */ + @Test + public void treatsWritingPropertyCorrectly() { + + SamplePersistentProperty property = getProperty(ReadingWritingProperties.class, "writingProperty"); + assertThat(property.isReading(), is(false)); + assertThat(property.isWriting(), is(true)); + } + + /** + * @see DATACMNS-534 + */ + @Test + public void treatsRadingWritingPropertyCorrectly() { + + SamplePersistentProperty property = getProperty(ReadingWritingProperties.class, "readWriteProperty"); + assertThat(property.isReading(), is(true)); + assertThat(property.isWriting(), is(true)); + } + + /** + * @see DATACMNS-534 + */ + @Test + public void shallBePersistedShouldReturnFalseWhenPropertyIsReadingButNotWritingProperty() { + assertThat(getProperty(ReadingWritingProperties.class, "readingProperty").shallBePersisted(), is(false)); + } + @SuppressWarnings("unchecked") private Map, Annotation> getAnnotationCache(SamplePersistentProperty property) { return (Map, Annotation>) ReflectionTestUtils.getField(property, "annotationCache"); @@ -270,4 +339,18 @@ public String getLastname() { return lastname; } } + + static class ReadingWritingProperties { + + String noAnnotations; + + @Transient String transientAnnotated; + + @ReadingProperty String readingProperty; + + @WritingProperty String writingProperty; + + @ReadingProperty @WritingProperty String readWriteProperty; + + } } From cb79cbd464607049f5e043812fa278f172d61796 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 8 Jul 2014 20:17:43 +0200 Subject: [PATCH 3/3] DATACMNS-534 - Add reading / writing property information to PersistentProperty. We introduced @ReadOnlyProperty which can be used for excluding properties from being persisted. @Transient supersedes @ReadOnlyProperty. @ReadOnlyProperty can be used as meta-annotation for others like @TextScore in spring data mongodb. --- ...ingProperty.java => ReadOnlyProperty.java} | 5 +- .../data/annotation/WritingProperty.java | 35 ----------- .../data/mapping/PersistentEntity.java | 8 --- .../data/mapping/PersistentProperty.java | 15 +---- .../model/AbstractPersistentProperty.java | 15 +---- .../AnnotationBasedPersistentProperty.java | 35 ++--------- .../mapping/model/BasicPersistentEntity.java | 17 +----- ...ationBasedPersistentPropertyUnitTests.java | 61 ++++--------------- 8 files changed, 25 insertions(+), 166 deletions(-) rename src/main/java/org/springframework/data/annotation/{ReadingProperty.java => ReadOnlyProperty.java} (82%) delete mode 100644 src/main/java/org/springframework/data/annotation/WritingProperty.java diff --git a/src/main/java/org/springframework/data/annotation/ReadingProperty.java b/src/main/java/org/springframework/data/annotation/ReadOnlyProperty.java similarity index 82% rename from src/main/java/org/springframework/data/annotation/ReadingProperty.java rename to src/main/java/org/springframework/data/annotation/ReadOnlyProperty.java index cbed77fa7e..5c2ef57509 100644 --- a/src/main/java/org/springframework/data/annotation/ReadingProperty.java +++ b/src/main/java/org/springframework/data/annotation/ReadOnlyProperty.java @@ -22,14 +22,13 @@ import java.lang.annotation.Target; /** - * Marks a field to be {@literal reading} for the mapping framework. Thus the property will not be persisted unless - * additionally marked as {@link WritingProperty}. + * Marks a field to be {@literal read-only} for the mapping framework and will therefore not be persisted. * * @author Christoph Strobl * @since 1.9 */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { FIELD, METHOD, ANNOTATION_TYPE }) -public @interface ReadingProperty { +public @interface ReadOnlyProperty { } diff --git a/src/main/java/org/springframework/data/annotation/WritingProperty.java b/src/main/java/org/springframework/data/annotation/WritingProperty.java deleted file mode 100644 index 6b9e008162..0000000000 --- a/src/main/java/org/springframework/data/annotation/WritingProperty.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2014 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.annotation; - -import static java.lang.annotation.ElementType.*; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Marks a field to be {@literal writing} for the mapping framework. Thus the property will not be read back unless - * additionally marked as {@link ReadingProperty}. - * - * @author Christoph Strobl - * @since 1.9 - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(value = { FIELD, METHOD, ANNOTATION_TYPE }) -public @interface WritingProperty { - -} diff --git a/src/main/java/org/springframework/data/mapping/PersistentEntity.java b/src/main/java/org/springframework/data/mapping/PersistentEntity.java index aeb48f9841..d2fabeced5 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/PersistentEntity.java @@ -162,14 +162,6 @@ public interface PersistentEntity> { */ void doWithWritingProperties(PropertyHandler

handler); - /** - * Applies the given {@link PropertyHandler} to all {@link PersistentProperty}s that can be read from the store. - * - * @param handler - * @since 1.9 - */ - void doWithReadingProperties(PropertyHandler

handler); - /** * Applies the given {@link AssociationHandler} to all {@link Association} contained in this {@link PersistentEntity}. * diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java index c9b400723a..567ab56537 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java @@ -22,8 +22,7 @@ import java.util.Map; import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.data.annotation.ReadingProperty; -import org.springframework.data.annotation.WritingProperty; +import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.util.TypeInformation; /** @@ -157,20 +156,12 @@ public interface PersistentProperty

> { boolean shallBePersisted(); /** - * Returns whether the property is considered to be a {@link ReadingProperty}. + * Returns whether the property is considered to be a {@link ReadOnlyProperty}. * * @return * @since 1.9 */ - boolean isReading(); - - /** - * Returns whether the property is considered to be a {@link WritingProperty}.
- * - * @return - * @since 1.9 - */ - boolean isWriting(); + boolean isReadOnly(); /** * Returns whether the property is an {@link Association}. diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index c92e6e4f4e..e13940baab 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -205,7 +205,7 @@ public boolean isTransient() { * @see org.springframework.data.mapping.PersistentProperty#shallBePersisted() */ public boolean shallBePersisted() { - return isWriting(); + return !isTransient() && !isReadOnly(); } /* @@ -221,17 +221,8 @@ public boolean isAssociation() { * @see org.springframework.data.mapping.PersistentProperty#isReading() */ @Override - public boolean isReading() { - return !isTransient(); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.PersistentProperty#isWriting() - */ - @Override - public boolean isWriting() { - return !isTransient(); + public boolean isReadOnly() { + return isTransient(); } public Association

getAssociation() { diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java index be5289d5d8..962d60fafc 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -29,11 +29,10 @@ import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.ReadingProperty; +import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.Reference; import org.springframework.data.annotation.Transient; import org.springframework.data.annotation.Version; -import org.springframework.data.annotation.WritingProperty; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; @@ -177,39 +176,13 @@ public boolean isAssociation() { * @see org.springframework.data.mapping.PersistentProperty#isReading() */ @Override - public boolean isReading() { + public boolean isReadOnly() { - if (isTransient()) { - return false; - } - - if (isAnnotationPresent(ReadingProperty.class)) { - return true; - } else if (isAnnotationPresent(WritingProperty.class)) { - return false; - } - - return true; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.PersistentProperty#isWriting() - */ - @Override - public boolean isWriting() { - - if (isTransient()) { - return false; - } - - if (isAnnotationPresent(WritingProperty.class)) { + if (isTransient() || isAnnotationPresent(ReadOnlyProperty.class)) { return true; - } else if (isAnnotationPresent(ReadingProperty.class)) { - return false; } - return true; + return false; } /** diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index 38c82ed4e9..b5b2900137 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -305,22 +305,7 @@ public void doWithWritingProperties(PropertyHandler

handler) { Assert.notNull(handler, "PropertyHandler must not be 'null'."); for (P property : properties) { - if (property.isWriting()) { - handler.doWithPersistentProperty(property); - } - } - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.PersistentEntity#doWithReadingProperties(org.springframework.data.mapping.PropertyHandler) - */ - @Override - public void doWithReadingProperties(PropertyHandler

handler) { - - Assert.notNull(handler, "PropertyHandler must not be 'null'."); - for (P property : properties) { - if (property.isReading()) { + if (property.shallBePersisted()) { handler.doWithPersistentProperty(property); } } diff --git a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java index 02e364df51..13f853c862 100644 --- a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java @@ -30,9 +30,8 @@ import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; import org.springframework.data.annotation.Id; -import org.springframework.data.annotation.ReadingProperty; +import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.Transient; -import org.springframework.data.annotation.WritingProperty; import org.springframework.data.mapping.context.SampleMappingContext; import org.springframework.data.mapping.context.SamplePersistentProperty; import org.springframework.test.util.ReflectionTestUtils; @@ -166,16 +165,8 @@ public void detectsAmbiguityCausedByFieldAnAccessorAnnotation() { * @see DATACMNS-534 */ @Test - public void treatsNoAnnotationAsReadProperty() { - assertThat(getProperty(ReadingWritingProperties.class, "noAnnotations").isReading(), is(true)); - } - - /** - * @see DATACMNS-534 - */ - @Test - public void treatsNoAnnotationAsWriteProperty() { - assertThat(getProperty(ReadingWritingProperties.class, "noAnnotations").isWriting(), is(true)); + public void treatsNoAnnotationCorrectly() { + assertThat(getProperty(ClassWithReadOnlyProperties.class, "noAnnotations").isReadOnly(), is(false)); } /** @@ -183,48 +174,25 @@ public void treatsNoAnnotationAsWriteProperty() { */ @Test public void treatsTransientAsNotExisting() { - assertThat(getProperty(ReadingWritingProperties.class, "transientAnnotated"), nullValue()); + assertThat(getProperty(ClassWithReadOnlyProperties.class, "transientProperty"), nullValue()); } /** * @see DATACMNS-534 */ @Test - public void treatsReadingPropertyCorrectly() { + public void treatsReadOnlyPropertyCorrectly() { - SamplePersistentProperty property = getProperty(ReadingWritingProperties.class, "readingProperty"); - assertThat(property.isReading(), is(true)); - assertThat(property.isWriting(), is(false)); + SamplePersistentProperty property = getProperty(ClassWithReadOnlyProperties.class, "readOnlyProperty"); + assertThat(property.isReadOnly(), is(true)); } /** * @see DATACMNS-534 */ @Test - public void treatsWritingPropertyCorrectly() { - - SamplePersistentProperty property = getProperty(ReadingWritingProperties.class, "writingProperty"); - assertThat(property.isReading(), is(false)); - assertThat(property.isWriting(), is(true)); - } - - /** - * @see DATACMNS-534 - */ - @Test - public void treatsRadingWritingPropertyCorrectly() { - - SamplePersistentProperty property = getProperty(ReadingWritingProperties.class, "readWriteProperty"); - assertThat(property.isReading(), is(true)); - assertThat(property.isWriting(), is(true)); - } - - /** - * @see DATACMNS-534 - */ - @Test - public void shallBePersistedShouldReturnFalseWhenPropertyIsReadingButNotWritingProperty() { - assertThat(getProperty(ReadingWritingProperties.class, "readingProperty").shallBePersisted(), is(false)); + public void shallBePersistedShouldReturnFalseWhenPropertyIsReadOnlyProperty() { + assertThat(getProperty(ClassWithReadOnlyProperties.class, "readOnlyProperty").shallBePersisted(), is(false)); } @SuppressWarnings("unchecked") @@ -340,17 +308,12 @@ public String getLastname() { } } - static class ReadingWritingProperties { + static class ClassWithReadOnlyProperties { String noAnnotations; - @Transient String transientAnnotated; - - @ReadingProperty String readingProperty; - - @WritingProperty String writingProperty; - - @ReadingProperty @WritingProperty String readWriteProperty; + @Transient String transientProperty; + @ReadOnlyProperty String readOnlyProperty; } }