Java Hql,如何在具有一对多关系的表之间编写连接查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16523420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hql, How to write join query between tables that has one to many relationship?
提问by Dinoop paloli
I have 2
tables. 1st
one have oneToMany
relationship with 2nd
.
我有2
桌子。1st
一有oneToMany
关系2nd
。
Class Author
班级作者
@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
}
Class Book
课本
@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
}
How can I write a Hql
query so that I will get all author's and there books , with a condition that book name should starts with hello
我怎样才能写一个Hql
查询,以便我将获得所有作者和那里的书籍,条件是书名应该以hello
I know using a query like this,
我知道使用这样的查询,
from Author;
I can fetch all author's and there books,but how to give condition on book?
我可以获取所有作者和那里的书籍,但是如何提供书籍的条件?
采纳答案by JeroenVP
I think its something like this:
我认为它是这样的:
select a from Author as a join a.Book as ab where ab.AuthorId like '%"hello"%';
not sure about a.Book though, it could also be a.Books as your columnname is named like that.
虽然不确定 a.Book,但它也可能是 a.Books,因为您的列名就是这样命名的。