Hql,如何在具有一对多关系的表之间编写连接查询? [英] Hql, How to write join query between tables that has one to many relationship?

查看:87
本文介绍了Hql,如何在具有一对多关系的表之间编写连接查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 表格。 第一其中一个 oneToMany 第二关系。

I have 2 tables. 1st one have oneToMany relationship with 2nd.

类作者

@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
}

课本

@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
}

我该如何写一个 Hql 查询,这样我就可以得到所有作者和书籍,条件是书名应该以 hello

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

我知道使用这样的查询,来自Author的

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?

推荐答案

我认为它是这样的:

I think its something like this:

select a from Author as a join a.Book as ab where ab.AuthorId like '%"hello"%';

不知道a.Book,但它也可以是a.Books,因为您的列名被命名为like那是。

not sure about a.Book though, it could also be a.Books as your columnname is named like that.

这篇关于Hql,如何在具有一对多关系的表之间编写连接查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆