我有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;
我可以获取所有作者和书籍,但如何在书上给出条件?
答案 0 :(得分:9)
我认为它是这样的:
select a from Author as a join a.Book as ab where ab.AuthorId like '%"hello"%';
不确定a.Book虽然,它也可能是a.Books,因为你的列名被命名为。