Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneration query when the ManyToMany relationship is used by Spring Data Jpa project
    text
    copied!<p>I've the following entities mapping:</p> <pre><code>@Entity @Table(name = "books") public class Book implements Serializable { @ManyToMany @JoinTable(name="books2categories", joinColumns=@JoinColumn(name="book_id"), inverseJoinColumns=@JoinColumn(name="category_id")) Collection&lt;Category&gt; categories; </code></pre> <p>...</p> <pre><code>@Entity @Table(name = "categories") public class Category implements Serializable { @ManyToMany(mappedBy="categories") private Collection&lt;Book&gt; books; </code></pre> <p>BookRepository interface is looked:</p> <pre><code>public interface BookRepository extends JpaRepository&lt;Book, Long&gt; { @Query("SELECT b FROM Book b INNER JOIN b.categories c WHERE c IN (:categories)") List&lt;Book&gt; findByCategories(Collection&lt;Category&gt; categories); </code></pre> <p>Please fix me if I'm wrong in the query itself. When I run test for the <code>findByCategories</code> method, I'm getting the error:</p> <blockquote> <p>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</p> </blockquote> <p>Which option do I have to resolve it?</p> <p>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.</p> <p>UPDATE: I've fixed it by using <code>(?1)</code>:</p> <pre><code>@Query("SELECT b FROM Book b INNER JOIN b.categories c WHERE c IN (?1)") </code></pre> <p>instead of </p> <pre><code>@Query("SELECT b FROM Book b INNER JOIN b.categories c WHERE c IN (:categories)") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload