Case-insensitive equals using Hibernate Criteria

后端 未结 7 1492
旧时难觅i
旧时难觅i 2021-01-31 13:32

I\'ve seen Restrictions.ilike(\'property\', \'%value%\'), but would like to generate SQL like: lower(property) = \'value\'. Any ideas?

I used:

Restri         


        
7条回答
  •  天涯浪人
    2021-01-31 14:13

    As Andy's answer suggests, this for case-insensitive searches but it is also works through to Hibernate version 4.1:

    crit(Restrictions.eq("firstName", firstName).ignoreCase());
    

    Versions 4.1.1 and later of Hibernate do not support the ignoreCase() method on Restriction.eq(). For that, we have to use ilike with MatchMode.

    Criteria crit = session.createCriteria(ENTITY.class);
    crit.add(Restrictions.ilike('PROPERTY NAME', 'VALUE', MatchMode.ANYWHERE));
    

    As an example, for a USER entity with id, name, surname properties, a case-insensitive search based on name will be:

    Criteria crit = session.createCriteria(USER.class);
    crit.add(Restrictions.ilike('name', 'Satyam', MatchMode.ANYWHERE));
    

    This will return all results, case insensitive.

提交回复
热议问题