Skip to content

Commit 9c42393

Browse files
committed
DATACMNS-180 - Don't consider Java transient fields transient for persistence.
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.
1 parent b483cbc commit 9c42393

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
/*
2-
* Copyright (c) 2011 by the original author(s).
2+
* Copyright 2011-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mapping.model;
1817

1918
import java.beans.PropertyDescriptor;
2019
import java.lang.annotation.Annotation;
2120
import java.lang.reflect.Field;
22-
import java.lang.reflect.Modifier;
2321
import java.util.ArrayList;
2422
import java.util.List;
2523
import java.util.Map;
@@ -34,7 +32,7 @@
3432
/**
3533
* Simple impementation of {@link PersistentProperty}.
3634
*
37-
* @author Jon Brisbin <jbrisbin@vmware.com>
35+
* @author Jon Brisbin
3836
* @author Oliver Gierke
3937
*/
4038
public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>> implements PersistentProperty<P> {
@@ -127,8 +125,12 @@ public String getSpelExpression() {
127125
return null;
128126
}
129127

128+
/*
129+
* (non-Javadoc)
130+
* @see org.springframework.data.mapping.PersistentProperty#isTransient()
131+
*/
130132
public boolean isTransient() {
131-
return Modifier.isTransient(field.getModifiers());
133+
return false;
132134
}
133135

134136
/*

spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2011-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.mapping.model;
217

318
import static org.hamcrest.Matchers.*;
@@ -13,6 +28,7 @@
1328
import org.junit.Test;
1429
import org.springframework.data.mapping.Association;
1530
import org.springframework.data.mapping.PersistentEntity;
31+
import org.springframework.data.mapping.PersistentProperty;
1632
import org.springframework.data.util.ClassTypeInformation;
1733
import org.springframework.data.util.TypeInformation;
1834
import org.springframework.util.ReflectionUtils;
@@ -94,8 +110,21 @@ public void considersPropertiesEqualIfFieldEquals() {
94110
assertThat(firstProperty.hashCode(), is(secondProperty.hashCode()));
95111
}
96112

113+
/**
114+
* @see DATACMNS-180
115+
*/
116+
@Test
117+
public void doesNotConsiderJavaTransientFieldsTransient() {
118+
119+
Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField");
120+
121+
PersistentProperty<?> property = new SamplePersistentProperty(transientField, null, entity, typeHolder);
122+
assertThat(property.isTransient(), is(false));
123+
}
124+
97125
class Generic<T> {
98126
T genericField;
127+
99128
}
100129

101130
class FirstConcrete extends Generic<String> {
@@ -117,6 +146,7 @@ class TestClassComplex {
117146
TestClassSet testClassSet;
118147
Map map;
119148
Collection collection;
149+
transient Object transientField;
120150
}
121151

122152
class SamplePersistentProperty extends AbstractPersistentProperty<SamplePersistentProperty> {

0 commit comments

Comments
 (0)