Search code examples
stringhibernateintegersql-likecriteria

Hibernate Criteria API condition "like" with integer


In my database I have a column "year" which is an integer.

How can I search by using Criteria API (not by HQL) which records contain, for example "196..." in the year column?

I think it should be Restrictions.like, but I got exception:

SEVERE: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer


Solution

  • a better and faster solution would be to use Criterion.sqlRestriction(String sql). You can form the sql as, "to_char(year) like '19%'", be aware that to_char is database specific. I dont think JPA supports to_char function.