Ensuring order of child properties with JPA/EJB3?

When I have a one to many relationship, a simple parent child relationship, I specify the relationship like this:

@OneToMany(cascade = CascadeType.ALL, mappedBy ="reportType")

private List<ReportTypeValue> values;

But when the List is populated, the actual order of the elements is arbitrary, and varies depending on the whim of the database.

Is there a way to specify what order the children should be populated within the resulting collection?

[541 byte] By [bronze-starDukes] at [2007-11-26 12:13:38]
# 1
use the @OrderBy Annotationgreets basti
bronzestar at 2007-7-7 14:15:10 > top of Java-index,Archived Forums,Socket Programming...
# 2

how about if i want to order without using any of child's field?

I'd like to set who is going to be 1st, who's 2nd, etc.

something like:

child objects

c1[id=1, name=A]c2[id=2, name=B]c3[id=3, name=C]

and child order could be:

{c1, c2, c3} or {c2,c3,c1} or {c3,c1,c2} or etc.

bronzestar at 2007-7-7 14:15:10 > top of Java-index,Archived Forums,Socket Programming...
# 3
maybe you could sort the collection in the getter of the propertybastiMessage was edited by: basti
bronzestar at 2007-7-7 14:15:10 > top of Java-index,Archived Forums,Socket Programming...
# 4

I have to persist the order of those child objects, just sorting in the getter method won't work.

I came up with:

Create another persistent object that wraps the child object and has a position field. Then the relationship from parent is to the wrapper object. This way I have the original parent-child relationship plus "persistent-custom-ordering" of child objects.

might no be the smart solution, but it works...

Message was edited by:

Ronpon

bronzestar at 2007-7-7 14:15:10 > top of Java-index,Archived Forums,Socket Programming...