I have 2
tables. 1st
one have oneToMany
relationship with 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
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?
HQL Join : HQL supports inner join, left outer join, right outer join and full join.
FULL OUTER JOIN using WHERE clause We can include a WHERE clause with a FULL OUTER JOIN to get return only those rows where no matching data between the joining tables are exist.
More than one entity can also appear in HQL which will perform cartesian product that is also known as cross join.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With