私はSpring
+ Hibernate
を使用していますが、クエリの結果として非Entity
オブジェクト(リスト)を取得する必要がある特定のケースがあります。@NamedNativeQuery - どのようにしてリポジトリメソッドにバインドできますか?
私はhereとhereが述べたように、@SqlResultSetMapping
で@ConstructorResult
を使用して@NamedNativeQuery
にこのマッピングを参照することを決めました。
ただし、名前付きネイティブクエリを使用して、すべての例では、彼らは@PersistenceContext
経由EntityManager
インスタンスを取得し、this答えに見られるように、その呼び出しにパラメータとして@NamedNativeQuery
のname
を提供し、それにcreateNativeQuery
を呼び出します。
リポジトリinterface
で宣言されたメソッドを特定の@NamedNativeQuery
にマップするにはどうすればよいですか?私の試みは、またはMethodNameInRepository
をname
として@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!
受信しているスタックトレースを追加できますか? –
@GrantLayが例外を追加しました。 – ram
サービスからaggregateRevenueを呼び出す方法は?私は、AdDailyDataEntityをオートワイヤリングして、adDailyDataEntity.aggregateRevenueを呼び出そうとしましたが、メソッドが定義されていないというエラーが表示されます。 –