5

我有2桌子。1st一个有oneToMany关系2nd

班级作者

@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;

我可以获取所有作者和那里的书,但是如何给书提供条件?

4

1 回答 1

9

我认为它是这样的:

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

虽然不确定 a.Book,但它也可能是 a.Books,因为您的列名就是这样命名的。

于 2013-05-13T14:14:58.407 回答