I'm using JPA Specification to retrieve data for my Spring boot app.
I have a mySQL query as below to get the list that I wanted:
select *
from TableA
order by colB = 'C' DESC, colB;
Result: -
no | colB
3 | C
1 | A
2 | B
4 | D
My question is, is it possible to convert into JPA Specification? I have already looked around but could not find what I wanted.
Page<Object> page = tableARepository.findAll(
(Specification<Object>) (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
if (sortConfig.getSpecification() != null) {
Predicate predicate = sortConfig.getSpecification().toPredicate(root, criteriaQuery, criteriaBuilder);
predicates.add(predicate);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
},
sortConfig.getPageable()
);