Forums Register Login
Hibernate sorting - is there a better way?
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.

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
I suggest to use Criteria API as below:
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

Gene Hilpert wrote:

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

Crusading Chameleon likes the size of this ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

This thread has been viewed 2022 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 23, 2019 18:52:35.