JP QL-过滤结果为一对多关系 [英] JP QL - Filtering result in a One To Many relationship

查看:82
本文介绍了JP QL-过滤结果为一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试构建JPQL查询,并希望JPA经验超出我的能力的人可以提供帮助.考虑以下两个实体:

I am stuck trying to construct a JPQL query and was hoping someone with more JPA experience than mine could help. Consider the following two entities:

class Author{
  String name
  @OneToMany(mappedBy="author")
  Set<Book> books
}

class Book{
  String title  
  Boolean inPrint
  @ManyToOne
  Author author
}

如果我想返回一个特定的作者(按名称)并热切地获取(即LEFT JOIN FETCH)Book.inPrint标志为true的书,我该如何在JPQL中表示呢?

If I want to return a specific Author (by name) and eagerly fetch (ie LEFT JOIN FETCH) the books where the Book.inPrint flag is true, how would I express that in JPQL?

推荐答案

SELECT a FROM Author a LEFT JOIN a.books b WHERE b.inPrint = true OR b is null

这篇关于JP QL-过滤结果为一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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