Hql,如何在具有一对多关系的表之间编写连接查询?

时间:2013-05-13 13:34:02

标签: java mysql hibernate hql

我有2个表格。 1st人与oneToMany2nd个关系。

班级作者

@Entity
@Table(name = "Author")
Public class Author{

    @Id
    @Column(name = "AuthorId")
    private int autherId;

    @Column(name = "AuthorName")
    private String authorName;

    @OneToMany
    @JoinColumn(name="AuthorId",referencedColumnName="AuthorId")
    List<Book> Books;

    //getter and setter
}

课本

@Entity
@Table(name = "Book")
Public class Book{

    @Id
    @Column(name = "BookId")
    private int bookId;

    @Column(name = "BookName")
    private String bookName;

    @Column(name = "AuthorId")
    private int authorId;

    //getter and setter
}

如何编写Hql查询,以便我获取所有作者和书籍,条件是图书名称应以hello开头

我知道使用这样的查询,

  from Author;

我可以获取所有作者和书籍,但如何在书上给出条件?

1 个答案:

答案 0 :(得分:9)

我认为它是这样的:

select a from Author as a join a.Book as ab where ab.AuthorId like '%"hello"%';

不确定a.Book虽然,它也可能是a.Books,因为你的列名被命名为。