title: Spring Data and how to sort by a column not in an Entity
body: <p>I'm new to Java Spring and trying to understand how it works as I go.  My goal here is to apply a sort to a JPQL query that is not in an Entity.</p>
  
  <p>References I've been looking at:</p>
  
  <p><a href="http://docs.spring.io/spring-data/data-commons/docs/current/api/org/springframework/data/domain/Sort.html" rel="noreferrer">Spring Sort Class Docs</a></p>
  
  <p><a href="http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-six-sorting/" rel="noreferrer">Spring Sort crash course</a></p>
  
  <p>If I declare the following query</p>
  
  <pre><code>@Query("SELECT a, b.someColumn FROM TableA a INNER JOIN a.tableB b where a.search like %?1%"
  public Page&lt;Object[]&gt; findSomething(String search, Pageable pageable);
  </code></pre>
  
  <p>This will result in a list of objects with a form of</p>
  
  <pre><code>Object[0] = TableA entity
  Object[1] = b.someColumn value
  </code></pre>
  
  <p>I've been reading up on the Sort class, but I haven't found a way to make this sort by b.someColumn.</p>
  
  <p>If I do the following</p>
  
  <pre><code>Sort sort = new Sort(Sort.Direction.DESC, "someColumn");
  Page&lt;Object[]&gt; things = mgr.findSomething("junk", sort);
  </code></pre>
  
  <p>Then the JPQL results in</p>
  
  <blockquote>
    <p>SELECT a, b.someColumn FROM TableA a INNER JOIN a.tableB b where
    a.search like ?1 <strong>order by a.someColumn</strong></p>
  </blockquote>
  
  <p>when I want it to be </p>
  
  <blockquote>
    <p>SELECT a, b.someColumn FROM TableA a INNER JOIN a.tableB b where
    a.search like ?1 <strong>order by b.someColumn</strong></p>
  </blockquote>
  
  <p>How do I prevent it from appending the sort column to the Entity in the query?  Is there a way to disable this function and make it interpret my sort column literally? Is there a better sorting technique that would be more appropriate to my case?</p>
  
  <pre><code>Sort sort = new Sort(Sort.Direction.DESC, "b.someColumn");
  </code></pre>