Monday, April 25, 2011

Case-insensitive equals using Hibernate Criteria

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

I used:

Restrictions.eq("email", email).ignoreCase()

since Expression is deprecated. The SimpleExpression will call toLowerCase() on the value, so it is not necessary to do it beforehand.

See: SimpleExpression source

From stackoverflow
  • I'm not absolutely sure, but when you use Restriction.eq you obtain a SimpleExpression object, and that object suppports an ignoreCase() operation which I've never tried using but sounds like it could make a difference.

    Kudos to Hibernate for not documenting what this method actually does.

    duffymo : Careful Uri, Gavin King or Christian Baer might be watching... 8) They tend to be a little defensive about criticism on the Hibernate forums.
    Uri : I've doing my Ph.D. research on the usability and correctness of JavaDocs... So completely omitting the JavaDocs on that methods takes away from my work ;)
  • Be careful of using ilike because it would allow someone to enter things like "test%" and match. I use the following to do a case-insensitive equal in one app:

    ...
    Criteria crit=session.createCriteria(Event.class);
    crit.add(Expression.eq("rsvpCode","test1").ignoreCase());
    ...
    

0 comments:

Post a Comment