0

I'm using hibernate template and it's
findByCriteria(criteria, offset, maxResults) method
to get results paginated. To get results ordered before calling findByCriteria I set in criteria OrderBy property. The problem is a want to order this column not just as simple string, but take into account that it may contain numbers and sort it in alphanumeric way:

entity 2
entity 19
entity 22

not like this:

entity 19
entity 2
entity 22

To do this I'm using comparator it works ok with Collections.sort. But I need a way to bind it to criteria and get already ordered after firing findByCriteria method. Is there is a way to accomplish this?
Thanks!

stacy
  • 11
  • 1
  • 10
  • You will have to sort it yourself after fetching results – Antoniossss Apr 06 '15 at 13:46
  • The problem is I will be sorting only one page of whole table. So each time I switch the page the sorted results will be relative only to that page. I was thinking about implementing Comparable for my entity and somehow apply natural order, but can't find how to do that. – stacy Apr 06 '15 at 13:55
  • And I told you, that you have to sort after fetching whole result set, not just a part of it. Did you try to perform such sorting of yours in plain SQL? If it is possible, than you can do it with Hibernate... – Antoniossss Apr 06 '15 at 14:07
  • Yeah this is the problem. Not sure if it is possible in SQL and I need to find some way to order it without fetching whole result set. There is way to order joined columns with @Order(Comparator.class). I was thinking that there is something similar applicable to whole Entity class. – stacy Apr 06 '15 at 14:15
  • Still, it will be after fetching whole result set. You cannot use java code (Comparator) in sql... – Antoniossss Apr 06 '15 at 14:20
  • One wy would be to split numbers into separate column. No other way around – Antoniossss Apr 06 '15 at 14:20

1 Answers1

0

I think what you need is to sort in your criteria query the elements itself by some "criteria". Basically you will be using a SQL query (build internally by Hibernate when you specify your order criteria) to sort the elements themselves, instead of sorting them after being retrieved> You will end up having a better performant, cleaner and straightforward solution.

Remember you are fetching a chunk of data every time and you can't tell how to sort the whole set, but that small one you already have plus the previous ones...what you might get (in the next ones) is foreseen.

x80486
  • 6,627
  • 5
  • 52
  • 111