persistence@glassfish.java.net

Re: Maintaining an order of the list

From: Aleksei Valikov <aleksei.valikov_at_gmail.com>
Date: Wed, 17 Oct 2007 12:34:10 +0200

Hi.

> Why not add an "index" field to the Item?
>
> Then you could simply iterate through the list and set the index for each
> Item, and specify @OrderBy( value = "index" ) to make sure the list is
> retrieved in that same order.
>
> I would guess that you could also make Item implement Comparable, and use a
> sorted collection like TreeSet, but I haven't tested this.

Simply because the index is not a property or attribute of the item.
What you're suggesting is that if I want to map List<MyItem> and
retain the order of the elements, I introduce a new "index" field in
the MyItem and the @OrderBy this field. This will introduce a
tremendous overhead and data integrity issues. Just think of what
you'll need to do to set and maintain correct indexes within MyItems.
Or what happens with many-to-many associations and so on.

Bye.
/lexi