DATAJPA-923 - QueryByExamplePredicateBuilder now considers match mode.

We now inspect QueryByExamplePredicateBuilder.isAndMatching() to decide whether to concatenate the predicates built using and or or.

Related tickets: DATACMNS-879.
This commit is contained in:
Oliver Gierke
2016-07-02 08:59:19 +02:00
parent 30030a11c1
commit e4b9ba8e0e
2 changed files with 29 additions and 4 deletions

View File

@@ -52,6 +52,7 @@ import org.springframework.util.StringUtils;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Oliver Gierke
* @since 1.10
*/
public class QueryByExamplePredicateBuilder {
@@ -77,9 +78,10 @@ public class QueryByExamplePredicateBuilder {
Assert.notNull(cb, "CriteriaBuilder must not be null!");
Assert.notNull(example, "Example must not be null!");
ExampleMatcher matcher = example.getMatcher();
List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(),
example.getProbeType(), new ExampleMatcherAccessor(example.getMatcher()),
new PathNode("root", null, example.getProbe()));
example.getProbeType(), new ExampleMatcherAccessor(matcher), new PathNode("root", null, example.getProbe()));
if (predicates.isEmpty()) {
return cb.isTrue(cb.literal(true));
@@ -89,7 +91,9 @@ public class QueryByExamplePredicateBuilder {
return predicates.iterator().next();
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
Predicate[] array = predicates.toArray(new Predicate[predicates.size()]);
return matcher.isAllMatching() ? cb.and(array) : cb.or(array);
}
@SuppressWarnings({ "rawtypes", "unchecked" })

View File

@@ -43,6 +43,8 @@ import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.util.ObjectUtils;
/**
@@ -59,6 +61,7 @@ public class QueryByExamplePredicateBuilderUnitTests {
@Mock Predicate truePredicate;
@Mock Predicate dummyPredicate;
@Mock Predicate listPredicate;
@Mock Predicate orPredicate;
@Mock Path dummyPath;
Set<SingularAttribute<? super Person, ?>> personEntityAttribtues;
@@ -102,7 +105,8 @@ public class QueryByExamplePredicateBuilderUnitTests {
when(cb.literal(any(Boolean.class))).thenReturn(expressionMock);
when(cb.isTrue(eq(expressionMock))).thenReturn(truePredicate);
when(cb.and(Matchers.<Predicate> anyVararg())).thenReturn(listPredicate);
when(cb.and(Matchers.<Predicate>anyVararg())).thenReturn(listPredicate);
when(cb.or(Matchers.<Predicate>anyVararg())).thenReturn(orPredicate);
}
/**
@@ -166,6 +170,23 @@ public class QueryByExamplePredicateBuilderUnitTests {
verify(cb, times(1)).equal(any(Expression.class), eq(2L));
}
/**
* @see DATAJPA-879
*/
@Test
public void orConcatenatesPredicatesIfMatcherSpecifies() {
Person person = new Person();
person.firstname = "foo";
person.age = 2L;
Example<Person> example = of(person, ExampleMatcher.matchingAny());
assertThat(QueryByExamplePredicateBuilder.getPredicate(root, cb, example), equalTo(orPredicate));
verify(cb, times(1)).or(Matchers.<Predicate>anyVararg());
}
static class Person {
@Id Long id;