Java Spring Data Jpa项目使用ManyToMany关系时的生成查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19886903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generation query when the ManyToMany relationship is used by Spring Data Jpa project
提问by Alexandr
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:
BookRepository 接口看起来:
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:
如果我在查询本身中错了,请修复我。当我对该findByCategories
方法运行测试时,出现错误:
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
testFindByCategories(com.savdev.springmvcexample.repository.JpaBookRepositoryTest): org.hibernate.QueryParameterException: 位置超出了已声明序数参数的数量。请记住,序数参数是从 1 开始的!职位:1;嵌套异常是 java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position 超出声明的序数参数的数量。请记住,序数参数是从 1 开始的!职位: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.
第二,我可以调试将参数传递给查询的 Spring Data Jpa 逻辑吗?我得到了一个由 Spring Data Jpa 返回的代理,无法理解在哪里使用断点来调试这种行为。
UPDATE:
I've fixed it by using (?1)
:
更新:我已经使用(?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)")
采纳答案by wasabi
Since parameter names are lost in bytecode, you need to use @Paramannotation to indicate the parameter that is mapped as the :category
variable in your JPQL. So, you code would look like:
由于参数名称在字节码中丢失,因此您需要使用@Param注解来指示映射为:category
JPQL 中的变量的参数。所以,你的代码看起来像:
@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.
?1
当然有效,但可能不那么可读。