很难加入Spring中的ManyToMany表以使hql工作

时间:2016-05-18 04:09:01

标签: java spring hibernate hql

我有两个表,书和作者是多对多的关系

book.java

@Id
@GeneratedValue
private Integer id;

@ManyToMany
@JoinTable(name="BookAuthor", 
joinColumns=@JoinColumn(name="id"),
inverseJoinColumns=@JoinColumn(name="author_id"))  
private List<Author> authors;

Author.java

@Id
@GeneratedValue
private Integer id;

@ManyToMany(mappedBy = "authors")
private List<Book> books;

我现在正在尝试在查询中加入两个表

String hql = "select book from Book book" +
    " join book.authors author where author.id = :author_id";

但是我收到了这些错误

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:  
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:

我在book.java中命名连接ID是错误的,还是在查询中命名错误?

0 个答案:

没有答案