Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » persist a collection in order(persist an entity and its respective collection (OnetoMany) in order)
persist a collection in order [message #758563] Wed, 23 November 2011 17:00 Go to next message
FiruzzZ Mising name is currently offline FiruzzZ Mising nameFriend
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 #758619 is a reply to message #758563] Wed, 23 November 2011 22:46 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Are you using @OrderBy or @OrderColumn?
Re: persist a collection in order [message #758728 is a reply to message #758619] Thu, 24 November 2011 12:22 Go to previous messageGo to next message
FiruzzZ Mising name is currently offline FiruzzZ Mising nameFriend
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 Shocked
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 12:25]

Report message to a moderator

Re: persist a collection in order [message #758764 is a reply to message #758563] Thu, 24 November 2011 14:00 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
May I ask where did you take this statement from?

Quote:
The ORM should respect the order of the items in the collection.


In a contrary, take a look at JPA 2.0 FR specification:

"JPA 2.0 FR, 2.2 Persistent Fields and Properties"

Collection-valued persistent fields and properties must be defined in terms of one of the following collection-valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans method conventions noted above and whether field or property access is used: java.util.Collection, java.util.Set, java.util.List, java.util.Map. (...) Portable applications should not expect the order of a list to be maintained across persistence contexts unless the OrderColumn construct is used or unless the OrderBy construct is used and the modifications to the list observe the specified ordering.


Regards.
Re: persist a collection in order [message #758768 is a reply to message #758728] Thu, 24 November 2011 14:04 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
May I ask where did you take this statement from?

Quote:
Quote:
The ORM should respect the order of the items in the collection.



In a contrary, take a look at JPA 2.0 FR specification:

"JPA 2.0 FR, 2.2 Persistent Fields and Properties"

> Collection-valued persistent fields and properties must be defined in terms of one of the following collection-valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans method conventions noted above and whether field or property access is used: java.util.Collection, java.util.Set, java.util.List, java.util.Map. (...) Portable applications should not expect the order of a list to be maintained across persistence contexts unless the OrderColumn construct is used or unless the OrderBy construct is used and the modifications to the list observe the specified ordering.



Regards.
Re: persist a collection in order [message #758771 is a reply to message #758728] Thu, 24 November 2011 14:05 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
May I ask where did you take this statement from?

Quote:
Quote:
> The ORM should respect the order of the items in the collection.



In a contrary, take a look at JPA 2.0 FR specification:

"JPA 2.0 FR, 2.2 Persistent Fields and Properties"
> > Collection-valued persistent fields and properties must be defined in terms of one of the following collection-valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans method conventions noted above and whether field or property access is used: java.util.Collection, java.util.Set, java.util.List, java.util.Map. (...) Portable applications should not expect the order of a list to be maintained across persistence contexts unless the OrderColumn construct is used or unless the OrderBy construct is used and the modifications to the list observe the specified ordering.



Regards.
Re: persist a collection in order [message #758776 is a reply to message #758771] Thu, 24 November 2011 14:14 Go to previous messageGo to next message
FiruzzZ Mising name is currently offline FiruzzZ Mising nameFriend
Messages: 19
Registered: October 2011
Junior Member
I think it should work that way, make sense.. I don't see the necessity of leave this "issue" to a pseudo random internal mechanisms of THIS (Hibernate doesn't do this) ORM.

PD: why the triple post?
Re: persist a collection in order [message #758780 is a reply to message #758771] Thu, 24 November 2011 14:14 Go to previous messageGo to next message
FiruzzZ Mising name is currently offline FiruzzZ Mising nameFriend
Messages: 19
Registered: October 2011
Junior Member
I think it should work that way, make sense.. I don't see the necessity of leave this "issue" to a pseudo random internal mechanisms of THIS (Hibernate doesn't do this) ORM.

PD: why the triple post?
Re: persist a collection in order [message #758785 is a reply to message #758780] Thu, 24 November 2011 14:45 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Of course, the JPA provider can do this. It's not forbidden. It's clearly stated that "Portable applications should not expect the order of a list". If you're aware of this and intend to use vendor-specific feature, than it's fine, but you cannot expect portable bahaviour.

PS. I think I'm still not quite sure how this forum works, and what's the difference between 'reply', 'quick reply' and 'post reply'; sometimes my post is shown as separate thread, sometimes like a post within thread. Don't have an idea what's going on Wink)
Re: persist a collection in order [message #758787 is a reply to message #758780] Thu, 24 November 2011 14:45 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Of course, the JPA provider can do this. It's not forbidden. It's clearly stated that "Portable applications should not expect the order of a list". If you're aware of this and intend to use vendor-specific feature, than it's fine, but you cannot expect portable bahaviour.

PS. I think I'm still not quite sure how this forum works, and what's the difference between 'reply', 'quick reply' and 'post reply'; sometimes my post is shown as separate thread, sometimes like a post within thread. Don't have an idea what's going on ;))
Re: persist a collection in order [message #758821 is a reply to message #758785] Thu, 24 November 2011 16:10 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
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 : Twitter
Re: persist a collection in order [message #758822 is a reply to message #758821] Thu, 24 November 2011 16:11 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Yes, these forums are screwed up. Please complain to the Eclipse Foundation. It seems any post with a link or code will be duplicated.


James : Wiki : Book : Blog : Twitter
Previous Topic:persist a collection in order
Next Topic:How can I check PersistenceContexts for equality
Goto Forum:
  


Current Time: Wed Apr 24 16:21:50 GMT 2024

Powered by FUDForum. Page generated in 0.04526 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top