带有 IN 子句的 JPA CriteriaBuilder

JPA CriteriaBuilder with IN Clause

我有实体报告 "CollectionTable" ReportUser:

@实体 class 举报 {

@专栏 私有短用户 ID;

@Column(名称="reportUserId") @ElementCollection(获取=FetchType.EAGER) @CollectionTable(name = "ReportUser", joinColumns = { @JoinColumn(name = "reportId", referencedColumnName = "reportId") }) private Set reportUsers = new HashSet<>();

}

我需要使用 CriteraiQuery 编写以下 SQL(基本上我需要由用户 1111 创建或用户 ID 1111 在该集合中的所有报告 table:

select * 来自 Report r join ReportUser ru on r.reportId = ru.reportId where r.userId=1111 or ru.reportUserId=1111;

知道怎么做吗?

谢谢你和最诚挚的问候 达利博尔

好的,我找到了解决方案:

CriteriaBuilder cb = persistence.getCriteriaBuilder();
CriteriaQuery<Report> query = cb.createQuery(Report.class);
Root<Report> root = query.from(Report.class);

Predicate userCreated = cb.equal(root.get(Report_.rowInfo).get(RowInfo_.userCreated), getUserId());
Predicate reportUser = cb.isMember(getUserId(), root.get(Report_.reportUsers));
query.where(cb.or(userCreated, reportUser));

return persistence.getCriteriaResults(query);