I have class BookSpecBuider that create JPA Specification from dto.
BookSpecBuider class:
public class BookSpecBuilder {
public Specification<Book> getSpec(BookSearchDto bookSearchDto) {
return (root, query, builder) -> {
List<Predicate> predicates = new ArrayList<>();
if (bookSearchDto.getGenre() != null) {
predicates.add(root.get("genre").in(bookSearchDto.getGenre()));
}
// another conditions
Genre is enum-class, so I had problem that in my BookSearchDto Genre is String and it had problem with mapping.
I change it in my if-block:
predicates.add(root.get("genre").in(Genre.valueOf(bookSearchDto.getGenre())));
But now I have problem with exceptions, if I get String of Genre, that my enum doesn't have. What is the best way to avoid this problem?