Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OrderBy problem

 
Dave Lane
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a situation where, in JPA, I have a parent with a collection of children. Firstly, the parent is created in memory and stored in the httpsession. Next, 1 to n children are created and added to the parent. Then the parent object is sent to the entity manager for persistence and the children are persisted in cascade.

I have now been presented with a requirement that the children be presented in the order they were created. As I understand it, there is no guarantee that JPA will preserve the ordering of the collection. I have a created_date field in the database but, as the whole matrix is persisted in one go, the creation date of the records as saved is the same for the parent and all children - so no help there.

A possible solution is to add a new field/column to the children that I can populate as they are created, then use an @OrderBy on this for retrieval.

This seems a bit heavy-handed.

Is there a more elegant solution to this problem using the vanilla JPA implementation?

Thanks
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Think that thats the best solution for your problem, add a new column that will contain a progressive value that holds the index of the children.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're guaranteed that the id column on the many side is going to increment, you should be able to point the @OrderBy annotation on the list to the id of the many side. Take a look at this and this.

 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jave, but this will not guarantee the order in which they are inserted to the list, this will guarantee the order in which they are inserted in the DB.
 
Dave Lane
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Thanks for your responses. I think the pertinent bit is from the first link above : "This means that the positions of the elements in the List at persistence are not preserved (are not persisted). Instead an ordering is defined for their retrieval." My problem is how to control the definition of the retrieval order. This wouldn't be a problem if they weren't being saved in cascade because I could just use the timestamp. (On that point, my db is at hh:mm:ss resolution, so perhaps if I dialled it down to milliseconds or below I could actually use the timestamp, but again I want to use vanilla JPA instead of relying on a particular implementation, be it db or container.)

I've done a bit of empirical fiddling and so far the (sequence-generated) primary keys of the children are in the order I add stuff to the collection - so it actually is presisting the order of the collection. I have seen, however, dire warnings not to depend on this behaviour.

It looks to me as though the framers of the spec are implying that, if you want to guarantee ordering, you have to supply your own ordering criteria, over and above anything provided by the collections framework. I suppose it makes sense to concentrate on the data rather than the collections implementation. I think hibernate does preserve the order though, doesn't it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic