aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate sorting - is there a better way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate sorting - is there a better way?" Watch "Hibernate sorting - is there a better way?" New topic
Author

Hibernate sorting - is there a better way?

Gene Hilpert
Ranch Hand

Joined: May 22, 2002
Posts: 51

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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Sep 09, 2009
Posts: 25
I suggest to use Criteria API as below:
Gene Hilpert
Ranch Hand

Joined: May 22, 2002
Posts: 51

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

Joined: Aug 26, 2006
Posts: 4968
    
    1

Gene Hilpert wrote:

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



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate sorting - is there a better way?