title: EJBQL - How to sort query results by a field in LEFT JOIN
body: <p>I have the following classes (simplified for clarity):</p>
  
  <pre><code>Class Top {
    InternationalStringType name;
  }
  
  Class InternationalStringType {
    List&lt;LocalizedStringType&gt; localizedString; 
  }
  
  Class LocalizedStringType {
    String value;
  }
  </code></pre>
  
  <p>The following EJBQL query successfully retrieves all instance of Top with their sub-objects populated:</p>
  
  <pre><code>SELECT DISTINCT Object(t) FROM Top t LEFT OUTER JOIN t.name nm LEFT OUTER JOIN nm.localizedString nm_ls
  </code></pre>
  
  <p>I would like to modify the above query such that the results are sorted by Top.name.localizedString.value</p>
  
  <p>What is the correct syntax to do this? I tried the following query but I get "ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list"</p>
  
  <pre><code>SELECT DISTINCT Object(t) FROM Top t LEFT OUTER JOIN t.name nm LEFT OUTER JOIN nm.localizedString nm_ls ORDER BY nm_ls.value ASC
  </code></pre>
  
  <p>Its not clear to me what to put in the select list for the ORDER BY clause.</p>
  
  <p>Following give errors for the "," after Object(t) no matter what I put after the comma:</p>
  
  <pre><code>SELECT DISTINCT Object(t), Object(nm_ls) FROM Top t LEFT OUTER JOIN t.name nm LEFT OUTER JOIN nm.localizedString nm_ls ORDER BY nm_ls.value ASC
  </code></pre>
  
  <p>In case its relevant my JPA implementation is hibernate 3.6.4.Final. TIA for your help.</p>