Skip to content

Commit c8bb46f

Browse files
committed
DATAMONGO-413 - Fixed bug in MongoQueryCreator.
MongoQueryCreator used the outdated OrQuery class to concatenate parts with OR. Refactored the class to use the Criteria.orOperator(…) method and deprecated the OrQuery class to be removed in the 1.1.x branch.
1 parent f82de36 commit c8bb46f

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/OrQuery.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2011-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,12 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21+
/**
22+
* @deprecated use {@link Criteria#orOperator(Criteria...)} instead.
23+
* @author Thomas Risberg
24+
* @author Oliver Gierke
25+
*/
26+
@Deprecated
2127
public class OrQuery extends Query {
2228

2329
public OrQuery(Query... q) {
@@ -31,5 +37,4 @@ private static Criteria getOrCriteria(Query[] queries) {
3137
}
3238
return new Criteria(criteriaList, "$or");
3339
}
34-
3540
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
3232
import org.springframework.data.mongodb.core.query.Criteria;
3333
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
34-
import org.springframework.data.mongodb.core.query.OrQuery;
3534
import org.springframework.data.mongodb.core.query.Query;
3635
import org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.PotentiallyConvertingIterator;
3736
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
@@ -45,7 +44,7 @@
4544
*
4645
* @author Oliver Gierke
4746
*/
48-
class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
47+
class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
4948

5049
private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class);
5150
private final MongoParameterAccessor accessor;
@@ -92,7 +91,7 @@ public MongoQueryCreator(PartTree tree, ConvertingParameterAccessor accessor,
9291
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator)
9392
*/
9493
@Override
95-
protected Query create(Part part, Iterator<Object> iterator) {
94+
protected Criteria create(Part part, Iterator<Object> iterator) {
9695

9796
if (isGeoNearQuery && part.getType().equals(Type.NEAR)) {
9897
return null;
@@ -103,15 +102,15 @@ protected Query create(Part part, Iterator<Object> iterator) {
103102
where(path.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)),
104103
(PotentiallyConvertingIterator) iterator);
105104

106-
return new Query(criteria);
105+
return criteria;
107106
}
108107

109108
/*
110109
* (non-Javadoc)
111110
* @see org.springframework.data.repository.query.parser.AbstractQueryCreator#and(org.springframework.data.repository.query.parser.Part, java.lang.Object, java.util.Iterator)
112111
*/
113112
@Override
114-
protected Query and(Part part, Query base, Iterator<Object> iterator) {
113+
protected Criteria and(Part part, Criteria base, Iterator<Object> iterator) {
115114

116115
if (base == null) {
117116
return create(part, iterator);
@@ -122,7 +121,8 @@ protected Query and(Part part, Query base, Iterator<Object> iterator) {
122121
Criteria criteria = from(part.getType(),
123122
where(path2.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)),
124123
(PotentiallyConvertingIterator) iterator);
125-
return base.addCriteria(criteria);
124+
125+
return criteria.andOperator(criteria);
126126
}
127127

128128
/*
@@ -133,8 +133,10 @@ protected Query and(Part part, Query base, Iterator<Object> iterator) {
133133
* #or(java.lang.Object, java.lang.Object)
134134
*/
135135
@Override
136-
protected Query or(Query base, Query query) {
137-
return new OrQuery(new Query[] { base, query });
136+
protected Criteria or(Criteria base, Criteria criteria) {
137+
138+
Criteria result = new Criteria();
139+
return result.orOperator(base, criteria);
138140
}
139141

140142
/*
@@ -145,12 +147,13 @@ protected Query or(Query base, Query query) {
145147
* #complete(java.lang.Object, org.springframework.data.domain.Sort)
146148
*/
147149
@Override
148-
protected Query complete(Query query, Sort sort) {
150+
protected Query complete(Criteria criteria, Sort sort) {
149151

150-
if (query == null) {
152+
if (criteria == null) {
151153
return null;
152154
}
153155

156+
Query query = new Query(criteria);
154157
QueryUtils.applySorting(query, sort);
155158

156159
if (LOG.isDebugEnabled()) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -211,6 +211,20 @@ public void createsFalseClauseCorrectly() {
211211
assertThat(creator.createQuery().getQueryObject(), is(query.getQueryObject()));
212212
}
213213

214+
/**
215+
* @see DATAMONGO
216+
*/
217+
@Test
218+
public void createsOrQueryCorrectly() {
219+
220+
PartTree tree = new PartTree("findByFirstNameOrAge", Person.class);
221+
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "Dave", 42), context);
222+
223+
Query query = creator.createQuery();
224+
assertThat(query.getQueryObject(),
225+
is(query(new Criteria().orOperator(where("firstName").is("Dave"), where("age").is(42))).getQueryObject()));
226+
}
227+
214228
private void assertBindsDistanceToQuery(Point point, Distance distance, Query reference) throws Exception {
215229

216230
when(converter.convertToMongoType("Dave")).thenReturn("Dave");

0 commit comments

Comments
 (0)