0

How to use/add optional parameter in JPQL?

@Query(value = "SELECT stud FROM Student stud where stud.name = :studentName AND stud.age IN :studentAgeList")
List<Student> getStudents(
            @Param("studentName ") String studentName, 
            @Param("studentAgeList") List<Integer> studentAgeList
         )

How to make studentAgeList parameter in above query ?

I tried below :

@Query(value = "SELECT stud FROM Student stud where stud.name = :studentName AND (:studentAgeList IS NULL OR stud.age IN :studentAgeList))
List<Student> getStudents(
            @Param("studentName ") String studentName, 
            @Param("studentAgeList") List<Integer> studentAgeList
         )

But getting error : unexpected AST node:

Tried above but getting error

Ajit
  • 33
  • 4

1 Answers1

0

JPQL does not support optional parameters, you can use overload methods with different queries or criteria API or JPA specifications.

may be get answer here: Set optional parameters in JPQL Query

I.Yuldoshev
  • 108
  • 1
  • 5
  • Indeed, there could be 2 query methods defined (with or without the student list) but the question was more about the possibility to build a where clause testing the parameter it self. – Pierre Demeestere Nov 16 '22 at 13:58