title: How to map native query result into custom class object?
body: <p>I have three classes: Lending, LendingReturn and ProductLending.</p>
  
  <p><strong>Lending</strong></p>
  
  <pre><code>@Entity
  public class Lending implements Serializable {
  
  @Id
  private Long id;
  
  private BigDecimal amount;
  
  private Instant createdDate;
  
  private User lender;
  
  private ProductLending productLending;
  
  }
  </code></pre>
  
  <p><strong>Lending Return</strong></p>
  
  <pre><code>@Entity
  public class LendingReturn implements Serializable {
  
  @Id
  private Long id;
  
  private BigDecimal amount;
  
  private Instant createdDate;
  }
  </code></pre>
  
  <p><strong>Product Lending</strong></p>
  
  <pre><code>@Entity
  public class ProductLending implements Serializable {
  
      @Id
      private Long id;
  
      private String title;
  }
  </code></pre>
  
  <p>Below is my native query. I want to query then the result is mapped into good mapped list</p>
  
  <pre><code>Query query = em.createNativeQuery("select l.id, pl.title, sum(lr.amount)+l.amount as total_return, " +
                                              "(select amount as yesterday_return from lending_return where lending_id=l.id and date(created_date)=current_date-2), " +
                                              "l.period_in_day-(current_date-date(l.created_date)) as mature_in_day " +
                                              "from lending_return lr right join lending l on lr.lending_id=l.id " +
                                          "join product_lending pl on l.product_lending_id=pl.id " +
                                          "where l.lender_id=:lenderId " +
                                          "group by l.id, pl.title, l.amount");
  
      query.setParameter("lenderId", userService.getLoggedInUser().getId());
      List&lt;Object[]&gt; resultList = query.getResultList();
  </code></pre>
  
  <p>So the query returns columns: id, title, total_return, yesterday_return</p>
  
  <p>Then the <strong>resultList</strong> contains:</p>
  
  <pre><code>[
    [
      2804,
      "Title 1",
      1001800,
      600,
      24
    ],
    [
      2809,
      "Title 2",
      null,
      null,
      28
    ]
  ]
  </code></pre>
  
  <p>How to map the <strong>resultList</strong> so the result will be like below?</p>
  
  <pre><code>[
    {
      "id": 2804,
      "title": "Title 1",
      "total_return": 1001800,
      "yesterday_return": 600,
      "mature_in_day": 24
    },
    {
      "id": 2809,
      "title": "Title 2",
      "total_return": null,
      "yesterday_return": null,
      "mature_in_day": 28
    }
  ]
  </code></pre>