ram*_*ram 12 java spring hibernate jpa spring-data-jpa
我正在使用Spring
+ Hibernate
并且我有一个特殊情况,我需要获取(一个列表)非Entity
对象作为查询的结果.
我决定使用@ConstructorResult
in @SqlResultSetMapping
并参考此映射@NamedNativeQuery
,如此处和此处所述.
然而,在使用命名原生查询所有案例中,他们获得EntityManager
通过实例@PersistenceContext
并调用createNativeQuery
就可以了,提供name
的@NamedNativeQuery
作为参数传递给该呼叫,如在此回答.
如何将存储库中声明的方法映射interface
到特定的@NamedNativeQuery
?我的尝试是使用EntityName.MethodNameInRepository
或MethodNameInRepository
作为name
的@NamedNativeQuery
,但没有运气.
这是我的简化代码:
@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<AdDailyDataEntity, Long> {
public List<RevenueByAppAndDay> aggregateRevenue(@Param("appId") long appId, @Param("day") LocalDate day);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的非Entity
全班.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
任何形式的帮助都非常感谢.
编辑: 堆栈跟踪的结束如下:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property aggregateRevenue found for type AdDailyDataEntity!
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8820 次 |
最近记录: |