I have following data structure stored in my database:
public class Company{
...
Long companyNumber;
...
}
Now I want to perform Like
search on this particular column. As stated in some similar questions, I'm trying to do a .as(String.class)
on criteria builder inside my specification class:
public class CompanySpecification implements Specification<Company> {
private String searchFilter;
@Override
public Predicate toPredicate(Root<Company> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
...
orPredicates.add(criteriaBuilder.like(root.get(Company_.companyNumber).as(String.class), "%" + searchFilter + "%"));
...
}
However the following implementation works only on the first character. For example:
If is search for string value 9
, this predicate returns all Companies where 9
is part of companyNumber (1119
, 1191
, 1911
, 9111
) but when I search for values with more then one character, no results are found. For example: if i search for 91
, no results are found even though there are companies in db with company numbers containing 91
: 1191
, 1911
, 9111
. I'm stuck on this, could you please point where have i made a mistake?
EDIT
added definition of searchFilter
, search filter is set via constructor:
CompanySpecification companySpecification = new CompanySpecification(searchParameter);
where search parameter is passed as request parameter