Generation query when the ManyToMany relationship is used by Spring Data Jpa project Generation query when the ManyToMany relationship is used by Spring Data Jpa project spring spring

Generation query when the ManyToMany relationship is used by Spring Data Jpa project


Since parameter names are lost in bytecode, you need to use @Param annotation to indicate the parameter that is mapped as the :category variable in your JPQL. So, you code would look like:

@Query("SELECT b FROM Book b INNER JOIN b.categories c WHERE c IN (:categories)")List<Book> findByCategories(@Param("categories") Collection<Category> categories);

?1 certainly works, but is probably not as readable.


You can directly use the JPA naming convention to solve the issue.

List<Book> findAllByCategoriesIn(Collection<Category> categories);

Hope it will help to solve the problem.