Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retain order of list?

 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following code which have OneToMany relation.


However, I want to retain order of units in unitList without changing Unit entity.

Also, my client should be able to change this order.

I'm afraid I can't use Hibernate extension. Only standard Java Persistance could be used.

What can I do for this?
[ September 07, 2008: Message edited by: Bupjae Lee ]
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bupjae,

you can additionally annotate the relationship to specify an order for collections in 1-to-many or many-to-many relationships. Have a look at the documentation for the "@OrderBy" annotation I hope this is what you're looking for...

Marco
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

However, is @OrderBy annotation needs extra column on Unit entity?

Because there is some Unit entity that doesn't belong to any UnitCollection, maybe this wouldn't be good......
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to change the Unit entity class. The @OrderBy annotation just takes an attribute/column which should be used to sort the collection. Additionally you can choose to sort the collection elements in ascending or descending order. But that's up to you to define what kind of order makes sense for these Unit objects...
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply again.

Because my client should be able to change order freely regardless of other columns in Unit entity, maybe I could use something @OrderBy("unitListOrder ASC").

However, is this means there should be column "unitListOrder" in Unit entity?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know the @OrderBy annotation requires that there is an existing column/attribute with the name you give it.

From the short description of your problem it seems to me that you want to allow the client to execute some kind of query. Perhaps you could simply wrap this query logic in another method which returns the Unit collection in the order(s) the client specified. Or you could do this queries in the database layer by providing some methods for the clients of your class which use the EJB query language. This would be a little bit more flexible than just the plain @OrderBy annotation.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then maybe I should develop some EJB-QL.

Thanks for all of these replies.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! If you have more questions, feel free to ask Anyway I think EJB QL will be a good choice to satisfy your requirements.
 
KetanK Shah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean by specifying order by maintains its order - whenever data is retrieved in List. Or it can still change. For example I provide order by for bag and it will map to List. so whenever i access that list will it be always in same order?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic