Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate sorting - is there a better way?

 
Gene Hilpert
Ranch Hand
Posts: 51
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Hibernate with an embedded derby database. I have a parent child relationship between two classes, Think of it as a checkbook application with Accounts and Checks. My form is a master-detail set of jtables with the top being the Accounts and the bottom Checks, When you click on the accounts the detail changes to the checks in the account. That is working fine. But I want the checks to be sorted in date order with the most current check at the top.
I have been trying to use the Collections.sort but it does not like my List of Checks and I still have to write the comparator. But I'm wondering if there is a better way? If not I will keep working it as long as I know it is the right direction.

Thanks
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like using the comparitor and sorting the list of data that is in memory on the web tier. It should be easy, and I'd be interested to know what your problem is with your current comparator? The other option is to use the orderby option when retrieving the data from the database, but of course, this means another trip to the data tier. It's a performance issue, but perhaps not that big of an issue if your database is properly indexed,and you're only pulling out a select number of records. Getting data from the database is really what a database does, so it shouldn't be a huge issue.

-Cameron McKenzie
 
Justin Chi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest to use Criteria API as below:
 
Gene Hilpert
Ranch Hand
Posts: 51
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the main problem was but when I passed the list that was retrived when I got the Account object into the sort method I got an error saying the compiler did not know what the type was ( I dont have access to the exact wording here) then when I cast the list to List or Arraylist I got different errors, I did not have the comparitor inplace yet.

I will go back and look it it again. And if I cant get it I will just rewrite it with the Criteria API like Justin said.

PS I dont know if this matters but it is not a web application just a desktop application.

PSS . Your Hibernate made easy - Good book, it has helped me a lot.

thanks everyone
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gene Hilpert wrote:

PSS . Your Hibernate made easy - Good book, it has helped me a lot.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic