Tuesday, November 23, 2010

Case Insensitive withCriteria

I found this easy way to do a case insensitive query on the DB using with Criteria. Unfortunately Grails documentation does not mention this, but remember you are using Hibernate. So using .ignoreCase() on the eq works perfectly.


 personList = Person.withCriteria {
  if (params.firstName)
   eq('firstName', params.firstName).ignoreCase()
  if (params.lastName)
   eq('lastName', params.lastName).ignoreCase()
  if (params.login)
   eq('login', params.login).ignoreCase()
  if (params.personHashId)
   like('personHashId', params.personHashId)
  maxResults(Constants.SEARCH_MAX_RESULTS)
 }

2 comments:

  1. Thank you dekay,just what I needed. -Bose

    ReplyDelete
  2. Is there a way to do the ignore case for the like operator as well?
    Something like,

    Person.withCriteria {
    or{
    like('firstName', params.fname)
    like('lastName', params.lname)
    }
    }

    How do i apply the ignoreCase to the above code?

    ReplyDelete