带有参数和可分页错误的 hql 查询 "Parameter with that position [1] did not exist"
hql query with param and pageable error "Parameter with that position [1] did not exist"
这是我的本机查询(不幸的是我不能使用它,因为本机查询不适用于可分页)
select * from stock_indicators si inner join stock st on si.stock_id = st.id where si.slope_simple_regression_10 > 1 and si.date = (select date from stock_details order by date desc limit 1) order by si.slope_simple_regression_10 desc
这是对应的hql查询:
@Query(value = "from StockIndicators si join fetch si.stock where si.slopeSimpleRegression10Days > 1 and si.date = :date order by si.slopeSimpleRegression10Days desc", countQuery = "select count(si.stock) from StockIndicators si where si.slopeSimpleRegression10Days > 1")
Page<StockIndicators> findWithStocksIndicators10DaysTrendUp(Pageable pageable, @Param("date") LocalDate date);
并且此查询不起作用,出现以下错误:
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
到目前为止我做了什么:
- 更改了参数的顺序(仍然是同样的错误)
- 更改查询以包含子查询,但不幸的是 HQL
不支持限制
- 将 date = :date 部分更改为 date = ?2(仍然是同样的错误)
你能帮我吗?
我终于找到了解决办法。这是我的愚蠢错误。我忘了给 countQuery 添加参数。正确的方法应该是:
@Query(value = "from StockIndicators si join fetch si.stock where si.slopeSimpleRegression10Days > 1 and si.date = :date order by si.slopeSimpleRegression10Days desc", countQuery = "select count(si.stock) from StockIndicators si where si.slopeSimpleRegression10Days > 1 and si.date = :date ")
Page<StockIndicators> findWithStocksIndicators10DaysTrendUp(Pageable pageable, @Param("date") LocalDate date);
这是我的本机查询(不幸的是我不能使用它,因为本机查询不适用于可分页)
select * from stock_indicators si inner join stock st on si.stock_id = st.id where si.slope_simple_regression_10 > 1 and si.date = (select date from stock_details order by date desc limit 1) order by si.slope_simple_regression_10 desc
这是对应的hql查询:
@Query(value = "from StockIndicators si join fetch si.stock where si.slopeSimpleRegression10Days > 1 and si.date = :date order by si.slopeSimpleRegression10Days desc", countQuery = "select count(si.stock) from StockIndicators si where si.slopeSimpleRegression10Days > 1")
Page<StockIndicators> findWithStocksIndicators10DaysTrendUp(Pageable pageable, @Param("date") LocalDate date);
并且此查询不起作用,出现以下错误:
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
到目前为止我做了什么:
- 更改了参数的顺序(仍然是同样的错误)
- 更改查询以包含子查询,但不幸的是 HQL 不支持限制
- 将 date = :date 部分更改为 date = ?2(仍然是同样的错误)
你能帮我吗?
我终于找到了解决办法。这是我的愚蠢错误。我忘了给 countQuery 添加参数。正确的方法应该是:
@Query(value = "from StockIndicators si join fetch si.stock where si.slopeSimpleRegression10Days > 1 and si.date = :date order by si.slopeSimpleRegression10Days desc", countQuery = "select count(si.stock) from StockIndicators si where si.slopeSimpleRegression10Days > 1 and si.date = :date ")
Page<StockIndicators> findWithStocksIndicators10DaysTrendUp(Pageable pageable, @Param("date") LocalDate date);