Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining the order of a list

Tags:

java

jpa

I have the following problem. I have three classes, A, B and C. A contains a OneToMany relationed list of B:s. B contains a ManyToOne relation to C. C contains a field called "name" and B also contains a field called "name". What I'd like to accomplish is to have the items in A's list sorted primarily by C's name and secondarily by B's name - the problem is that I do not know how to do this. Is it even possible?

I'm using EclipseLink as my JPA provider.


class A {
   @OneToMany
   @OrderBy("b.c.name, b.name") <---- this is the problem
   List<B> b;
}

class B {
   @ManyToOne
   C c;
   String name;
}

class C {
   String name;
}

EDIT Yes, I've tried different variations, for example @OrderBy("c.name") doesn't work, I just get an error message telling me that the entity class b does not contain a field called "c.name".

like image 466
Kim L Avatar asked Jul 09 '09 16:07

Kim L


People also ask

How do you rearrange the order of a list in Python?

You can sort a list in Python using the sort() method. The method accepts two optional arguments: reverse : which sorts the list in the reverse order (descending) if True or in the regular order (ascending) if False (which it is by default)

How do I check the order of a list in Python?

The simplest way to check this is run a loop for first element and check if we could find any smaller element than it after that element, if yes, the list is not sorted. if ( not flag) : print ( "Yes, List is sorted." )

How do you sort a list in chronological order?

Select the dates you want to sort chronologically. On the Home tab, in the Formats group, click Sort & Filter and select Sort Oldest to Newest. Alternatively, you can use the A-Z option on the Data tab, in the Sort & Filter group.

Can a list be ordered?

A list, by definition, is an ordered sequence of items, accessible by their indices.


1 Answers

It's NOT possible. @OrderBy only accepts direct property / field names, not nested properties. Which makes sense, really, because "c" table - depending on your fetching strategy may not even be part of a select issued to retrieve your "b"s.

like image 122
ChssPly76 Avatar answered Oct 19 '22 08:10

ChssPly76