Databases Reference
In-Depth Information
The source code of the search() method looks like the following code:
@Transactional(readOnly = true)
@Override
public List<Contact> search(String searchTerm) {
String likePattern = buildLikePattern(dto.getSearchTerm());
Sort sortSpec = sortByLastNameAndFirstNameAsc();
return repository.findContacts(likePattern, sortSpec);
}
JPA Criteria API
In order to create queries by using JPA Criteria API, we had to modify the
ContactRepository interface to extend the JpaSpecificationExecutor<T>
interface. This gives us access to the List<Contact> findAll(Specification
spec, Sort sort) method that returns a sorted list of entities matching the given
search conditions.
Our implementation of the search() method of the RepositoryContactService
class is described as follows:
1. We get the used search criteria by using our specification builder class.
2.
We get the used Sort object.
3.
We will call the findAll() method of the ContactRepository and provide
the necessary parameters.
Our search() method is given as follows:
@Transactional(readOnly = true)
@Override
public List<Contact> search(String searchTerm) {
Specification<Contact> contactSpec = firstOrLastNameStartsWith(se
archTerm);
Sort sortSpec = sortByLastNameAndFirstNameAsc();
return repository.findAll(contactSpec, sortSpec);
}
Sorting with Querydsl
Extending the QuerydslPredicateExecutor<T> interface in our contact repository
gave us access to the Iterable<Contact> findAll(Predicate predicate,
OrderSpecifier<?>… orders) method that returns a sorted list of all entities that
match with the given search criteria.
 
Search WWH ::




Custom Search