如何使用单引号设置查询参数

时间:2015-05-20 06:51:53

标签: java spring oracle11g spring-data-jpa

我正在使用oracle数据库。我需要通过jpa repository运行更新查询。这是我试图执行的查询。

            @Transactional(propagation = Propagation.REQUIRES_NEW)
            @Modifying
            @Query(
                value = "UPDATE transactionlog SET transactionstatus= :ps,startedat = CURRENT_TIMESTAMP, readytoprocessat= (CURRENT_TIMESTAMP+ interval ':to' second)  WHERE logid IN (:li) ",
                nativeQuery = true)
            public Integer reserve(@Param("ps") short processingStatus, @Param("li") List<Integer> logIdList, @Param("to") int timeOut);

但是这个例外

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that name [to] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [to] did not exist

但如果我按如下方式更改此方法,则可以正常工作。

@Transactional(propagation = Propagation.REQUIRES_NEW)
            @Modifying
            @Query(
                value = "UPDATE transactionlog SET transactionstatus= :ps,startedat = CURRENT_TIMESTAMP, readytoprocessat= (CURRENT_TIMESTAMP+ interval '5' second)  WHERE logid IN (:li) ",
                nativeQuery = true)
            public Integer reserve(@Param("ps") short processingStatus, @Param("li") List<Integer> logIdList);

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

名称为[to]的参数不存在,因为您将:to放在单引号之间。使用:to代替':to'

话虽如此,无论如何这都行不通。我遇到了类似的问题,几个小时后终于找到了我提出的解决方案in answer here。出于某种原因,当interval发挥作用时,注入参数并不像您期望的那样有效。

考虑到上述链接的结论 - 我认为这应该有效:

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Modifying
@Query(value = "UPDATE transactionlog SET transactionstatus= :ps,
       startedat = CURRENT_TIMESTAMP, 
       readytoprocessat= (CURRENT_TIMESTAMP + (( :to ) || 'second')\\:\\:interval)
       WHERE logid IN (:li) ",nativeQuery = true)
public Integer reserve(@Param("ps") short processingStatus, @Param("li") List<Integer> logIdList, @Param("to") int timeOut);

答案 1 :(得分:0)

将?ps和所有其他参数替换为?1,?2,...并使methos参数与SQL参数匹配(顺序很重要)。

答案 2 :(得分:0)

readytoprocessat= (CURRENT_TIMESTAMP+ interval ':to' second)

该条款有问题,请先尝试分别执行一行。然后你就可以自己看到问题了。

答案 3 :(得分:0)

我找到了解决这个问题的答案。

@Transactional(propagation = Propagation.REQUIRES_NEW)
        @Modifying
        @Query(
            value = "UPDATE transactionlog SET transactionstatus= :ps,startedat = CURRENT_TIMESTAMP, readytoprocessat= CURRENT_TIMESTAMP+ NUMTODSINTERVAL( :to, 'SECOND' )  WHERE logid IN (:li) ",
            nativeQuery = true)
        public Integer reserve(@Param("ps") short processingStatus, @Param("li") List<Integer> logIdList, @Param("to") int timeOut);