java @NamedNativeQuery - 如何将其绑定到存储库方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44871757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:23:14  来源:igfitidea点击:

@NamedNativeQuery - How can I bind it to a repository method?

javaspringhibernatejpaspring-data-jpa

提问by ram

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

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

I decided to use @ConstructorResultin @SqlResultSetMappingand refer to this mapping in @NamedNativeQuery, as mentioned hereand here.

我决定使用@ConstructorResultin@SqlResultSetMapping并参考in 中的映射@NamedNativeQuery,如此此处所述

However, in all examples using named native queries, they obtain EntityManagerinstance via @PersistenceContextand call createNativeQueryon it, providing the nameof @NamedNativeQueryas parameter to that call, as seen in thisanswer.

但是,在使用命名本机查询的所有示例中,它们通过EntityManager实例获取@PersistenceContext并调用createNativeQuery它,将nameof@NamedNativeQuery作为参数提供给该调用,如答案所示。

How can I map a method declared in a repository interfaceto a particular @NamedNativeQuery? My attempt was to use EntityName.MethodNameInRepositoryor MethodNameInRepositoryas the nameof @NamedNativeQuery, but no luck.

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

Here is my simplified code:

这是我的简化代码:

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

    }

}

Here is my non-Entityclass.

这是我的非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.

任何形式的帮助都受到高度赞赏。

EDIT:The end of the stack trace is as follows:

编辑:堆栈跟踪的结尾如下:

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

采纳答案by Grant Lay

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

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

回答by hizmarck

Although the answer is correct, for someone to arrived here for similar cuestions this works for me without used @NamedNativeQuery:

虽然答案是正确的,但对于为了类似提示而来到这里的人来说,这对我有用,而无需使用 @NamedNativeQuery:

public class Example {

private Long someProperty;
// getters and setters
}

To call a query or stored procedure and populate my object:

要调用查询或存储过程并填充我的对象:

@Repository
public interface ExampleRepository extends 
JpaRepository<Example,Long> {

/**
 * Search Example by someProperty
 *
 * @param property
 * @return
 */
@Query( nativeQuery = true, value = "SELECT * FROM public.my_stored_procedure(:property)")
List<Example> findByProperty(@Param("property") Long property);

}

I know this is another approach and the code is decoupled, but we get the same result.

我知道这是另一种方法,代码是解耦的,但我们得到了相同的结果。

I hope that can be useful for someone. Regards.

我希望这对某人有用。问候。