-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HQL query with case-insensitive 'where' statement?
PostPosted: Fri Apr 21, 2006 3:33 am 
Beginner
Beginner

Joined: Thu Apr 13, 2006 12:56 pm
Posts: 23
Is it possible to write an HQL query with case-insensitive 'where' criteria? i.e.:

Code:
class User{
   private String name;

   public String getName(){
      return this.name;
   }
}




Let's say I am trying to find all Users with name 'Joe', but I do not care about case (i.e. I'd like to match "JOE" and "joe"):

Code:
User u = new User();
u.setName("Joe");

String hqlQuery = "from User as usr where usr.name = "+u.getName();
//the above query will return users with 'name' "Joe" only, but not "joe" or "JOE" etc.

I'd like 'where' comparison to be case-insensitive. How would you rewrite the above query to achieve that?

Would the following work?:
Code:
String hqlQuery = "from User as usr where lower(usr.name) = "+u.getName().toLowerCase();


I realize that this can be achieved by using Criteria class, but in my current application, I have to use HQL.

Thanks,
-nikita

Hibernate version:
3.1
Name and version of the database you are using:
MySQL 4.1 (InnoDB)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 5:50 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
Quote:
Would the following work?:
String hqlQuery = "from User as usr where lower(usr.name) = "+u.getName().toLowerCase();


Yes it would. See also:

http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html#queryhql-expressions


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.