Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume you are using a version of Hibernate that implements JPA 2.0. Here's a JPA 2.0 solution that should work with any compliant implementation.</p> <p>Please annotate <code>uuids</code> with JPA's <code>@ElementCollection</code> annotation. Don't use Hibernate's <code>@CollectionOfElements</code> as mentioned in some of the other answer comments. The latter has equivalent functionality but is <a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1821" rel="noreferrer">being deprecated</a>.</p> <p><code>Foobar.java</code> will look approximately like this:</p> <pre><code>@Entity public class Foobar implements Serializable { // You might have some other id @Id private Long id; @ElementCollection private List&lt;String&gt; uuids; // Getters/Setters, serialVersionUID, ... } </code></pre> <p>Here's how you can build a <code>CriteriaQuery</code> to select all <code>Foobar</code>s whose <code>uuids</code> contain "abc123". </p> <pre><code>public void getFoobars() { { EntityManager em = ... // EM by injection, EntityManagerFactory, whatever CriteriaBuilder b = em.getCriteriaBuilder(); CriteriaQuery&lt;Foobar&gt; cq = b.createQuery(Foobar.class); Root&lt;Foobar&gt; foobar = cq.from(Foobar.class); TypedQuery&lt;Foobar&gt; q = em.createQuery( cq.select(foobar) .where(b.isMember("abc123", foobar.&lt;List&lt;String&gt;&gt;get("uuids")))); for (Foobar f : q.getResultList()) { // Do stuff with f, which will have "abc123" in uuids } } </code></pre> <p>I made a self-contained proof-of-concept program while playing with this. I can't push it out right now. Please comment if you want the POC pushed to github.</p>
 

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