【发布时间】:2020-06-08 11:02:08
【问题描述】:
这是 2 个实体的代码(它在数据库中生成三个表)。 Book 实体:
@Entity
public class Book {
@Id
private long id;
private String name;
@ManyToMany
private List<Author> authors;
}
Author 实体:
@Entity
public class Author {
@Id
private long id;
@Column(unique=true)
private String name;
}
我正在尝试按作者列表查找书籍。这是一个sql查询:
select book.id, ARRAY_AGG(author.name)
from book
join book_authors ba on book.id=ba.book_id
join author on ba.authors_id=author.id
group by book.id
having ARRAY_AGG(distinct author.name order by author.name)=ARRAY['a1', 'a2']::varchar[]
['a1', 'a2'] 是书籍作者列表,必须作为参数传递。这个想法是聚合作者,然后将它们与传递的参数列表进行比较。
如何将此 SQL 查询重写为 JPQL 或 CriteriaBuilder 查询?
【问题讨论】:
标签: spring-data-jpa jpql hibernate-criteria