Skip to content

Commit e63034c

Browse files
committed
DATAJPA-218 - Use true is true predicate if probe yields to empty criteria.
1 parent 7b24496 commit e63034c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Examp
8282
new PathNode("root", null, example.getProbe()));
8383

8484
if (predicates.isEmpty()) {
85-
return cb.isTrue(cb.literal(false));
85+
return cb.isTrue(cb.literal(true));
8686
}
8787

8888
if (predicates.size() == 1) {

src/test/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilderUnitTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class QueryByExamplePredicateBuilderUnitTests {
5656
@Mock Root root;
5757
@Mock EntityType<Person> personEntityType;
5858
@Mock Expression expressionMock;
59-
@Mock Predicate falsePredicate;
59+
@Mock Predicate truePredicate;
6060
@Mock Predicate dummyPredicate;
6161
@Mock Predicate listPredicate;
6262
@Mock Path dummyPath;
@@ -101,7 +101,7 @@ public void setUp() {
101101
when(cb.like(any(Expression.class), any(String.class))).thenReturn(dummyPredicate);
102102

103103
when(cb.literal(any(Boolean.class))).thenReturn(expressionMock);
104-
when(cb.isTrue(eq(expressionMock))).thenReturn(falsePredicate);
104+
when(cb.isTrue(eq(expressionMock))).thenReturn(truePredicate);
105105
when(cb.and(Matchers.<Predicate> anyVararg())).thenReturn(listPredicate);
106106
}
107107

@@ -133,8 +133,8 @@ public void getPredicateShouldThrowExceptionOnNullExample() {
133133
* @see DATAJPA-218
134134
*/
135135
@Test
136-
public void emptyCriteriaListShouldResultFalsePredicate() {
137-
assertThat(QueryByExamplePredicateBuilder.getPredicate(root, cb, of(new Person())), equalTo(falsePredicate));
136+
public void emptyCriteriaListShouldResultTruePredicate() {
137+
assertThat(QueryByExamplePredicateBuilder.getPredicate(root, cb, of(new Person())), equalTo(truePredicate));
138138
}
139139

140140
/**

src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,23 @@ public void findAllByExample() {
19971997
assertThat(users.get(0), is(firstUser));
19981998
}
19991999

2000+
/**
2001+
* @see DATAJPA-218
2002+
*/
2003+
@Test
2004+
public void findAllByExampleWithEmptyProbe() {
2005+
2006+
flushTestUsers();
2007+
2008+
User prototype = new User();
2009+
prototype.setCreatedAt(null);
2010+
2011+
List<User> users = repository
2012+
.findAll(of(prototype, ExampleSpec.untyped().withIgnorePaths("age", "createdAt", "active")));
2013+
2014+
assertThat(users, hasSize(4));
2015+
}
2016+
20002017
/**
20012018
* @see DATAJPA-218
20022019
*/

0 commit comments

Comments
 (0)