Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

java - How to set query parameters with single quotes

I'm using a oracle database.I need to run a update query through jpa repository.This is the query I have tried to execute.

            @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);

But this exception

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

But if i change this method as follows, it works fine.

@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);

Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Parameter with name [to] doesn't exist because you put :to between single quotes. Use :to instead of ':to'.

That being said, this will not work anyway. I faced really similar issue and after some hours finally found a solution which I present in answer here. For some reason, when interval comes into play injection of parameters doesn't work as you would expect.

Considering conclusion from the link above - I believe this should work:

@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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...