Named param not being detected

14,843

I would change

 @Query("select fd.message from FeedDetail as fd where fd.feedId =: id")

To (difference lies in space)

 @Query("select fd.message from FeedDetail as fd where fd.feedId = :id")

This is a small difference for you but big for Spring. He recognizes a parameter by attaching name to colon like that

:id

For more details refer to the official Spring Data JPA Reference.

Share:
14,843

Related videos on Youtube

Trevor_zam
Author by

Trevor_zam

Updated on June 04, 2022

Comments

  • Trevor_zam
    Trevor_zam almost 2 years

    I have a simple query as follows. I get the expected result if I hard code the id value as follows. But it throws IllegalArgumentException exception if I try to get the value from the Param instead. Note that I have tried to use the Param as both long and String and still the same results. Please advise what I am doing wrong. Thanks.

    My Query

    public interface FeedDetailRepository extends JpaRepository<FeedDetail, Long> {
        @Query("select fd.message from FeedDetail as fd where fd.feedId =: id")
        String custom(@Param("id") long id);
    }  
    

    At Controller, if I run the following, I get an exception.

    @GetMapping("/something/{id}")
        public String getDetail(@PathVariable long id){
            return feedDetailRepository.custom(id);
        }
    

    But if I hard code the id value as follows, I get the wanted result.

    public interface FeedDetailRepository extends JpaRepository<FeedDetail, Long> {
        @Query("select fd.message from FeedDetail as fd where fd.feedId = 4")
        String getDetailBasedOnFeedId(@Param("id") long id);
    }
    

    The exception

    nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: Named parameter not bound : id

  • Oleksii Valuiskyi
    Oleksii Valuiskyi almost 4 years
    What is the difference from Emil Hotkowski's answer?