Browse Source

Intercept NotWriteablePropertyException on setting bean values.

Using field access for populating objects in SimpleMongoConverter causes trouble when the class to be persisted contains a business getter without any parameters. This causes a PropertyDescriptor to be created for that method without a field actually backing it. So I manually catch NotWriteablePropertyException now to simply skip those invalid properties.
pull/1/head
Oliver Gierke 15 years ago
parent
commit
f90766d3f4
  1. 11
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoBeanWrapper.java

11
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoBeanWrapper.java

@ -19,6 +19,7 @@ import static org.springframework.beans.PropertyAccessorFactory.*; @@ -19,6 +19,7 @@ import static org.springframework.beans.PropertyAccessorFactory.*;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.ConfigurablePropertyAccessor;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.util.Assert;
@ -32,6 +33,7 @@ class MongoBeanWrapper { @@ -32,6 +33,7 @@ class MongoBeanWrapper {
private final ConfigurablePropertyAccessor accessor;
private final MongoPropertyDescriptors descriptors;
private final boolean fieldAccess;
/**
* Creates a new {@link MongoBeanWrapper} for the given target object and {@link ConversionService}.
@ -45,6 +47,7 @@ class MongoBeanWrapper { @@ -45,6 +47,7 @@ class MongoBeanWrapper {
Assert.notNull(target);
Assert.notNull(conversionService);
this.fieldAccess = fieldAccess;
this.accessor = fieldAccess ? forDirectFieldAccess(target) : forBeanPropertyAccess(target);
this.accessor.setConversionService(conversionService);
this.descriptors = new MongoPropertyDescriptors(target.getClass());
@ -78,6 +81,12 @@ class MongoBeanWrapper { @@ -78,6 +81,12 @@ class MongoBeanWrapper {
*/
public void setValue(MongoPropertyDescriptors.MongoPropertyDescriptor descriptor, Object value) {
Assert.notNull(descriptor);
accessor.setPropertyValue(descriptor.getName(), value);
try {
accessor.setPropertyValue(descriptor.getName(), value);
} catch (NotWritablePropertyException e) {
if (!fieldAccess) {
throw e;
}
}
}
}
Loading…
Cancel
Save