Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hibernate's Criteria API, can I use concat with ilike restrictions?

Intuitively, what I want to do is something like this:

Restrictions.ilike("concat(user.firstName, ' ', user.lastName)",
    text.toLowerCase() + "%")

However, I know that this won't work, because concat(user.firstName, ' ', user.lastName) is not a property that Hibernate understands, even though this is what I would have typed in a regular SQL statement as part of the where clause.

Is there any easy way to make this where restriction with Hibernate Criteria API, or do I have to fall back on good-old HSQL?

Thanks

like image 205
egervari Avatar asked Mar 19 '23 09:03

egervari


1 Answers

One solution is to define fullName property in your User class.

@Formula(value="first_name || ' ' || last_name")
private String fullName;

And then use that filed in Criteria,

criteria.add(Restrictions.ilike("fullName", text.toLowerCase() + "%"));
like image 58
Petar Butkovic Avatar answered Mar 21 '23 19:03

Petar Butkovic