Sort/Ordering of OneToMany relationship

View: New views
3 Messages — Rating Filter:   Alert me  

Sort/Ordering of OneToMany relationship

by DeSilentio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I'm new to Eclipselink (and the JPA in general) and am struggling with some query syntax.

I have an entity A that has a OneToMany relationship with entity B (using a list).
B has an integer field 'x' that should be used for ordering the OneToMany relationship.

In my application I use a ReadAllQuery to select a bunch of A entities similar to the following:

ExpressionBuilder builder = new ExpressionBuilder();
ReadAllQuery query = new ReadAllQuery(A.class);

Expression exp = builder.anyOf("someOtherOneToManyField").get("name").equal("someValue");

query.useScrollableCursor(pageSize);
ScrollableCursor c = (ScrollableCursor) session.executeQuery(query);

However the List of B entities in the returned A entities are not sorted. I have played around with builder.addOrdering(Expression exp) but cannot figure out the proper code. No matter what I have tried it will not order the List of Bs, but only changes the ordering of the returned As.

Any help is appreciated thanks!

Edit: Sorry posted in the wrong format

Re: Sort/Ordering of OneToMany relationship

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

OrderBy is normally done in the mapping.

Add the annotation to your OneToMany,
@OrderBy("x")

DeSilentio wrote:
Hi,

I'm new to Eclipselink (and the JPA in general) and am struggling with some query syntax.

I have an entity A that has a OneToMany relationship with entity B (using a list).
B has an integer field 'x' that should be used for ordering the OneToMany relationship.

In my application I use a ReadAllQuery to select a bunch of A entities similar to the following:

ExpressionBuilder builder = new ExpressionBuilder();
ReadAllQuery query = new ReadAllQuery(A.class);

Expression exp = builder.anyOf("someOtherOneToManyField").get("name").equal("someValue");

query.useScrollableCursor(pageSize);
ScrollableCursor c = (ScrollableCursor) session.executeQuery(query);

However the List of B entities in the returned A entities are not sorted. I have played around with builder.addOrdering(Expression exp) but cannot figure out the proper code. No matter what I have tried it will not order the List of Bs, but only changes the ordering of the returned As.

Any help is appreciated thanks!

Edit: Sorry posted in the wrong format

Re: Sort/Ordering of OneToMany relationship

by DeSilentio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


James Sutherland wrote:
OrderBy is normally done in the mapping.

Add the annotation to your OneToMany,
@OrderBy("x")
I could have sworn I tried that, but adding the annotation in the mapping seemed to do the trick.

Thanks!