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.
JPA (which hibernate implements) has @OrderBy
:
@OneToMany
@OrderBy("uuidColumn")
private List<Something> list;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With