@NamedNativeQuery - 如何将其绑定到存储库方法? [英] @NamedNativeQuery - How can I bind it to a repository method?

查看:14
本文介绍了@NamedNativeQuery - 如何将其绑定到存储库方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring + Hibernate 并且我有一个特殊情况,我需要获取(列表)非 Entity 对象作为查询的结果.

I am using Spring + Hibernate and I have a particular case where I need to obtain (a list of) non-Entity objects as a result of the query.

我决定在 @SqlResultSetMapping 中使用 @ConstructorResult 并在 @NamedNativeQuery 中引用这个映射,正如前面提到的 这里此处.

I decided to use @ConstructorResult in @SqlResultSetMapping and refer to this mapping in @NamedNativeQuery, as mentioned here and here.

然而,在所有使用命名原生查询的示例中,它们通过 @PersistenceContext 获取 EntityManager 实例并在其上调用 createNativeQuery,提供 <@NamedNativeQuery 的代码>名称 作为该调用的参数,如this 回答.

However, in all examples using named native queries, they obtain EntityManager instance via @PersistenceContext and call createNativeQuery on it, providing the name of @NamedNativeQuery as parameter to that call, as seen in this answer.

如何将存储库interface 中声明的方法映射到特定的@NamedNativeQuery?我尝试使用 EntityName.MethodNameInRepositoryMethodNameInRepository 作为 @NamedNativeQueryname,但没有成功.

How can I map a method declared in a repository interface to a particular @NamedNativeQuery? My attempt was to use EntityName.MethodNameInRepository or MethodNameInRepository as the name of @NamedNativeQuery, but no luck.

这是我的简化代码:

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

}

非常感谢任何形式的帮助.

Any kind of help is highly appreciated.

堆栈跟踪的结尾如下:

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

推荐答案

@NamedNativeQuery 上的 name 值需要设置为 "AdDailyDataEntity.aggregateRevenue".第一部分(点之前)需要匹配实体类名.

The name value on the @NamedNativeQuery needs to be set to "AdDailyDataEntity.aggregateRevenue". The first part (before the dot) needs to match the entity class name.

这篇关于@NamedNativeQuery - 如何将其绑定到存储库方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆