How I can cast Integer to a String for Criteria Query?
i.e.:
Predicate predicate = criteriaBuilder.like((Expression) root.get(filterKey), "%" + filterValue + "%");
I wan to create filter for dataTable component.... and i.e. for ID value I want to get filter like this:
If I type "1" I will get all items with ID containing "1" (223122 ID will be still correct)
Double-click the Last Name and City fields to add them to the query design grid. In the City field, add the “Like B*” criteria, and click Run.
CriteriaQuery. multiselect. This method takes one or more Selection items as parameters. The parameters specify the result returned by the Criteria Query. The multiselect method can be used to select a single entity, single or multiple values of the same entity or of different entities.
The CriteriaQuery interface defines the orderBy method to order query results according to attributes of an entity.
public interface CriteriaBuilder. Used to construct criteria queries, compound selections, expressions, predicates, orderings. Note that Predicate is used instead of Expression<Boolean> in this API in order to work around the fact that Java generics are not compatible with varags. Since: Java Persistence 2.0.
I have not used this myself, but this other question:
Using JPA 2.0 Criteria API and cast causes generated JPQL to fail in Hibernate
Suggests using .as(String.class)
after root.get(filterKey)
to accomplish what you want.
If you're using Hibernate, be aware of reported bugs
Try it
Predicate predicate = criteriaBuilder.like(root.get(filterKey).as(String.class), "%" + filterValue + "%");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With