Dinoop paloli
Dinoop paloli

Reputation: 633

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?

Upvotes: 5

Views: 23583

Answers (1)

JeroenVP
JeroenVP

Reputation: 175

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.

Upvotes: 9

Related Questions