DATADOC-248 - Customized MappingMongoEntityInformation to allow taking a custom collection.
In case a repository query method returns a domain type not assignable to the repository domain type we have to use the repositories domain type to determine the collection but still use the returned domain type to hand to the unmarshalling. Thus, we need to set up a custom MongoEntityInformation to reflect this scenario. Extended MappingMongoEntityInformation to allow manually defining a custom collection name. EntityInformationCreator was extended accordingly and MongoQueryMethod now sets up the EntityInformation accordingly.
This commit is contained in:
@@ -17,14 +17,14 @@ package org.springframework.data.mongodb.repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import org.springframework.data.mapping.model.BeanWrapper;
|
||||
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
||||
import org.springframework.data.repository.core.support.AbstractEntityInformation;
|
||||
|
||||
/**
|
||||
* {@link MongoEntityInformation} implementation using a {@link BasicMongoPersistentEntity} instance to lookup the
|
||||
* necessary information.
|
||||
* {@link MongoEntityInformation} implementation using a {@link MongoPersistentEntity} instance to lookup the necessary
|
||||
* information. Can be configured with a custom collection to be returned which will trump the one returned by the
|
||||
* {@link MongoPersistentEntity} if given.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@@ -32,16 +32,28 @@ public class MappingMongoEntityInformation<T, ID extends Serializable> extends A
|
||||
implements MongoEntityInformation<T, ID> {
|
||||
|
||||
private final MongoPersistentEntity<T> entityMetadata;
|
||||
private final String customCollectionName;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingMongoEntityInformation} for the given {@link MongoPersistentEntity}.
|
||||
*
|
||||
* @param domainClass
|
||||
* @param entity
|
||||
* @param entity must not be {@literal null}.
|
||||
*/
|
||||
public MappingMongoEntityInformation(MongoPersistentEntity<T> entity) {
|
||||
this(entity, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingMongoEntityInformation} for the given {@link MongoPersistentEntity} and custom
|
||||
* collection name.
|
||||
*
|
||||
* @param entity must not be {@literal null}.
|
||||
* @param customCollectionName
|
||||
*/
|
||||
public MappingMongoEntityInformation(MongoPersistentEntity<T> entity, String customCollectionName) {
|
||||
super(entity.getType());
|
||||
this.entityMetadata = entity;
|
||||
this.customCollectionName = customCollectionName;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -71,7 +83,7 @@ public class MappingMongoEntityInformation<T, ID extends Serializable> extends A
|
||||
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
|
||||
*/
|
||||
public String getCollectionName() {
|
||||
return entityMetadata.getCollection();
|
||||
return customCollectionName == null ? entityMetadata.getCollection() : customCollectionName;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -44,7 +44,8 @@ class MongoQueryMethod extends QueryMethod {
|
||||
public MongoQueryMethod(Method method, RepositoryMetadata metadata, EntityInformationCreator entityInformationCreator) {
|
||||
super(method, metadata);
|
||||
this.method = method;
|
||||
this.entityInformation = entityInformationCreator.getEntityInformation(ClassUtils.getReturnedDomainClass(method));
|
||||
this.entityInformation = entityInformationCreator.getEntityInformation(
|
||||
ClassUtils.getReturnedDomainClass(method), getDomainClass());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -64,14 +64,13 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
/**
|
||||
* Configures the {@link MongoTemplate} to be used.
|
||||
*
|
||||
* @param template
|
||||
* the template to set
|
||||
* @param template the template to set
|
||||
*/
|
||||
public void setTemplate(MongoTemplate template) {
|
||||
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Configures whether to automatically create indexes for the properties referenced in a query method.
|
||||
*
|
||||
@@ -92,14 +91,14 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
protected final RepositoryFactorySupport createRepositoryFactory() {
|
||||
|
||||
RepositoryFactorySupport factory = getFactoryInstance(template);
|
||||
|
||||
|
||||
if (createIndexesForQueryMethods) {
|
||||
factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(template));
|
||||
}
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates and initializes a {@link RepositoryFactorySupport} instance.
|
||||
*
|
||||
@@ -137,16 +136,14 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
/**
|
||||
* Creates a new {@link MongoRepositoryFactory} with the given {@link MongoTemplate} and {@link MappingContext}.
|
||||
*
|
||||
* @param template
|
||||
* must not be {@literal null}
|
||||
* @param template must not be {@literal null}
|
||||
* @param mappingContext
|
||||
*/
|
||||
public MongoRepositoryFactory(MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template);
|
||||
this.template = template;
|
||||
this.entityInformationCreator = new EntityInformationCreator(template.getConverter()
|
||||
.getMappingContext());
|
||||
this.entityInformationCreator = new EntityInformationCreator(template.getConverter().getMappingContext());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -222,7 +219,7 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, entityInformationCreator);
|
||||
String namedQueryName = queryMethod.getNamedQueryName();
|
||||
|
||||
|
||||
if (namedQueries.hasQuery(namedQueryName)) {
|
||||
String namedQuery = namedQueries.getQuery(namedQueryName);
|
||||
return new StringBasedMongoQuery(namedQuery, queryMethod, template);
|
||||
@@ -271,16 +268,24 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
|
||||
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
|
||||
|
||||
public EntityInformationCreator(MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
|
||||
public EntityInformationCreator(
|
||||
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
|
||||
Assert.notNull(mappingContext);
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||
return getEntityInformation(domainClass, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass,
|
||||
Class<?> collectionClass) {
|
||||
MongoPersistentEntity<T> persistentEntity = (MongoPersistentEntity<T>) mappingContext
|
||||
.getPersistentEntity(domainClass);
|
||||
return new MappingMongoEntityInformation<T, ID>(persistentEntity);
|
||||
String customCollectionName = collectionClass == null ? null : mappingContext
|
||||
.getPersistentEntity(collectionClass).getCollection();
|
||||
return new MappingMongoEntityInformation<T, ID>(persistentEntity, customCollectionName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,7 +329,7 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
Order order = toOrder(sort, property);
|
||||
index.on(property, order);
|
||||
}
|
||||
|
||||
|
||||
// Add fixed sorting criteria to index
|
||||
if (sort != null) {
|
||||
for (Sort.Order order : sort) {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingMongoEntityInformation}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MappingMongoEntityInformationUnitTests {
|
||||
|
||||
@Mock
|
||||
MongoPersistentEntity<Person> info;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
when(info.getType()).thenReturn(Person.class);
|
||||
when(info.getCollection()).thenReturn("Person");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesEntityCollectionIfNoCustomOneGiven() {
|
||||
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info);
|
||||
assertThat(information.getCollectionName(), is("Person"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesCustomCollectionIfGiven() {
|
||||
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info, "foobar");
|
||||
assertThat(information.getCollectionName(), is("foobar"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository;
|
||||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.repository.MongoRepositoryFactoryBean.EntityInformationCreator;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
|
||||
/**
|
||||
* Unit test for {@link MongoQueryMethod}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MongoQueryMethodUnitTests {
|
||||
|
||||
EntityInformationCreator creator;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MongoMappingContext context = new MongoMappingContext();
|
||||
creator = new MongoRepositoryFactoryBean.EntityInformationCreator(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsCollectionFromRepoTypeIfReturnTypeNotAssignable() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("method");
|
||||
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), creator);
|
||||
MongoEntityInformation<?,?> entityInformation = queryMethod.getEntityInformation();
|
||||
|
||||
assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Address.class)));
|
||||
assertThat(entityInformation.getCollectionName(), is("contact"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsCollectionFromReturnTypeIfReturnTypeAssignable() throws Exception {
|
||||
|
||||
Method method = SampleRepository2.class.getMethod("method");
|
||||
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), creator);
|
||||
MongoEntityInformation<?,?> entityInformation = queryMethod.getEntityInformation();
|
||||
|
||||
assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Person.class)));
|
||||
assertThat(entityInformation.getCollectionName(), is("person"));
|
||||
}
|
||||
|
||||
|
||||
interface SampleRepository extends Repository<Contact, Long> {
|
||||
|
||||
List<Address> method();
|
||||
}
|
||||
|
||||
interface SampleRepository2 extends Repository<Contact, Long> {
|
||||
|
||||
List<Person> method();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user