Home > Back-end >  JPA at Query named parameters issue
JPA at Query named parameters issue

Time:09-30

In the following query if I try to run the query by giving for example "488" as age. This JPA query considers it 4 and return result accordingly which is wrong. I do not know why JPA is trimming 2nd and third characters in this case. Any tips would be great. I would like to return all products where age is greater than 488 (example). If I replace greater than (>) sign with equal than JPA does not trim any characters and return all products where age is "488".

@Query("SELECT new com.model.Result(am.product, am.age)"
              "FROM Table am WHERE am.age > :age")
    List<Result> queryResult(@Param("age") String age );

CodePudding user response:

I assume that age is number type. Then you must also use the same type. For example Integer

@Query("SELECT new com.model.Result(am.product, am.age)"
          "FROM Table am WHERE am.age > :age")
List<Result> queryResult(@Param("age") int age );

CodePudding user response:

What you are seeing here is lexical comparison. When you want the strings to be interpreted as numbers to be able to compare numerically, you will have to cast:

@Query("SELECT new com.model.Result(am.product, am.age)"
          "FROM Table am WHERE cast(am.age as integer) > :age")
List<Result> queryResult(@Param("age") int age);
  • Related