| persist a collection in order [message #758563] |
Wed, 23 November 2011 12:00  |
FiruzzZ Mising name Messages: 19 Registered: October 2011 |
Junior Member |
|
|
Alo..
the issue is that when I persist a Order with a of List<Item> (OneToMany), Item's are persisten randomly without respect the order of the List.
Exam:
order.getItemList().add(item1);
order.getItemList().add(item2);
order.getItemList().add(item3);
persist(order);
When I retrieve (or check directly on the DB) the entity and check the detail, they are disordered.
Sometimes persist first item1, sometimes item2 and so on..
The only way i found to avoid this behavior was persist the Order with a empty collection and then one by one the collections of Item's.
here is fragment of the mapping
//on Order
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.EAGER)
private List<Item> itemList;
|
|
|
|
| Re: persist a collection in order [message #758728 is a reply to message #758619] |
Thu, 24 November 2011 07:22   |
FiruzzZ Mising name Messages: 19 Registered: October 2011 |
Junior Member |
|
|
would not change anything, because there is no field (column) to determinate the order in which should be persisted (the item.id is still null and autogenerated).
The ORM should respect the order of the items in the collection.
----seconds later
I could add a transient field to determinate this order!..
BUT "The ORM should respect the order of the items in the collection."
[Updated on: Thu, 24 November 2011 07:25] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
| Re: persist a collection in order [message #758821 is a reply to message #758785] |
Thu, 24 November 2011 11:10   |
James Sutherland Messages: 1834 Registered: July 2009 |
Senior Member |
|
|
EclipseLink will maintain the order of a List in memory. But can only maintain the order on the database if an OrderBy or OrderColumn is used.
The order you insert objects in to the database has no guarantee that they will come back in that same order. Relational databases are not ordered, the only way to obtain order is to use an order by. Depending on what table pages are free the objects can end up in any order. Selects can also return objects in any order depending on how they process the query.
James : Wiki : Book : Blog
|
|
|
|
Powered by
FUDForum. Page generated in 0.02166 seconds