Skip to content

Commit ff4d0e0

Browse files
committed
Ignore fields with Optional.empty when performing Query by Example.
The Auditable interface introduces Optional getters, which when combined with Query by Example result in cryptic errors. By ignoring a probe's field that contains an Optional.empty, Query by Example works properly. Closes #2176.
1 parent 8d59e51 commit ff4d0e0

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* @author Mark Paluch
5757
* @author Oliver Gierke
5858
* @author Jens Schauder
59+
* @author Greg Turnquist
5960
* @since 1.10
6061
*/
6162
public class QueryByExamplePredicateBuilder {
@@ -146,6 +147,10 @@ static List<Predicate> getPredicates(String path, CriteriaBuilder cb, Path<?> fr
146147

147148
Object attributeValue = optionalValue.get();
148149

150+
if (attributeValue == Optional.empty()) {
151+
continue;
152+
}
153+
149154
if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)
150155
|| (isAssociation(attribute) && !(from instanceof From))) {
151156

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2008-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.domain.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.List;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.data.domain.Example;
27+
import org.springframework.data.jpa.domain.sample.AuditableUser;
28+
import org.springframework.data.jpa.domain.sample.AuditorAwareStub;
29+
import org.springframework.data.jpa.repository.sample.AnnotatedAuditableUserRepository;
30+
import org.springframework.data.jpa.repository.sample.AuditableUserRepository;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit.jupiter.SpringExtension;
33+
34+
/**
35+
* Integration test for {@link org.springframework.data.repository.query.QueryByExampleExecutor} involving
36+
* {@link java.util.Optional}, using {@link org.springframework.data.domain.Auditable} as the means to verify it works
37+
* properly.
38+
*
39+
* @author Greg Turnquist
40+
*/
41+
@ExtendWith(SpringExtension.class)
42+
@ContextConfiguration("classpath:auditing/auditing-query-by-example.xml")
43+
public class AuditableQueryByExampleTests {
44+
45+
@Autowired AuditableUserRepository repository;
46+
@Autowired AnnotatedAuditableUserRepository annotatedUserRepository;
47+
48+
@Autowired AuditorAwareStub auditorAware;
49+
50+
private AuditableUser user;
51+
52+
@BeforeEach
53+
void setUp() {
54+
55+
user = new AuditableUser();
56+
auditorAware.setAuditor(user);
57+
58+
repository.saveAndFlush(user);
59+
}
60+
61+
@Test
62+
void queryByExampleTreatsEmptyOptionalsLikeNulls() {
63+
64+
user.setFirstname("Greg");
65+
user = repository.saveAndFlush(user);
66+
67+
AuditableUser probe = new AuditableUser();
68+
probe.setFirstname("Greg");
69+
Example<AuditableUser> example = Example.of(probe);
70+
71+
List<AuditableUser> results = repository.findAll(example);
72+
73+
assertThat(results).hasSize(1);
74+
assertThat(results).extracting(AuditableUser::getFirstname).containsExactly("Greg");
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd
8+
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
9+
10+
<import resource="../infrastructure.xml" />
11+
12+
<jpa:auditing auditor-aware-ref="auditorAware" />
13+
14+
<bean id="auditorAware" class="org.springframework.data.jpa.domain.sample.AuditorAwareStub">
15+
<constructor-arg ref="auditableUserRepository" />
16+
</bean>
17+
18+
<jpa:repositories base-package="org.springframework.data.jpa.repository.sample" />
19+
20+
</beans>

0 commit comments

Comments
 (0)