This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes How to retain order of list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to retain order of list?" Watch "How to retain order of list?" New topic
Author

How to retain order of list?

Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
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

Joined: Mar 07, 2007
Posts: 1278

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

Joined: May 14, 2007
Posts: 107
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

Joined: Mar 07, 2007
Posts: 1278

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

Joined: May 14, 2007
Posts: 107
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

Joined: Mar 07, 2007
Posts: 1278

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

Joined: May 14, 2007
Posts: 107
Then maybe I should develop some EJB-QL.

Thanks for all of these replies.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

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

Joined: Mar 15, 2009
Posts: 1
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retain order of list?
 
Similar Threads
Mapping Maps
OneToMany Unidirectional strange behavior ?
collection mapping using set and retain order how to ?
Hibernate 3 Annotations
ClassCastException thrown using @Inheritance and List