title: Spring JPA with native query and data projection mapping the wrong columns into the projected interface
body: <p>I've got a bit of a bizarre problem that I can't figure out why it's happening. I'm sure I did something wrong, because this is my first time using a data projection and I've never had such problems using DTOs.</p>
  
  <p>Pretty much I have a SELECT statemen that is returning certain columns of various data types. And I have an interface that I'm passing to the JPA Repository so it can do the interface mapping. But instead of mapping the results based on the column name (eg. 'accountnum' -> <code>getAccountnumber()</code>), it's mapping the columns in alphabetical order. So if 'date_of_order' is the first in the SELECT statement, its value will be returned by <code>getAccountnumber()</code>.</p>
  
  <p>I have a projected interface that looks something like this:</p>
  
  <pre><code>public interface FlatSearchResult {
      String getAccountnumber();
      UUID getTrackingId;
      Date getDateOfOrder;
  }
  </code></pre>
  
  <p>My model has three tables something like this:</p>
  
  <pre><code>ACCOUNT
    - account_id : uuid (pkey)
    - accountnumber : string
  ORDERS
    - order_id : uuid (pkey)
    - date_of_order : timestamp
    - account_id : uuid (fkey)
  TRACKING
    - tracking_id : uuid (pkey)
    - order_id : uuid (fkey)
  </code></pre>
  
  <p>There's other columns in each of those tables, but they're not relevant.</p>
  
  <p>I have a repository defined with a simple query:</p>
  
  <pre><code>public interface OrderTrackingRepository extends JpaRepository&lt;Account, UUID&gt; {
    @Query( nativeQuery = true,
            value = "SELECT o.date_of_order, a.accountnumber, t.tracking_id " +
                    "FROM account as a " +
                    "INNER JOIN orders as o USING (account_id) " +
                    "INNER JOIN tracking as t USING (tracking_id) " +
                    "WHERE a.accountnumber = :acctnum")
    &lt;T&gt; Collection&lt;T&gt; findOrderInfoForAccount(@Param("acctnum") acctNumber, Class&lt;T&gt; type);
  }
  </code></pre>
  
  <p>When I call this method, the correct rows are returned by the query. But instead of mapping using the column name (eg. date_of_order to <code>getDateOfOrder()</code>), it is mapping based on the order of the columns in the SELECT statement to the alphabetically-ordered methods in the interface.</p>
  
  <p>So:</p>
  
  <pre><code>SELECT date_of_order, accountnumber, tracking_id
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>getAccountNumber() -&gt; date_of_order
  getDateOfOrder() -&gt; accountnumber
  getTrackingId() -&gt; tracking_id
  </code></pre>
  
  <p>It will consistently return in this fashion, so it's not a transient issue. </p>
  
  <p>As a temporary workaround, I've reordered the columns in my SELECT statement. But I would rather not have to do this since it's like iterating through a result set and relying on column position, which just makes me twitchy....</p>
  
  <p>How can I get Spring JPA to map from the result set to my interface? Do I need to annotate my projection interface's methods with something to tell Spring what column name it's referring to?</p>
  
  <p><sup>My database is Postgres. I'm using Spring 5.0.2.RELEASE and Spring-Boot 2.0.0.M7. I can adjust either of those to newer versions if needed, but nothing older. I'm using C3P0 0.9.5.2 for my connection pooling, and postgres-9.2-1002.jdbc4. All my other dependencies (hibernate, etc) are what is pulled in by this version of Spring-Boot.</sUP></p>