Generation query when the ManyToMany relationship

2019-04-09 06:05发布

问题:

I've the following entities mapping:

@Entity
@Table(name = "books")
public class Book implements Serializable {
    @ManyToMany
    @JoinTable(name="books2categories",
    joinColumns=@JoinColumn(name="book_id"),
    inverseJoinColumns=@JoinColumn(name="category_id"))
    Collection<Category> categories;

...

@Entity
@Table(name = "categories")
public class Category implements Serializable {
    @ManyToMany(mappedBy="categories")
    private Collection<Book> books;

BookRepository interface is looked:

public interface BookRepository extends JpaRepository<Book, Long> {

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

Please fix me if I'm wrong in the query itself. When I run test for the findByCategories method, I'm getting the error:

testFindByCategories(com.savdev.springmvcexample.repository.JpaBookRepositoryTest): org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1

Which option do I have to resolve it?

And the second, can I debug Spring Data Jpa logic that passes the argument into the query? I'm getting a proxy returned by Spring Data Jpa, cannot understand where to use break point to debug this behaviour.

UPDATE: I've fixed it by using (?1):

@Query("SELECT b FROM Book b INNER JOIN b.categories c WHERE c IN (?1)")

instead of

@Query("SELECT b FROM Book b INNER JOIN b.categories c WHERE c IN (:categories)")

回答1:

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.