Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Criteria Query - Search for Integer using "like"

Tags:

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)

like image 952
Marcin Petrów Avatar asked Apr 19 '12 14:04

Marcin Petrów


People also ask

How use like criteria query?

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.

What is criteria query multiselect?

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.

Which method can be used for ordering the results using criteria queries?

The CriteriaQuery interface defines the orderBy method to order query results according to attributes of an entity.

What is CriteriaBuilder in Java?

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.


2 Answers

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

like image 143
Doug Moscrop Avatar answered Oct 26 '22 21:10

Doug Moscrop


Try it

Predicate predicate = criteriaBuilder.like(root.get(filterKey).as(String.class),                     "%" + filterValue + "%"); 
like image 27
Gianluca Musa Avatar answered Oct 26 '22 20:10

Gianluca Musa