Skip to content

Commit

Permalink
Fix Query by Example to property handle Optional types that are empty.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gregturn committed Jan 7, 2022
1 parent 8d59e51 commit a53b152
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @author Mark Paluch
* @author Oliver Gierke
* @author Jens Schauder
* @author Greg Turnquist
* @since 1.10
*/
public class QueryByExamplePredicateBuilder {
Expand Down Expand Up @@ -146,6 +147,10 @@ static List<Predicate> getPredicates(String path, CriteriaBuilder cb, Path<?> fr

Object attributeValue = optionalValue.get();

if (attributeValue == Optional.empty()) {
continue;
}

if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)
|| (isAssociation(attribute) && !(from instanceof From))) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2008-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.domain.support;

import static org.assertj.core.api.Assertions.*;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.sample.AuditableUser;
import org.springframework.data.jpa.domain.sample.AuditorAwareStub;
import org.springframework.data.jpa.repository.sample.AnnotatedAuditableUserRepository;
import org.springframework.data.jpa.repository.sample.AuditableUserRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
* Integration test for {@link org.springframework.data.repository.query.QueryByExampleExecutor} involving
* {@link java.util.Optional}, using {@link org.springframework.data.domain.Auditable} as the means to verify it works
* properly.
*
* @author Greg Turnquist
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:auditing/auditing-query-by-example.xml")
public class AuditableQueryByExampleTests {

@Autowired AuditableUserRepository repository;
@Autowired AnnotatedAuditableUserRepository annotatedUserRepository;

@Autowired AuditorAwareStub auditorAware;

private AuditableUser user;

@BeforeEach
void setUp() {

user = new AuditableUser();
auditorAware.setAuditor(user);

repository.saveAndFlush(user);
}

@Test
void queryByExampleTreatsEmptyOptionalsLikeNulls() {

user.setFirstname("Greg");
user = repository.saveAndFlush(user);

AuditableUser probe = new AuditableUser();
probe.setFirstname("Greg");
Example<AuditableUser> example = Example.of(probe);

List<AuditableUser> results = repository.findAll(example);

assertThat(results).hasSize(1);
assertThat(results).extracting(AuditableUser::getFirstname).containsExactly("Greg");
}
}
20 changes: 20 additions & 0 deletions src/test/resources/auditing/auditing-query-by-example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<import resource="../infrastructure.xml" />

<jpa:auditing auditor-aware-ref="auditorAware" />

<bean id="auditorAware" class="org.springframework.data.jpa.domain.sample.AuditorAwareStub">
<constructor-arg ref="auditableUserRepository" />
</bean>

<jpa:repositories base-package="org.springframework.data.jpa.repository.sample" />

</beans>

0 comments on commit a53b152

Please sign in to comment.