Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hql, How to write join query between tables that has one to many relationship?

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?

like image 369
Dinoop paloli Avatar asked May 13 '13 13:05

Dinoop paloli


People also ask

Can we use join in HQL query?

HQL Join : HQL supports inner join, left outer join, right outer join and full join.

Which type of join could return the following data if there is no match between the two tables a and b):?

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.

What is cross join in HQL?

More than one entity can also appear in HQL which will perform cartesian product that is also known as cross join.


1 Answers

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.

like image 186
JeroenVP Avatar answered Nov 10 '22 00:11

JeroenVP