DATAMONGO-315 - MongoTemplate.findOne(query) methods ignore SortOrder on query
This commit is contained in:
@@ -429,7 +429,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
}
|
||||
|
||||
public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
|
||||
return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass);
|
||||
if (query.getSortObject() == null) {
|
||||
return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass);
|
||||
} else {
|
||||
query.limit(1);
|
||||
List<T> results = find(query, entityClass, collectionName);
|
||||
return (results.isEmpty() ? null : results.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
// Find methods that take a Query to express the query and that return a List of objects.
|
||||
|
||||
@@ -117,6 +117,7 @@ public class MongoTemplateTests {
|
||||
|
||||
protected void cleanDb() {
|
||||
template.dropCollection(template.getCollectionName(Person.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWithAList.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeObjectId.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWith_idPropertyOfTypeString.class));
|
||||
template.dropCollection(template.getCollectionName(PersonWithIdPropertyOfTypeObjectId.class));
|
||||
@@ -742,8 +743,36 @@ public class MongoTemplateTests {
|
||||
assertThat(p4.getWishList().size(), is(1));
|
||||
assertThat(p4.getFriends().size(), is(1));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindOneWithSort() {
|
||||
PersonWithAList p = new PersonWithAList();
|
||||
p.setFirstName("Sven");
|
||||
p.setAge(22);
|
||||
template.insert(p);
|
||||
|
||||
PersonWithAList p2 = new PersonWithAList();
|
||||
p2.setFirstName("Erik");
|
||||
p2.setAge(21);
|
||||
template.insert(p2);
|
||||
|
||||
PersonWithAList p3 = new PersonWithAList();
|
||||
p3.setFirstName("Mark");
|
||||
p3.setAge(40);
|
||||
template.insert(p3);
|
||||
|
||||
|
||||
//test query with a sort
|
||||
Query q2 = new Query(Criteria.where("age").gt(10));
|
||||
q2.sort().on("age", Order.DESCENDING);
|
||||
PersonWithAList p5 = template.findOne(q2, PersonWithAList.class);
|
||||
assertThat(p5.getFirstName(), is("Mark"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUsingSlaveOk() throws Exception {
|
||||
this.template.execute("slaveOkTest", new CollectionCallback<Object>() {
|
||||
|
||||
Reference in New Issue
Block a user