-->
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.  [ 10 posts ] 
Author Message
 Post subject: QBC Order By case-insensitive?
PostPosted: Tue Aug 16, 2005 12:55 pm 
Newbie

Joined: Tue Aug 16, 2005 12:49 pm
Posts: 9
Is there a way to use Query By Criteria to order the results without regard to case?

I want to return the results ordered but I'm getting upper case sorted before lower. I used the Criteria.addOrder( Order.asc( "field" ) ) method. With SQL I would use a lower( "field" ) statement to get the results I want. The only resolution I can see is to rewrite some code using straight sql.

I gotta believe that this can be done with hibernate but alas I couldn't find it on my own so this is a last resort.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:19 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Did you try HQL?
Or is it necessary for you to use Criteria API?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:30 pm 
Newbie

Joined: Tue Aug 16, 2005 12:49 pm
Posts: 9
I know I could do it in HQL its just that I have a utility class that performs queries for me using QBC with java reflection. I don't think I could do the same sort of thing with HQL or if I could it would be a massive re-write. I prefer to use QBC 's flexibility. If nothing else I could do the sorting in java using comparators.

It just seems that this would be a common thing that the folks at hibernate would have accounted for some way or another.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:37 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Well it's kind-of a hack, but one way to do this is as follows.


1. Add a "fake" property to your class mapping with a formula that uses whatever your database's equivilent of the lower() function is.
Code:
  <property name="fieldLowercase">
    <formula>lower(field)</formula>
  </property>


2. Add dummy a dummy getter/setter to your entity class (or a real getter/setter if you prefer).
Code:
  public void setFieldLowercase(String s) {}
  public String getFieldLowercase() { return null; }


3. Now you can use a Criteria like this.
Code:
  criteria.addOrder(Order.asc("fieldLowercase"));


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:44 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Then simply extend Order class.
It should be very simple.

Code:
public class Test extends Order {
       
    /**
     * Constructor for Order.
     */
    public Test(String propertyName, boolean ascending) {
        super(propertyName, ascending);
    }

    public String toString() {
        return "lower(" + super.toString() + ")";
    }

    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
        return "...";
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 2:57 pm 
Newbie

Joined: Tue Aug 16, 2005 12:49 pm
Posts: 9
I tried as you suggested alesj. I extended the class and overwrote the toString().

public String toString()
{
String superString = super.toString();
return "lower( " + superString.substring( 0, superString.indexOf( ' ') ) + " ) " + superString.substring( superString.indexOf( ' ' ) +1, superString.length() );
}

The toString() returns what I expected but hibernate must not be using it to generate the sql. It didn't fix my problem. Good idea though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 3:56 pm 
Newbie

Joined: Tue Aug 16, 2005 12:49 pm
Posts: 9
Ok thanks to alesj the solution is not within the toString() but to overwrite the toSqlString() method as follows (in case anyone cares).

public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, this.propertyName);
StringBuffer fragment = new StringBuffer();
for ( int i=0; i<columns.length; i++ ) {
fragment.append( " lower( " ).append( columns[0] ).append( " )" ).append( this.ascending ? " asc" : " desc" );
if ( i<columns.length-1 ) fragment.append(", ");
}
return fragment.toString();
}


the fragment.append inside the for loop is the only change to this method. Works great.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 4:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use 3.1, or simply subclass Order yourself.


Top
 Profile  
 
 Post subject: Easiest method
PostPosted: Wed Feb 11, 2009 9:50 am 
Newbie

Joined: Wed Feb 11, 2009 9:47 am
Posts: 1
Criteria.addOrder( Order.asc( "field" ).ignoreCase())

I am using hibernate 3.2.


Top
 Profile  
 
 Post subject: Re: QBC Order By case-insensitive?
PostPosted: Wed Jun 22, 2011 4:09 am 
Newbie

Joined: Thu Jun 04, 2009 10:02 am
Posts: 6
@rakesh2002

Have you tested this? I too am using 3.2 and it is NOT working... Items get sorted like this:

Aab
Baa
aab

Which is really annoying...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.