2012-05-15 64 views
0

我有作者之间的ManyToMany关系。
表:作者
ID |命名针对非标准ManyToMany关系的HQL查询

表:ASSIGN
*号| author_id | book_id *

表:BOOK
ID |标题

但我不使用非标准多对多映射,我使用的下一个

class Book 
{ 
    @Id 
    long id; 

    String title; 

    @OneToMany 
    List<Assign> authors; 
} 

class Assign 
{ 
    @Id 
    long id; 

    @ManyToOne 
    Book book 

    @ManyToOne 
    Author author; 
} 

class Author 
{ 
    @Id 
    long id; 

    String name; 

    @OneToMany 
    List<Assign> books; 
} 

什么是查询来获取作者的名字所有的书吗?另外什么是查询来获取作者姓名的所有书籍的名称?

回答

1

您只需拥有两个OneToMany关联。您只需在查询中使用连接,如Hibernate documentation over HQL中所述。

select book from Author author 
left join author.books assign 
left join assign.book book 
where author.name = :name 

第二个查询是一样的,除非你只是希望这本书的名字:

select book.name from Author author ... 

旁注:你不应该命名分配booksauthors的您的收藏:这是非常令人困惑。将其命名为assigns

+0

最后'where book.title =:title'。是的,这是真的 – Ilya

0

什么是查询以获取作者姓名的所有书籍?

从书B,以便通过b.authors.author.name

什么是查询来获取所有书籍的名字作者的名字?

从b.authors.author.name

书B为了

选择b.title你换入作者和书类标题和名字吗?