Skip to main content

Custom Finders

If no finder options are specified, a finder is expanded based only on its operator pairs with no customization options.

// example for a `findByNameEq` finder
public SearchResult<LocatorData> findByNameEq(ParameterContext<SearchDetails<LocatorFindByNameEqDetails>>
searchParameter) {
SearchDetails < LocatorFindByNameEqDetails > searchDetails = searchParameter.getValue();

LocatorFindByNameEqDetails details = searchDetails.getDetails();

JPAQueryBuilder queryBuilder = createQueryBuilder()
.addParameter(QueryParameter.createStringParameter("name", "LIKE", "Name", details.getName()))
.addSortFields(searchDetails.getSortFields())
;

return fetchData((ParameterContext) searchParameter, queryBuilder);
}

Customizable Finders

A finder with a finderOption isCustomizableFinder expands fieldOperatorPairs in the same way a plain finder would, but includes customization anchors to further manipulate both the query, as well as the fetched data.

// example for `findByStatusSe` with `isCustomizableFinder` option
public SearchResult<LocatorData> findByStatusSe(ParameterContext<SearchDetails<LocatorFindByStatusSeDetails>>
searchParameter) {
SearchDetails < LocatorFindByStatusSeDetails > searchDetails = searchParameter.getValue();

LocatorFindByStatusSeDetails details = searchDetails.getDetails();

JPAQueryBuilder queryBuilder = createQueryBuilder()
.addParameter(QueryParameter.createStringParameter("status", "=", "Status", details.getStatus()))
// anchor:custom-findByStatusSe-query:start
// anchor:custom-findByStatusSe-query:end
;

List<SortField> sortFields = searchDetails.getSortFields();
// anchor:custom-findByStatusSe-sorting:start
// anchor:custom-findByStatusSe-sorting:end
queryBuilder.addSortFields(sortFields);

SearchResult<LocatorData> searchResult = fetchData((ParameterContext) searchParameter, queryBuilder);
// anchor:custom-findByStatusSe-post-fetch:start
// anchor:custom-findByStatusSe-post-fetch:end

return searchResult;
}
Option
isCustomizableFinder Finder

Adds additional custom anchors to the find method.

<options>
<isCustomizableFinder/>
</options>

Custom Finders

Finally, a finder with a finderOption isCustomFinder expands only a shell method to be implemented by developer.

Note that the Finder's details class (e.g. LocatorFindByCustomStatusDetails) will still expand accessors for field operator pairs, if any were defined in the model.

// example for `findByCustomStatus` finder with `isCustomFinder` option and a single `Eq:status` field operator pair.
@SuppressWarnings({"unchecked", "rawtypes"})
public SearchResult<LocatorData> findByCustomStatus(ParameterContext<SearchDetails<LocatorFindByCustomStatusDetails>>
searchParameter) {
SearchDetails < LocatorFindByCustomStatusDetails > searchDetails = searchParameter.getValue();
SearchResult<LocatorData> searchResult = new SearchResult.SearchError<LocatorData>();
//anchor:custom-findByCustomStatus-finder:start
//anchor:custom-findByCustomStatus-finder:end

return searchResult;
}
Option
isCustomFinder Finder

Replaces finder implementation with a single custom anchor.

<options>
<isCustomFinder/>
</options>