DATACOUCH-226 - Enable strict @Field mapping

If the MappingCouchbaseConverter strict mode is enabled and no annotation is present, the property will not be eligible for document mapping.

This allows to specifically ignore some attributes in Couchbase only. For generally ignoring attributes in any Spring Data backing store, prefer the standard `@Transient`.

Closes #112
This commit is contained in:
Geoffrey Mina
2016-05-20 17:48:07 +02:00
committed by Simon Baslé
parent 811c024041
commit 7a57a235d8
2 changed files with 75 additions and 0 deletions

View File

@@ -23,6 +23,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import com.couchbase.client.java.repository.annotation.Field;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.CollectionFactory;
@@ -61,6 +63,7 @@ import org.springframework.util.CollectionUtils;
*
* @author Michael Nitschinger
* @author Oliver Gierke
* @author Geoffrey Mina
*/
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
implements ApplicationContextAware {
@@ -99,6 +102,11 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
*/
private final SpELContext spELContext;
/**
* Enable strict @Field checking on mapper
*/
private boolean enableStrictFieldChecking = false;
/**
* Create a new {@link MappingCouchbaseConverter}.
*
@@ -135,6 +143,20 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
return typeMapper.getTypeKey();
}
/**
* Toggles strict checking of the couchbase {@link Field} annotation. If enabled,
* strict checking will prevent non-annotated properties to be serialized. This only
* applies to the Couchbase datastore, allowing other Spring Data datastores to still
* deal with the property.
*
* @param enableStrictFieldChecking true to only consider Field-annotated properties for
* Couchbase serialization.
* @see DATACOUCH-226
*/
public void setEnableStrictFieldChecking(boolean enableStrictFieldChecking){
this.enableStrictFieldChecking = enableStrictFieldChecking;
}
@Override
public <R> R read(final Class<R> clazz, final CouchbaseDocument source) {
return read(ClassTypeInformation.from(clazz), source, null);
@@ -434,6 +456,8 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
if (prop.equals(idProperty) || (versionProperty != null && prop.equals(versionProperty))) {
return;
} else if(enableStrictFieldChecking && !prop.isAnnotationPresent(Field.class)){
return;
}
Object propertyObj = accessor.getProperty(prop, prop.getType());