Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing @ElementCollection in CriteriaQuery (or Querying over the content of an @ElementCollection)
    primarykey
    data
    text
    <pre><code>public enum ReportStatus { SUCCCEED, FAILED; } public class Work { @ElementCollection @Enumerated(EnumType.STRING) List&lt;ReportStatus&gt; reportStatuses; } </code></pre> <p>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 :</p> <pre><code>public List&lt;Long&gt; queryHQL() { final String query = "SELECT w.id FROM Work w JOIN w.reportStatuses s WHERE s in (:rs)"; final List&lt;ReportStatus&gt; reportStatuses = new ArrayList&lt;ReportStatus&gt;(); reportStatuses.add(ReportStatus.FAILED); return this.entityManager.createQuery(query).setParameter("rs", reportStatuses).getResultList(); } </code></pre> <p>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 :</p> <pre><code>public List&lt;Long&gt; query() { final List&lt;ReportStatus&gt; reportStatuses = new ArrayList&lt;ReportStatus&gt;(); reportStatuses.add(ReportStatus.FAILED); final CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery&lt;Long&gt; criteriaQuery = builder.createQuery(Long.class); final Root&lt;Work&gt; workModel = criteriaQuery.from(Work.class); final ListJoin&lt;Work, ReportStatus&gt; status = workModel.joinList("reportStatuses"); final Predicate predicate = status.in(reportStatuses); criteriaQuery.where(predicate); criteriaQuery.select(workModel.&lt;Long&gt; get("id")); return this.entityManager.createQuery(criteriaQuery).getResultList(); } </code></pre> <p>I've also tried with the hibernate criteria API, but as the jpa2 one I've failed to find the correct the syntax.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload