Извините, ребята, я ошибся в другой части кода, полный код:
@Getter
@Setter
public class SearchCriteria {
private String key;
private Object value;
private SearchOperation operation;
public SearchCriteria(String key, Object value, SearchOperation operation) {
this.key = key;
this.value = value;
this.operation = operation;
}
}
SearchOperation Enum
:
public enum SearchOperation {
GREATER_THAN,
LESS_THAN,
GREATER_THAN_EQUAL,
LESS_THAN_EQUAL,
NOT_EQUAL,
EQUAL,
MATCH,
MATCH_END,
BETWEEN_DATE,
GREATER_THAN_DATE,
LESS_THAN_DATE;
}
И GenericSpecification:
public class GenericSpecification<T> implements Specification<T> {
private SimpleDateFormat localeIta = new SimpleDateFormat("dd/MM/yyyy");
private List<SearchCriteria> list;
public GenericSpecification() {
this.list = new ArrayList<>();
}
public void add(SearchCriteria criteria) {
list.add(criteria);
}
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
for (SearchCriteria criteria : list) {
switch(criteria.getOperation()) {
....
case BETWEEN_DATE: {
Path<Date> entityDate = root.get(criteria.getKey());
predicates.add(builder.between(entityDate, getComparingDates((String[])criteria.getValue(), 0), getComparingDates((String[])criteria.getValue(), 1)));
}
break; /** I FORGET THIS BREAK **/
}
private java.sql.Date getComparingDates(String[] dates, int order) {
java.sql.Date result = null;
try {
if (order == 0) {
java.util.Date from = localeIta.parse(dates[0].toString());
result = new java.sql.Date(from.getTime());
} else {
java.util.Date to = localeIta.parse(dates[1].toString());
result = new java.sql.Date(to.getTime());
}
} catch (ParseException e) {
log.error(e.getMessage());
}
return result;
}
Теперь отлично работает! Спасибо