具有IN子句的JPA CriteriaBuilder

时间:2020-05-19 08:58:28

标签: jpa criteriaquery

我具有“ CollectionTable” ReportUser的实体报告:

@实体 班级报告{

@专栏 私人Short userId;

@Column(name =“ reportUserId”) @ElementCollection(获取= FetchType.EAGER) @CollectionTable(name =“ ReportUser”,joinColumns = {@JoinColumn(name =“ reportId”,referencedColumnName =“ reportId”)}) 私人Set reportUsers = new HashSet <>();

}

我需要使用CriteraiQuery编写以下SQL(基本上,我需要由用户1111或userId 1111创建的所有报告都在该收集表中:

从报告r中选择*在r.reportId = ru.reportId上加入ReportUser ru,其中r.userId = 1111或ru.reportUserId = 1111;

有什么想法吗?

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

1 个答案:

答案 0 :(得分:0)

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

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);