2

更新

我已经将第一个方法更改为返回 a List,但我得到了同样的异常。

Repository班上有 2 个本机查询。键入一个以返回所有记录,另一个仅返回一行。见下文:

@Query(nativeQuery = true, value = NATIVE_SUMMARY_QUERY_PARTIAL + "group by (rjd.refresh_job_identifier)) as rc")
List<RefreshSummary> getRefreshJobDetailSummary();

@Query(nativeQuery = true, value = NATIVE_SUMMARY_QUERY_PARTIAL + " WHERE rjd.refresh_job_identifier = :refreshJobId" +
        " group by (rjd.refresh_job_identifier)) as rc")
List<RefreshSummary> getRefreshJobDetailSummaryById(@Param("refreshJobId") String refreshJobId);

interface RefreshSummary {
    String getRefreshJobId();
    Date getRefreshJobStart();
    Date getRefreshJobComplete();
    String getUserId();
    long getTotalRecords();
    long getSuccessfulRecords();
    long getPendingRecords();
    long getErrorRecords();
    long getCancelledRecords();
    String getRefreshJobStatus();
}

String NATIVE_SUMMARY_QUERY_PARTIAL = "SELECT  rc.refresh_job_identifier as refresh_job_id, ..."

第一种方法,getRefreshJobDetailSummary,工作正常。但是第二种方法,我只想要一行,给了我这个例外:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.company.repository.RefreshJobDetailRepository$RefreshSummary]

完整的查询如下所示:

String NATIVE_SUMMARY_QUERY_PARTIAL = "SELECT  rc.refresh_job_identifier as refresh_job_id, " +
        "rc.refresh_job_start_time as refresh_job_start, " +
        "rc.record_completion_time as refresh_job_complete, " +
        "rc.user_id as user_id, " +
        "rc.pending + rc.successful + rc.cancelled + rc.error as total_records, " +
        "rc.successful as successful_records, " +
        "rc.pending as pending_records, " +
        "rc.error as error_records, " +
        "rc.cancelled as cancelled_records, " +
        "CASE WHEN pending > 0 THEN 'In progress' " +
        "ELSE 'Complete' " +
        "END as refresh_job_status " +
        "FROM " +
        "(SELECT rjd.refresh_job_identifier as refresh_job_identifier, " +
        "MAX(rjd.refresh_job_start_time) as refresh_job_start_time, " +
        "MAX(rjd.record_completion_time) as record_completion_time, " +
        "MAX(rjd.org_usr_nu) as user_id, " +
        "SUM(CASE WHEN LOWER(record_status) = 'pending' THEN 1 ELSE 0 END) as pending, " +
        "SUM(CASE WHEN LOWER(record_status) = 'successful' THEN 1 ELSE 0 END) as successful, " +
        "SUM(CASE WHEN LOWER(record_status) = 'cancelled' THEN 1 ELSE 0 END) as cancelled, " +
        "SUM(CASE WHEN LOWER(record_status) = 'error' THEN 1 ELSE 0 END) as error " +
        "from erd_cfg_owner.refresh_job_detail rjd " ;

查询返回的值如下所示:

'{20191218204913458hc35, 2019-12-18 20:49:13.314, 2019-12-18 20:49:24.335, hc35, 1, 1, 0, 0, 0, Complete}'

有人可以对此有所了解吗?为什么一种方法有效,而另一种无效?

4

2 回答 2

0

您能否尝试在查询声明中使用构造函数表达式或创建自定义转换器。

Eg-SELECT NEW com.example.MyClass(e.attribute1, e.attribute2...) FROM MyEntity e");
于 2019-12-29T06:21:20.133 回答
0

我会将您的第二种方法的返回类型从RefreshSummaryto替换为List<RefreshSummary>. 从理论上讲,您的方法应该返回一个列表而不是单个值

于 2019-12-18T21:50:13.797 回答