[英]Hql, How to write join query between tables that has one to many relationship?
我有2
张桌子。 1st
一个与2nd
个有oneToMany
关系。
班级作者
@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;
我可以拿到所有作者和书籍,但如何在书上给出条件?
我认为它是这样的:
select a from Author as a join a.Book as ab where ab.AuthorId like '%"hello"%';
虽然不确定a.Book,它也可能是a.Books,因为你的列名被命名为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.