Advanced and dynamic searching with Spring Data JPA
Tags: java, springboot, jpa, hibernate
Published: 2024-01-29 08:19:35

I have often been asked to develop advanced search services. By advanced search I mean searches in which it’s possible to apply multiple filters on all (or almost all) fields such as: like, between, in, greater than, etc.

So imagine having to build a service, based on one or more entities, capable of offering an endpoint that can be called like this (start keeping an eye out for special suffixes _< propertyName >< **_suffix** >_):

```bash
curl - request GET \
 - url 'http://www.myexampledomain.com/persons?
firstName=Biagio
&lastName_startsWith=Toz
&birthDate_gte=19910101
&country_in=IT,FR,DE
&company.name_in=Microsoft,Apple
&company.employees_between=500,5000'
```

or

```bash
curl --request GET \
--url 'http://www.myexampledomain.com/persons?
firstName_endsWith=gio
&lastName_in=Tozzi,Totti
&birthDate_lt=19980101
&_offset=0
&_limit=100
&birthDate_sort=ASC'
```

If you are using [JPA](https://jakarta.ee/specifications/persistence/) in a Spring Boot project you can now develop this search service with just a few lines of code thanks to [JPA Search Helper](https://github.com/biagioT/jpa-search-helper)! Let me explain what it is.

## JPA Search Helper
### First step: @Searchable annotation
Start by applying the _@Searchable_ annotation to the fields in your DTO, or alternatively your JPA entity, that you want to make available for search.
```java
public class Person {

    @Searchable
    private String firstName;

    @Searchable
    private String lastName;

    @Searchable(entityFieldKey = "dateOfBirth")
    private Date birthDate;

    @Searchable
    private String country;
    
    private Company company;

    @Data
    public static class Company {
        
        @Searchable(entityFieldKey=companyEntity.name)
        private String name;
              
        @Searchable(entityFieldKey=companyEntity.employeesCount)
        private int employees;

    }

}
```
The annotation allows you to specify:
Core properties:
- _entityFieldKey_: the name of the field defined on the entity bean (not to be specified if using the annotation on the entity bean). If not specified the key will be the field name.
- _targetType_: the managed object type by entity. If not specified the librariy tries to obtain it based on field type (es. `Integer` field without target type definition will be _INTEGER_). If there is no type compatible with those managed, it will be managed as a string. Managed types: _STRING, INTEGER, DOUBLE, FLOAT, LONG, BIGDECIMAL, BOOLEAN, DATE, LOCALDATE, LOCALDATETIME, LOCALTIME, OFFSETDATETIME, OFFSETTIME_.
Validation properties:
- _datePattern_: only for DATE target type. Defines the date pattern to use.
- _maxSize_, _minSize_: maximum/minimum length of the value
- _maxDigits_, _minDigits_: only for numeric types. Maximum/minimum number of digits.
- _regexPattern_: regex pattern.
- _decimalFormat_: only for decimal numeric types. Default #.##
Continuing the example, our entity classes:
```java
@Entity
@Data
public class PersonEntity {

    @Id
    private Long id;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "BIRTH_DATE")
    private Date dateOfBirth;

    @Column(name = "COUNTRY")
    private String country;
        
    @OneToOne
    private CompanyEntity companyEntity;

}

@Entity
@Data
public class CompanyEntity {

    @Id
    private Long id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "COUNT")
    private Integer employeesCount;

}
```

### Second and last step: JPASearchRepository<?>
Your Spring JPA repository must extend `JPASearchRepository<?>`:
```java
@Repository
public interface PersonRepository extends JpaRepository<PersonEntity, Long>, JPASearchRepository<PersonEntity> {

}
```
Well, let’s build the filters and feed them to the repository:
```java
// ...

Map<String, String> filters = new HashMap<>();
filters.put("firstName_eq", "Biagio");
filters.put("lastName_startsWith", "Toz");
filters.put("birthDate_gte", "19910101"); 
filters.put("country_in", "IT,FR,DE");
filters.put("company.name_in", "Microsoft,Apple");
filters.put("company.employees_between", "500,5000");

// Without pagination
List<PersonEntity> fullSearch = personRepository.findAll(filters, Person.class);

filters.put("birthDate_sort" : "ASC");
filters.put("_limit", "10");
filters.put("_offset", "0");

// With pagination
Page<PersonEntity> sortedAndPaginatedSearch = personRepository.findAllWithPaginationAndSorting(filters, Person.class);

// ...
```

Basically you just need to define a map whose key is made up of < _fieldName_ >< **__suffix_** > and search value. The complete list of suffixes, i.e. available filters, is [here](https://github.com/biagioT/jpa-search-helper?tab=readme-ov-file#managed-search-filters).

> Note 1: if no suffix is ​​specified the search is done in equal (_eq)

> Note 2: In the example I applied the @Searchable annotation on the DTO fields. Alternatively, it’s possible to apply them directly on the entity.

### A pseudo-real implementation in a Spring Boot project
Service/Manager bean:
```java
@Service 
public class PersonManager { 	
    	
    @Autowired 		
    private PersonRepository personRepository;
    		 		
    public List<Person> find(Map<String, String> filters) {
      return personRepository.findAllWithPaginationAndSorting(filters, Person.class).stream().map(this::toDTO).toList(); 
    } 

    private static Person toDTO(PersonEntity personEntity) {
        // ...
    }

}
```

Controller:
```java
@RestController
public class MyController {
    
    @Autowired 		
    private PersonManager personManager;
         
    @GetMapping(path="/persons", produces = MediaType.APPLICATION_JSON_VALUE)  
    public List<Person> findPersons(@RequestParam Map<String, String> requestParams) {  
        return personManager.find(requestParams);  
    }
}
```

_..et voilà les jeux sont faits_

### Extra
The library allows you to force join fetch.

> A “fetch” join allows associations or collections of values to be initialized along with their parent objects using a single select.

That’s how:
```java
// ...

Map<String, JoinFetch> fetches = Map.of("companyEntity", JoinFetch.LEFT);
personRepository.findAll(filters, Person.class, fetches);

// ...
```
That’s all.. for now!