Hi,

I'm trying to persist a java.util.List backed up by the ArrayList
implementation. I want the elements in the list ordered in the same way when
I retreive the list as they were when I stored them. I am using the @OrderBy
annotation that specifies ordering by the primary key. I am using the
@GeneratedValue to generate the primary key.

It seems like the primary keys generated for the OrderImpl entity below does
not come in a sequence that corresponds their order in the list. This causes
the list to be populated in a different order when I retrieve it.

Do I have to use one additional field to get the elements back in the list
in the same order? I.e. add an index field and use @OrderBy("index ASC") or
is there another solution? Have I missed something? A simplified (made up)
version of the domain model can be found below.

Regards,

Petter



@Entity
@Table(name="CUSTOMER_TABLE")
public static class CustomerImpl implements Customer {
   @Id
   @GeneratedValue
   private String customerID;

   @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL,
mappedBy="customer", targetEntity=OrderImpl.class)
   @OrderBy
   private List<Order> orders;
}

@Entity
@Table(name="ORDER_TABLE")
public static class OrderImpl implements Order {
   @Id
   @GeneratedValue
   private String orderID;

   @ManyToOne(targetEntity=CustomerImpl.class)
   @JoinColumn(name="customerID")
   private  Customer customer;

   private String item;
}

Reply via email to