I am creating a record of Widgets with Hibernate. It's pretty basic. I've got an hbm.xml file describing the object, a DAO class that creates and saves them, etc.
However, there is an explicit sorting order to the widgets, and each new Widget needs to be inserted with a sortIndex
column value that is greater than all other sortIndex
column values (i.e. new widgets are automatically sorted last). I can't for the life of me figure out how to accomplish this.
Hibernate is perfectly capable of setting ID columns automatically, and it makes sense to me that it would be able to set some other column to a unique value according to a formula like max(sortIndex)+1 or according to some increasing sequence generator value, but I can't find a reference in the documentation to this sort of thing. Could someone point me in the right direction?
One approach that sprang to mind was to just query for the highest sortIndex manually, but I started worrying about two different transactions both finding the same new sortIndex.