Criterion.like limitations and numbers and dates

I am using Hibernate over mySQL. In mySQL, you can put LIKE in queries even by numbers (for example, double) parameters and dates, for example, you can write:

select * from sillytable where field like "3.33%"

and it will work. The problem is that I cannot do this in criteria using Restrictions. For example, it actually throws an exception, saying that it cannot use String for Date or Double.

How can I do this with this?

+3
source share
4 answers

I found that the only good solution I made was to use HQL. There is no clear way to do this in the criteria

+1
source

I think this is actually:

x => 3.33 and x < 3.34

In sleep mode it is:

Restriction.between("field", 3.33, 3.34)

(min <= expr AND expr <= max). , 3.33. (: , , 3.34 ​​ 3.33999.... , nextAfter .)

Restriction.between("field", 3.33, Math.nextAfter(3.34, -Double.MAX_VALUE));
0
Criteria crit;

try {
  crit = session.createCriteria(sillytable.class, "st");
  crit.add(Restrictions.like("field", number + "%"));
}
0
source

If you still want to adhere to the criteria Hibernate, use Restrictions.sqlRestrictioninstead Restrictions.like, as described below:

Restrictions.sqlRestriction(" CAST({alias}.field AS CHAR) LIKE ('%" + number + "%')")
0
source

All Articles