【问题标题】:JPA Repository : How to multiply column value by parameter valueJPA 存储库:如何将列值乘以参数值
【发布时间】:2017-05-30 07:19:37
【问题描述】:

我想将从数据库中移动的值乘以参数中的“km”值并返回结果,当我使用以下查询时,它给了我一个错误,即该点的参数 [1] 不存在

请帮助我如何计算 calue ?

public interface FareRateRepository  extends JpaRepository<FareRate, Long>{

    @Query("select f.moving* :km from FareRate f where f.id=1" )
    float  calculateFare(@Param("km") Long km);

}

【问题讨论】:

  • 您可以在 from 子句中使用参数,而不是在查询中的任意位置。这也是 JPQL 还是原生 sql 查询?
  • 任意位置是什么意思? @M.Deinum
  • 基本上是 from 子句之前的任何内容。
  • 因为一些 JDBC 驱动程序不支持 result 子句中的参数(尽管一些 JPA 提供者通过其他方式支持它)

标签: java mysql sql spring jpa


【解决方案1】:

你错过了“:”

select f.moving* :km from FareRate f where f.id=1

【讨论】:

  • 只是为了确认这是正确的? f.id=1 还是 f.id=?1 在您的代码中?
【解决方案2】:

试试这个

@Query(value = "select ( f.moving * ?1 ) as fare from FareRate f where f.id=1", nativeQuery = true )
float  calculateFare(Long km);

注意:您必须在查询中指定实际的表名而不是FareRate。这将运行原生查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2018-01-26
    • 2013-08-04
    相关资源
    最近更新 更多