I have an object Foo with a list of Bar. Is there a way I can set up my class so that getBars() will return a List that has been sorted with Collections.sort? In other words, I would like to run Collections.sort when the list is first populated. Presently, I call sort when I retrieve the collection, which may be redundant and is easily forgotten.
Share
Are you using J2EE-style mappings with Hibernate XML files, or are you using JPA-annotated JavaBeans?
If you’re using JPA, you can use the
@OrderByannotation to let the database sort the collection for you. You could also use@Sortto do it on the Java side.Last, if you’re writing HQL – say, in a
@NamedQuery– you can use theORDER BYclause.Lots of ways to do it!