Hibernate version: 3.0.5
Hibernate does a wonderfull job of managing index columns of a list. One possible issue, however, is that the database 'unique' constraint is not used to enforce the sort order (while logically, it is certainly unique within a particular list). So far (in my experience),
I have never experienced problems with Hibernate's managing of the index column. However, it feels safer (to me) to let database enforce data integrity (in this case, list uniqueness).
A dumb solution would be to add the list-index column to the child element with 'insert="false"' and 'update="false"' and add a 'unique-key' attribute to it (along with whatever happens to be "natural parent definition" - probably 'many-to-one' part). The problem is that the reordering update fails since at some point in the middle of the update two rows have the same list index.
A possible correct solution would be to have hibernate manage that relationship (instead of a hack above). Then during the update, the duplicates can be prevented by setting one of the values to an "invalid value" (e.g. -1) temporarily - and then setting it to the correct new value. For example:
Code:
step 1:
item 'x', index="0"
item 'y', index="1"
step 2:
item 'x', index="-1"
item 'y', index="0"
step 3:
item 'x', index="1"
item 'y', index="0"
This, of course, might have performance implications - so perhaps this behavior (and unique constraint) should be selectable.
For example, list definition might look like this:
Code:
<list name="children">
<key column="parent_id">
<list-index column=indexcolumn" ENFORCE-UNIQUE="true"/>
<one-to-many class="Child"/>
</list>
The same sort of thing can apply to maps to enforce uniqueness of the 'map-key'.