DATACMNS-49, DATADOC-100 - Allow externalizing Mongo repository queries.
Adapted changes in Spring Data Commons for DATACMNS-49. We now automatically pick up classpath*:META-INF/mongo-named-queries.properties to find named queries when using the namespace. See AbstractMongoRepositoryIntegrationTests.findsPeopleByNamedQuery() for sample.
This commit is contained in:
@@ -69,6 +69,14 @@ public class SimpleMongoRepositoryConfiguration
|
||||
return new AutomaticMongoRepositoryConfiguration(interfaceName, this);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfig#getNamedQueriesLocation()
|
||||
*/
|
||||
public String getNamedQueriesLocation() {
|
||||
return "classpath*:META-INF/mongo-named-queries.properties";
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.QueryCreationListener;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
@@ -193,11 +194,15 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
|
||||
* org.springframework.data.repository.query.QueryLookupStrategy
|
||||
* #resolveQuery(java.lang.reflect.Method, java.lang.Class)
|
||||
*/
|
||||
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata) {
|
||||
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
|
||||
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, entityInformationCreator);
|
||||
|
||||
if (queryMethod.hasAnnotatedQuery()) {
|
||||
String namedQueryName = queryMethod.getNamedQueryName();
|
||||
|
||||
if (namedQueries.hasQuery(namedQueryName)) {
|
||||
String namedQuery = namedQueries.getQuery(namedQueryName);
|
||||
return new StringBasedMongoQuery(namedQuery, queryMethod, template);
|
||||
} else if (queryMethod.hasAnnotatedQuery()) {
|
||||
return new StringBasedMongoQuery(queryMethod, template);
|
||||
} else {
|
||||
return new PartTreeMongoQuery(queryMethod, template);
|
||||
|
||||
@@ -44,11 +44,15 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
|
||||
* @param method
|
||||
* @param template
|
||||
*/
|
||||
public StringBasedMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
public StringBasedMongoQuery(String query, MongoQueryMethod method, MongoTemplate template) {
|
||||
super(method, template);
|
||||
this.query = method.getAnnotatedQuery();
|
||||
this.query = query;
|
||||
this.fieldSpec = method.getFieldSpecification();
|
||||
}
|
||||
|
||||
public StringBasedMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
this(method.getAnnotatedQuery(), method, template);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
|
||||
@@ -254,4 +254,11 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(females.size(), is(1));
|
||||
assertThat(females.get(0), is(alicia));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsPeopleByNamedQuery() {
|
||||
List<Person> result = repository.findByNamedQuery("Dave");
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result, hasItem(dave));
|
||||
}
|
||||
}
|
||||
@@ -122,4 +122,6 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
List<Person> findByLocationWithin(Box box);
|
||||
|
||||
List<Person> findBySex(Sex sex);
|
||||
|
||||
List<Person> findByNamedQuery(String firstname);
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Person.findByNamedQuery={'firstname' : ?0}
|
||||
@@ -1,9 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
|
||||
|
||||
<mongo:db-factory dbname="repositories"/>
|
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.repository"/>
|
||||
@@ -16,6 +18,13 @@
|
||||
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
|
||||
<property name="template" ref="mongoTemplate"/>
|
||||
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository"/>
|
||||
<property name="namedQueries">
|
||||
<bean class="org.springframework.data.repository.core.support.PropertiesBasedNamedQueries">
|
||||
<constructor-arg>
|
||||
<util:properties location="META-INF/mongo-named-queries.properties" />
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user