title: How to map sql native query result into DTO in spring jpa repository?
body: <p>Hi what I am trying to achieve is to get SQL native query result map into my DTO in java spring jpa repository, how do I do this properly? I try several code, but it does not work, here is what I tried:</p>
  <p>First try :</p>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface StockRepository extends RevisionRepository&lt;Stock, Long, Integer&gt;, JpaRepository&lt;Stock, Long&gt; {
      
     @Query(value = &quot;SELECT stock_akhir.product_id AS productId, stock_akhir.product_code AS productCode, SUM(stock_akhir.qty) as stockAkhir &quot;
          + &quot;FROM book_stock stock_akhir &quot;
          + &quot;where warehouse_code = (:warehouseCode) &quot;
          + &quot;AND product_code IN (:productCodes) &quot;
          + &quot;GROUP BY product_id, product_code, warehouse_id, warehouse_code&quot;, nativeQuery = true)
     List&lt;StockAkhirDto&gt; findStockAkhirPerProductIn(@Param(&quot;warehouseCode&quot;) String warehouseCode, @Param(&quot;productCodes&quot;) Set&lt;String&gt; productCode);
  }
  </code></pre>
  <p>once I execute the function, I got this error:</p>
  <blockquote>
  <p>No converter found capable of converting from type
  [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap]
  to type [com.b2bwarehouse.Dto.RequestDto.StockDto.StockAkhirDto]</p>
  </blockquote>
  <p>Second try :</p>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface StockRepository extends RevisionRepository&lt;Stock, Long, Integer&gt;, JpaRepository&lt;Stock, Long&gt; {
      
     @Query(value = &quot;SELECT new com.b2bwarehouse.Dto.RequestDto.StockDto.StockAkhirDto(stock_akhir.product_id AS productId, stock_akhir.product_code AS productCode, SUM(stock_akhir.qty) as stockAkhir) &quot;
        + &quot;FROM book_stock stock_akhir &quot;
        + &quot;where warehouse_code = (:warehouseCode) &quot;
        + &quot;AND product_code IN (:productCodes) &quot;
        + &quot;GROUP BY product_id, product_code, warehouse_id, warehouse_code&quot;, nativeQuery = true)
     List&lt;StockAkhirDto&gt; findStockAkhirPerProductIn(@Param(&quot;warehouseCode&quot;) String warehouseCode, @Param(&quot;productCodes&quot;) Set&lt;String&gt; productCode);
  }
  </code></pre>
  <p>in second here is the error:</p>
  <blockquote>
  <p>could not extract ResultSet; SQL [n/a]; nested exception is
  org.hibernate.exception.SQLGrammarException: could not extract
  ResultSet</p>
  </blockquote>
  <p>below is my DTO:</p>
  <pre class="lang-java prettyprint-override"><code>@Data
  @AllArgsConstructor
  @NoArgsConstructor
  public class StockAkhirDto {
     private Long productId;
     private String productCode;
     private Integer stockAkhir;
  }
  </code></pre>
  <p>How should I correct my code? So, can I get the result into my DTO?</p>