Hibernate: how to maintain insertion order

Posted by jwaddell on Stack Overflow See other posts from Stack Overflow or by jwaddell
Published on 2010-06-11T04:43:01Z Indexed on 2010/06/11 4:52 UTC
Read the original article Hit count: 222

Filed under:
|

I have a list of entities where creation order is important, but they do not contain a timestamp to use for sorting. Entities are added to the end of the list as they are created so they will be ordered correctly in the list itself.

After persisting the list using Hibernate the entities appear in the database table in the order that they were created. However when retrieving the list using a new Hibernate session the list is now in reverse order of insertion/creation.

Is this expected behaviour? Is there any way to retrieve the list in the same order as it appears in the table?

The primary key is a UUID, and the list of entities should always have been created on the same IP address and JVM. This mean sorting by UUID is a possibility but I'd rather not make assumptions.

Another possibility is if the list is guaranteed to always come out in reverse order I could always just work through it backwards.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate