-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria order
PostPosted: Mon Nov 16, 2009 1:47 pm 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Hi,

I would like to sort my address by a number of the building. This number may consist of digits and letters (letters only at the end).
I would like to sort this list using Criteria object. When I am using:
Code:
criteria.addOrder(Order.asc("address.firstNumber"));


I got:
22A, 23, 24B, 3, 31 and so on...
Of course I would like to have the list like that:
3, 22A, 23, 24B, 31...

Of course the problem is because my address.firstNumber field is a String instead of int. I can not use just an integer because this number may be for example 22A.

Is there an easy possibility to sort this list using Criteria object? The only solution of this problem which comes to my mind is to make an usual sql query and not a Criteria.

Regards,
Gandalf


Top
 Profile  
 
 Post subject: Re: Criteria order
PostPosted: Mon Nov 16, 2009 5:00 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I hate to tell you but even if you write it as simple sql (which criteria translates down into) you're going to get the same behavior.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: Re: Criteria order
PostPosted: Tue Nov 17, 2009 11:07 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
It depends of the exact sql. I did something like that:

Code:
query += " order by address.street, cast(nullif(regexp_replace(address.firstNumber, '^([0-9]*).*$', '\\\\1'),'') as int), address.firstNumber ..."
...
return getHibernateTemplate().find(query, attrs);


This query orders the list by numeric value of the address.firstNumber and then - by the whole value of address.firstNumber (in case there would be the same number and different letters like 24A 24B 24D).

This query works. But of course it works only with the dedicated database platform (postgres in my case) so I was wondering if it could be done with the more universal Criteria object. I guess it is impossible.


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