I have field int displayOrder = 1 in Grails domain object and would like to increment it by 1 each time the domain object gets saved.
If I have domain object Widget that has one-to-many relationship with Pix, every time Widget gets a new Pix, Pix needs to have displayOrder incremented.
What would be the most prudent way of setting this up? Preferably count should start from 1 to keep confusion to minimum when someone wants to change the display order. I thought about just duplicating the ID, but then we are possibly looking at displayOrder of 1123, followed by 1169 , etc., so that’s not a practical option.
The collection of Pixes belonging to Widget is a sortedSet.
You could also do a Named Query to get the last max value of the
Pixes, which would work in scenarios where the display orders have skips and jumps.Then:
If you want to reinitialize the display order numbers after a deleted
Pixso they are sequential for aWidget, per your comment to @Jarred Olson, you could do: