DATACOUCH-161 - Improve multitemplate mapping configuration
The configuration step made it look like a new mapping was necessary in overrides, whereas it is actually possible and clearer to mutate the base mapping. Added a convenience method to get the MappingContext from the repository mapping.
This commit is contained in:
@@ -161,12 +161,27 @@ public abstract class AbstractCouchbaseConfiguration {
|
||||
/**
|
||||
* Creates the {@link RepositoryOperationsMapping} bean which will be used by the framework to choose which
|
||||
* {@link CouchbaseOperations} should back which {@link CouchbaseRepository}.
|
||||
* Override {@link #configureRepositoryOperationsMapping(RepositoryOperationsMapping)} in order to customize this.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Bean(name = BeanNames.REPO_OPERATIONS_MAPPING)
|
||||
public RepositoryOperationsMapping repositoryOperationsMapping() throws Exception {
|
||||
return new RepositoryOperationsMapping(couchbaseTemplate());
|
||||
//create a base mapping that associates all repositories to the default template
|
||||
RepositoryOperationsMapping baseMapping = new RepositoryOperationsMapping(couchbaseTemplate());
|
||||
//let the user tune it
|
||||
configureRepositoryOperationsMapping(baseMapping);
|
||||
return baseMapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* In order to customize the mapping between repositories/entity types to couchbase templates,
|
||||
* use the provided mapping's api (eg. in order to have different buckets backing different repositories).
|
||||
*
|
||||
* @param mapping the default mapping (will associate all repositories to the default template).
|
||||
*/
|
||||
protected void configureRepositoryOperationsMapping(RepositoryOperationsMapping mapping) {
|
||||
//NO_OP
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,6 +20,10 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.data.couchbase.core.CouchbaseOperations;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A utility class for configuration allowing to tell which {@link CouchbaseOperations} should be backing
|
||||
@@ -33,12 +37,14 @@ public class RepositoryOperationsMapping {
|
||||
private Map<String, CouchbaseOperations> byRepository = new HashMap<String, CouchbaseOperations>();
|
||||
private Map<String, CouchbaseOperations> byEntity = new HashMap<String, CouchbaseOperations>();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new mapping, setting the default fallback to use by otherwise non mapped repositories.
|
||||
*
|
||||
* @param defaultOperations the default fallback couchbase operations.
|
||||
*/
|
||||
public RepositoryOperationsMapping(CouchbaseOperations defaultOperations) {
|
||||
Assert.notNull(defaultOperations);
|
||||
this.defaultOperations = defaultOperations;
|
||||
}
|
||||
|
||||
@@ -49,6 +55,7 @@ public class RepositoryOperationsMapping {
|
||||
* @return the mapping, for chaining.
|
||||
*/
|
||||
public RepositoryOperationsMapping setDefault(CouchbaseOperations aDefault) {
|
||||
Assert.notNull(aDefault);
|
||||
this.defaultOperations = aDefault;
|
||||
return this;
|
||||
}
|
||||
@@ -85,6 +92,15 @@ public class RepositoryOperationsMapping {
|
||||
return defaultOperations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link MappingContext} to use in repositories. It is extracted from the default {@link CouchbaseOperations}.
|
||||
*
|
||||
* @return the mapping context.
|
||||
*/
|
||||
public MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> getMappingContext() {
|
||||
return defaultOperations.getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a repository interface and its domain type, resolves which {@link CouchbaseOperations} it should be backed with.
|
||||
*
|
||||
|
||||
@@ -91,7 +91,7 @@ public class CouchbaseRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
this.couchbaseOperationsMapping = couchbaseOperationsMapping;
|
||||
this.indexManager = indexManager;
|
||||
mappingContext = this.couchbaseOperationsMapping.getDefault().getConverter().getMappingContext();
|
||||
mappingContext = this.couchbaseOperationsMapping.getMappingContext();
|
||||
viewPostProcessor = ViewPostProcessor.INSTANCE;
|
||||
|
||||
addRepositoryProxyPostProcessor(viewPostProcessor);
|
||||
@@ -157,7 +157,6 @@ public class CouchbaseRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
//for other repos, they might also need N1QL if they don't have only @View methods
|
||||
if (!needsN1ql) {
|
||||
|
||||
for (Method method : metadata.getQueryMethods()) {
|
||||
|
||||
boolean hasN1ql = AnnotationUtils.findAnnotation(method, Query.class) != null;
|
||||
|
||||
@@ -54,7 +54,7 @@ public class CouchbaseRepositoryFactoryBean<T extends Repository<S, ID>, S, ID e
|
||||
|
||||
public void setCouchbaseOperationsMapping(final RepositoryOperationsMapping mapping) {
|
||||
this.operationsMapping = mapping;
|
||||
setMappingContext(operationsMapping.getDefault().getConverter().getMappingContext());
|
||||
setMappingContext(operationsMapping.getMappingContext());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user