gpt4 book ai didi

java - Spring 数据JPA : Having trouble combining native query with parameter

转载 作者:行者123 更新时间:2023-12-02 01:11:03 30 4
gpt4 key购买 nike

我的存储库定义如下:

@Repository
public interface RefreshJobDetailRepository extends JpaRepository<RefreshJobDetailEntity, Long> {

@Query(nativeQuery = true, value = NATIVE_SUMMARY_QUERY_PARTIAL + " WHERE rjd.refresh_job_identifier = ?1" +
" group by (rjd.refresh_job_identifier)) as rc")
RefreshSummary getRefreshJobDetailSummaryById(String refreshJobId);

interface RefreshSummary {
// getters
}

String NATIVE_SUMMARY_QUERY_PARTIAL = "select ...";
}

当我运行此程序时,出现以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named parameters, on JDK 8 or better, be sure to compile with -parameters.; nested exception is java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! On JDKs < 8, you need to use @Param for named parameters, on JDK 8 or better, be sure to compile with -parameters.

令我困惑的是我没有使用“命名参数”。这是一个位置参数。那么这个错误告诉我什么?我们在代码中的其他位置使用位置参数,并且没有设置 -parameters 标志,但这是唯一的 native 查询。不知道这是否有影响?顺便说一句,数据库是 Postgresql。

最佳答案

您可以简单地重构代码以使用命名参数。无论是原生查询还是 jpql:

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

关于java - Spring 数据JPA : Having trouble combining native query with parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59399706/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com