2017-07-02 8 views
2

私はSpring + Hibernateを使用していますが、クエリの結果として非Entityオブジェクト(リスト)を取得する必要がある特定のケースがあります。@NamedNativeQuery - どのようにしてリポジトリメソッドにバインドできますか?

私はherehereが述べたように、@SqlResultSetMapping@ConstructorResultを使用して@NamedNativeQueryにこのマッピングを参照することを決めました。

ただし、名前付きネイティブクエリを使用して、すべての例では、彼らは@PersistenceContext経由EntityManagerインスタンスを取得し、this答えに見られるように、その呼び出しにパラメータとして@NamedNativeQuerynameを提供し、それにcreateNativeQueryを呼び出します。

リポジトリinterfaceで宣言されたメソッドを特定の@NamedNativeQueryにマップするにはどうすればよいですか?私の試みは、またはMethodNameInRepositorynameとして@NamedNativeQueryとしましたが、運はありませんでした。ここに私の非Entityクラスです

@Entity(name = "AdDailyData") 
@SqlResultSetMapping(
     name="RevenueByAppAndDayMapping", 
     [email protected](
       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<AdDailyDataEntity, Long> { 

     public List<RevenueByAppAndDay> aggregateRevenue(@Param("appId") long appId, @Param("day") LocalDate day); 

    } 

} 

は、ここに私の単純化されたコードです。

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; 
    } 

} 

どのようなヘルプも高く評価されます。

EDIT次のよう スタックトレースの端部がある:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property aggregateRevenue found for type AdDailyDataEntity! 
+0

受信しているスタックトレースを追加できますか? –

+0

@GrantLayが例外を追加しました。 – ram

+0

サービスからaggregateRevenueを呼び出す方法は?私は、AdDailyDataEntityをオートワイヤリングして、adDailyDataEntity.aggregateRevenueを呼び出そうとしましたが、メソッドが定義されていないというエラーが表示されます。 –

答えて

3

"AdDailyDataEntity.aggregateRevenue"に設定する@NamedNativeQueryニーズにname値。最初の部分(ドットの前)はエンティティクラス名と一致する必要があります。

関連する問題