title: @NamedNativeQuery - How can I bind it to a repository method?
body: <p>I am using <code>Spring</code> + <code>Hibernate</code> and I have a particular case where I need to obtain (a list of) non-<code>Entity</code> objects as a result of the query.</p>
  
  <p>I decided to use <code>@ConstructorResult</code> in <code>@SqlResultSetMapping</code> and refer to this mapping in <code>@NamedNativeQuery</code>, as mentioned <a href="https://stackoverflow.com/questions/29082749/spring-data-jpa-map-the-result-to-non-entity-pojo">here</a> and <a href="https://www.thoughts-on-java.org/result-set-mapping-constructor-result-mappings/" rel="noreferrer">here</a>.</p>
  
  <p>However, in all examples using named native queries, they obtain <code>EntityManager</code> instance via <code>@PersistenceContext</code> and call <code>createNativeQuery</code> on it, providing the <code>name</code> of <code>@NamedNativeQuery</code> as parameter to that call, as seen in <a href="https://stackoverflow.com/a/36109273/7491770">this</a> answer.</p>
  
  <p>How can I map a method declared in a repository <code>interface</code> to a particular <code>@NamedNativeQuery</code>? My attempt was to use <code>EntityName.MethodNameInRepository</code> or <code>MethodNameInRepository</code> as the <code>name</code> of <code>@NamedNativeQuery</code>, but no luck.</p>
  
  <p>Here is my simplified code:</p>
  
  <pre><code>@Entity(name = "AdDailyData")
  @SqlResultSetMapping(
          name="RevenueByAppAndDayMapping",
          classes=@ConstructorResult(
                  targetClass=RevenueByAppAndDay.class,
                  columns={@ColumnResult(name="country_code"),
                          @ColumnResult(name="revenue", type=Double.class),
                          @ColumnResult(name="currency")}))
  @NamedNativeQuery(
          name="AdDailyData.aggregateRevenue",
          query="SELECT country_code, sum(earnings) as revenue, currency "
                  + "FROM ad_daily_data, pseudo_app, app "
                  + "WHERE ad_daily_data.pseudo_app_id=pseudo_app.id AND pseudo_app.app_id=app.id AND app.id=:appId and ad_daily_data.day = :day "
                  + "GROUP BY country_code, currency "
                  + "ORDER BY country_code ASC",
          resultSetMapping="RevenueByAppAndDayMapping")
  public class AdDailyDataEntity {
  
      // fields, getters, setters etc.
  
      public static interface Repository extends JpaRepository&lt;AdDailyDataEntity, Long&gt; {
  
          public List&lt;RevenueByAppAndDay&gt; aggregateRevenue(@Param("appId") long appId, @Param("day") LocalDate day);
  
      }
  
  }
  </code></pre>
  
  <p>Here is my non-<code>Entity</code> class.</p>
  
  <pre><code>public class RevenueByAppAndDay {
  
      private String countryCode;
      private Double earnings;
      private String currency;
  
      public RevenueByAppAndDay(String countryCode, Double earnings, String currency) {
          this.countryCode = countryCode;
          this.earnings = earnings;
          this.currency = currency;
      }
  
      public String getCountryCode() {
          return countryCode;
      }
  
      public Double getEarnings() {
          return earnings;
      }
  
      public String getCurrency() {
          return currency;
      }
  
  }
  </code></pre>
  
  <p>Any kind of help is highly appreciated.</p>
  
  <p><strong>EDIT:</strong>
  The end of the stack trace is as follows:</p>
  
  <pre><code>Caused by: org.springframework.data.mapping.PropertyReferenceException: No property aggregateRevenue found for type AdDailyDataEntity!
  </code></pre>