Browse Source

DATAMONGO-987 - Avoid creation of lazy-loading proxies for null-values.

We now avoid creating a lazy-loading proxy if we detect that the property-value in the backing DbObject for a @Lazy(true) annotated field is null.

Original pull request: #207.
1.5.x
Thomas Darimont 12 years ago committed by Oliver Gierke
parent
commit
f2f3faef08
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 21
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java

5
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -265,6 +265,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -265,6 +265,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
MongoPersistentProperty property = association.getInverse();
Object value = dbo.get(property.getName());
if (value == null) {
return;
}
DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
Object obj = dbRefResolver.resolveDbRef(property, dbref, new DbRefResolverCallback() {

21
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java

@ -429,6 +429,27 @@ public class DbRefMappingMongoConverterUnitTests { @@ -429,6 +429,27 @@ public class DbRefMappingMongoConverterUnitTests {
assertProxyIsResolved(result.dbRefEqualsAndHashcodeObjectMethodOverride2, true);
}
/**
* @see DATAMONGO-987
*/
@Test
public void shouldNotGenerateLazyLoadingProxyForNullValues() {
DBObject dbo = new BasicDBObject();
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs();
lazyDbRefs.id = "42";
converter.write(lazyDbRefs, dbo);
ClassWithLazyDbRefs result = converter.read(ClassWithLazyDbRefs.class, dbo);
assertThat(result.id, is(lazyDbRefs.id));
assertThat(result.dbRefToInterface, is(nullValue()));
assertThat(result.dbRefToConcreteCollection, is(nullValue()));
assertThat(result.dbRefToConcreteType, is(nullValue()));
assertThat(result.dbRefToConcreteTypeWithPersistenceConstructor, is(nullValue()));
assertThat(result.dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor, is(nullValue()));
}
private Object transport(Object result) {
return SerializationUtils.deserialize(SerializationUtils.serialize(result));
}

Loading…
Cancel
Save