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

醉酒当歌 提交于 2019-12-04 03:30:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16523420/hql-how-to-write-join-query-between-tables-that-has-one-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!