我正在使用Spring+,Hibernate并且在特殊情况下,Entity由于查询,我需要获取(列表)非对象。
Spring
Hibernate
Entity
我决定使用@ConstructorResultin,@SqlResultSetMapping并在其中引用此映射@NamedNativeQuery,如此处和此处所述。
@ConstructorResult
@SqlResultSetMapping
@NamedNativeQuery
但是,在所有使用命名本机查询的示例中,它们都EntityManager通过获取实例@PersistenceContext并对其进行调用createNativeQuery,从而为该调用提供nameof @NamedNativeQuery参数,如该答案所示。
EntityManager
@PersistenceContext
createNativeQuery
name
如何将存储库中声明的方法映射interface到特定对象@NamedNativeQuery?我的尝试是使用EntityName.MethodNameInRepository或MethodNameInRepository作为name的@NamedNativeQuery,但没有运气。
interface
EntityName.MethodNameInRepository
MethodNameInRepository
这是我的简化代码:
@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); } }
这是我的非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; } }
任何帮助都将受到高度赞赏。
编辑: 堆栈跟踪的结尾如下:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property aggregateRevenue found for type AdDailyDataEntity!
需要将name值@NamedNativeQuery设置为"AdDailyDataEntity.aggregateRevenue"。第一部分(点前)需要与实体类名称匹配。
"AdDailyDataEntity.aggregateRevenue"