Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting a list of JPQL Entities
    primarykey
    data
    text
    <p>I've searched long and hard for an answer, but I haven't had any luck so far. I'm trying to retrieve and sort a list of objects via a JPQL query, but since the query itself uses a lot of joins between different tables, it's really difficult.</p> <p>Basically, we have an entity "Person" with the fields</p> <pre><code>String name List&lt;Telephone&gt; phones List&lt;Email&gt; emails List&lt;Address&gt; addresses </code></pre> <p>"Telephone", "Email", and "Address" are all separate Entities, each containing its own data, like a String field "number" or something similar (Address.street, Address.state). So all four of these objects are tables in the database.</p> <p>I want the user to be able to sort a list of Persons by any particular data. Right now, I want to sort by any one of the following: the Person's name, the first email address in the emails list, by the street or state of the person's first address, and so on. So if a list of Persons is of the following:</p> <pre><code>Name Phone Number Street State Mack 555-1234 1 Main Street WA Andy 222-9999 2 Other Way RI Wendy 222-3333 3 Wrong Way UT </code></pre> <p>and I want the table to be sorted by State, the list should be:</p> <pre><code>Name Phone Number Street State Andy 222-9999 2 Other Way RI Wendy 222-3333 3 Wrong Way UT Mack 555-1234 1 Main Street WA </code></pre> <p>I want this to be done using a JPQL query so a list is already filtered and sorted when it's given to the web server to optimize performance. I must also add that I am implementing a search feature that searches all these "columns" for a particular term.</p> <p>Long story short, how do I write a JPQL query such that a can obtain a list of Person objects that have been sorted for one of these columns? My implementation of "SELECT DISTINCT":</p> <pre><code>SELECT DISTINCT x FROM Person x, IN(x.phones) phones ORDER BY phones.number </code></pre> <p>doesn't work since the result table requires to have the column being sorted (which is invalid because it causes the list to not qualify as a list of Person objects), and I can't seem to use nested SELECT statements to generate the result set and then pull a list of Person objects from that. Is there any easy way to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload