public enum ReportStatus {
SUCCCEED, FAILED;
}
public class Work {
@ElementCollection
@Enumerated(EnumType.STRING)
List<ReportStatus> reportStatuses;
}
Given the following structure, I'd like to perform a query to find all the work filtered by reportStatuses. It works fine with the following hql syntax :
public List<Long> queryHQL() {
final String query = "SELECT w.id FROM Work w JOIN w.reportStatuses s WHERE s in (:rs)";
final List<ReportStatus> reportStatuses = new ArrayList<ReportStatus>();
reportStatuses.add(ReportStatus.FAILED);
return this.entityManager.createQuery(query).setParameter("rs", reportStatuses).getResultList();
}
But I'd like to use the criteria API (jpa2), and can't figure out how to do it. Here is my closest try I think :
public List<Long> query() {
final List<ReportStatus> reportStatuses = new ArrayList<ReportStatus>();
reportStatuses.add(ReportStatus.FAILED);
final CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
final Root<Work> workModel = criteriaQuery.from(Work.class);
final ListJoin<Work, ReportStatus> status = workModel.joinList("reportStatuses");
final Predicate predicate = status.in(reportStatuses);
criteriaQuery.where(predicate);
criteriaQuery.select(workModel.<Long> get("id"));
return this.entityManager.createQuery(criteriaQuery).getResultList();
}
I've also tried with the hibernate criteria API, but as the jpa2 one I've failed to find the correct the syntax.