-->
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.  [ 5 posts ] 
Author Message
 Post subject: Case insensitive sorting with Criteria API
PostPosted: Wed Aug 03, 2005 3:06 pm 
Newbie

Joined: Wed Aug 03, 2005 2:55 pm
Posts: 3
I am using Criteria API in Hibernate 3 and need to perform case insensitive sort, I know how to do it with HQL but the requirement is to use Criteria API.

something like:

criteria.addOrder(Order.asc("propertyName") will translate into SQL:

order by column_name asc

what we need to achieve is SQL:

order by lower(column_name) asc

Hibernate's Order class will not understand something like
Order.asc("lower(property_name)") and there is no flag to pass to indicate case insensitive sorting.

Any suggestions/comments on how to do this with Criteria API.

Thank you,

Eugene


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 3:27 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
One way to do it would be to add a new property to your class mapping for your lowercase property.

Code:
<property name="lowerCasePropertyName" insert="false" lazy="true" update="false">
<formula>
    ( lower(some_column_name) )
</formula>
</property>


You'll have to have at least dummy getters and setters in your Java class. If you don't care about always getting the property back as lowercase, you can add the formula directly to the real property itself.

Then in your code, you'd have something like this:

Code:
criteria.addOrder(Order.asc("lowerCasePropertyName"));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 4:05 pm 
Newbie

Joined: Wed Aug 03, 2005 2:55 pm
Posts: 3
thanks for quick reply, i tried it and this works, the only negative side effect of this approach is the when doing Hibernate includes the formula in the select, even though update/insert is set to false and lazy is set to true, i.e

generated sql looks like:

select column1, column2, lower(column1) as formula0_0_ from table_name order by lower(column1) asc

so it does the job, but I am not sure what performance implications would be especially if you have a lot of those "formula" properties to provide different sorting options.

thanks again for your help, and if you know how to exclude that formula (via mapping) from selected columns while still using it for order by please let me know.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 4:08 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
What about honoring jdl by rating his post as helpful. :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 4:11 pm 
Newbie

Joined: Wed Aug 03, 2005 2:55 pm
Posts: 3
already done :)


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