Wednesday, October 05, 2005

EJB3: @OneToMany and @OrderBy - Set versus List

I noticed that my (Hibernate) EJB3 entity with a @OneToMany collection did not sort correctly even though I had an @OrderBy("sortcolumn") specified on it. Every time I fetched the entity from database, the collection order was randomly different. The reason was that the collection was declared as java.util.Set, and a Set does not guarantee that the order will remain constant over time. The database performed the order by statement, but the Set did not return elements in the same order from time to time. Changing the collection type to java.util.List solved the problem, the sort order remained constant.

1 comment:

Unknown said...

Thanks.

This post helps me from pouring through books & documentation.