一尘不染

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

hibernate

我有2桌子。与… 1stoneToMany关系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;

我可以获取所有作者和那里的书,但是如何在书上注明条件?


阅读 201

收藏
2020-06-20

共1个答案

一尘不染

我认为是这样的:

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

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

2020-06-20