Search code examples
javajpaspring-elin-clause

SPRING|Java - Use IN clause with SpEl in query


I got an enum (ClubRole) which got a method returning a collection of values from this enum. I trying to call this method from inside the query using SpEl.

 @Query("select m from ClubMember m " +
    "where m.student = :student " +
    "and m.role in :#{#role.getParents()}"
  )
  List<ClubMember> findByRoleWithInheritance(@Param("student") Student student, @Param("role") ClubRole role);

This passes the build, and the application runs but when this method's called I got ``No parameter binding found for name role!; nested exception is java.lang.IllegalArgumentException: No parameter binding found for name role!

I tried different ways but none worked. I would like to know if it's possible to use SpEl in this situation, and if so how ?


Solution

  • Looks like this is an issue in spring-data-jpa. I could see people discussing same issue on spring-blog. Not sure whether there is an open issue for this. You can try following as a workaround.

    @Query("select m from ClubMember m " +
        "where m.student = :#{#student}" +
        "and m.role in :#{#role.getParents()}"
      )
      List<ClubMember> findByRoleWithInheritance(@Param("student") Student student, @Param("role") ClubRole role);
    

    Or you can try with index access for second parameter like #{[1].getParents()}

    this might help.