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.