File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Retrieving a Collection for Later Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Retrieving a Collection for Later Sorting" Watch "Retrieving a Collection for Later Sorting" New topic

Retrieving a Collection for Later Sorting

Mike Petrov

Joined: Oct 10, 2011
Posts: 22

This is a question in-between Hibernate and Collections API. Sorry for posting it in a wrong section if the topic tends to be more Collection-specific.

The situation is this. Let's say we have a User entity that should own a Collection of Orders so that we could easily provide user with all his orders stored in DB if necessary. Under the hood it's Orders and Users tables with Orders having a key to a particular User in each row.
1. What Collection interface should we choose for our code and mapping purposes? Obviously there can be no duplicate orders so should it be a Set? Or a List since we will encounter some sorting purposes (see next question)?
2. Let's say we show our user's orders in a table somewhere in our View (servlet, JSP, whatever). We should provide the opportunity to sort the table using different columns (date / total sum / etc). How do we implement this? Should we read our Collection once from DB and then sort it in code based on View's needs? Then should we use a sorted Set (TreeSet) or a List maintaining order (ArrayList)? Do we make each new sorting using a new Comparator? I think we definitely shouldn't read it from the DB again using Hibernate's Order since we create an unnecessary select.

The question may sound stupid but it's a matter of practical experience I don't have for now. Thank you in advance.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

The database will take care of not having duplicates so it makes sense to use a List (so you have sorting) over a set (which just duplicates what the database is enforcing.)

Whether to sort in Java or in the database generally depends on
1) How many rows you have - if you have a million rows, you want to sort in the database and only return the first X rows using paging to deal with the volume
2) How up to date the data needs to be on the screen - if you are printing stock prices, you want to get new data from the database every time.

Assuming you can sort in memory, consider using CompareToBuilder. This lets you build a comparator for fields easily. Or a reflection comparator so you just pass the name of the field.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Retrieving a Collection for Later Sorting
It's not a secret anymore!