Can you please help me find out what is wrong with the following JPQL query. It should filter books by their genres.
Query has additional OR conditions which I have removed due to better readability (with or without the other conditions the error is the same).
@Query("SELECT b FROM Book b WHERE " +
"b in (select distinct b1 FROM Book b1 inner join b1.genres genre where genre in :genres)")
List<Book> searchBooks(@Param("genres") List<Genre> genres);
Following is the stacktrace:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 1, column 197 [select count(b) FROM *.domain.Book AS b WHERE b.id = (select distinct b1.id FROM *.domain.Book b1 inner join b1.genres genre where genre in :genres_0_, :genres_1_)]
Relation between Book and Genre classes is ManyToMany undirectional.
@ManyToMany
@JoinTable(name = "books_genres",
joinColumns = @JoinColumn(name = "BOOK_ID"),
inverseJoinColumns = @JoinColumn(name = "GENRE_ID"))
private List<Genre> genres;
I have checked the subquery separately and it works:
@Query("select distinct b1 FROM Book b1 inner join b1.genres genre where genre in :genres")
List<Book> searchBooksByGenre(@Param("genres") List<Genre> genres);
Is it possible at all to use the subquery in this way?