From 031129e46199fa8ce3c012a5185b455dfd6933eb Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 6 Jun 2012 12:33:17 +0200 Subject: [PATCH] DATACMNS-180 - Don't consider Java transient fields transient for persistence. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So far we have considered Java transient fields as transient through the isTransient() method on PersistentProperty. This resulted in the properties not being persisted eventually. We now consider all fields as non-transient by default as we actually have to persist them as the object cannot be re-created completely without doing so as the usage of the transient keyword might rely on the readObject(…) method inside the object which would actually reconstitute the transient field's state but we of course cannot call this method actually. --- .../model/AbstractPersistentProperty.java | 14 +++++---- .../AbstractPersistentPropertyUnitTests.java | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 6afbed9db..dd10a26dc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2012 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 + * 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, @@ -13,13 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mapping.model; import java.beans.PropertyDescriptor; import java.lang.annotation.Annotation; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,7 +32,7 @@ import org.springframework.util.Assert; /** * Simple impementation of {@link PersistentProperty}. * - * @author Jon Brisbin + * @author Jon Brisbin * @author Oliver Gierke */ public abstract class AbstractPersistentProperty

> implements PersistentProperty

{ @@ -127,8 +125,12 @@ public abstract class AbstractPersistentProperty

return null; } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.PersistentProperty#isTransient() + */ public boolean isTransient() { - return Modifier.isTransient(field.getModifiers()); + return false; } /* diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index bd839efd5..2aef42918 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -1,3 +1,18 @@ +/* + * Copyright 2011-2012 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.mapping.model; import static org.hamcrest.Matchers.*; @@ -13,6 +28,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; import org.springframework.util.ReflectionUtils; @@ -94,8 +110,21 @@ public class AbstractPersistentPropertyUnitTests { assertThat(firstProperty.hashCode(), is(secondProperty.hashCode())); } + /** + * @see DATACMNS-180 + */ + @Test + public void doesNotConsiderJavaTransientFieldsTransient() { + + Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField"); + + PersistentProperty property = new SamplePersistentProperty(transientField, null, entity, typeHolder); + assertThat(property.isTransient(), is(false)); + } + class Generic { T genericField; + } class FirstConcrete extends Generic { @@ -117,6 +146,7 @@ public class AbstractPersistentPropertyUnitTests { TestClassSet testClassSet; Map map; Collection collection; + transient Object transientField; } class SamplePersistentProperty extends AbstractPersistentProperty {