title: jpa criteria query, match any of list passed as parameter
body: <p>I'm trying to write a DAO method where I select any number of Widgets that have a matching tag. So in entities like:</p>
  
  <pre><code>@Entity
  public class Widget {
      @Id
      private id;
  
      @OneToMany (cascade = CascadeType.ALL)
      private List&lt;Tag&gt; tagList;
  }
  
  
  @Entity
  public class Tag {
      @Id
      private int id;
  
      String tagValue;
  }
  </code></pre>
  
  <p>and in my DAO, I'm trying to write a method that will take in a list of strings</p>
  
  <pre><code>List&lt;String&gt; myList = new ArrayList&lt;String&gt;();
  myList.add("tom");
  myList.add("dick");
  myList.add("harry");
  </code></pre>
  
  <p>to the DAO</p>
  
  <pre><code>public List&lt;Widget&gt; getWidgetsMatchingTags(EntityManager entityManager, List&lt;String&gt; tagValues) {
      CriteriaBuilder cb = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;Widget&gt; query = cb.createQuery(Widget.class);
      Root&lt;Widget&gt; widgetRoot = query.from(Widget.class);
      Join&lt;Widget, Tag&gt; tagJoin = widgetRoot.join(Widget_.tagList);
  
      // this works
      query.select(widgetRoot).where(cb.and(
             cb.equal(tagJoin.get(Tag_.value), "tom")));
  
     // but if I try to add another predicate, it does not work, 
     // it returns a list size of 0
  
     query.select(widgetRoot).where(cb.and(
             cb.equal(tagJoin.get(Tag_.value), "tom")),
             cb.equal(tagJoin.get(Tag_.value), "harry"));
  
  }
  </code></pre>
  
  <p>would like to be able to be able to match all or any of the given tags, and thought that that would be a matter of cb.and() or cb.or()</p>
  
  <p>I google around and found a few link, like this on <a href="https://www.objectdb.com/java/jpa/query/jpql/collection#NOT_MEMBER_OF" rel="nofollow noreferrer">collections</a>, also <a href="https://stackoverflow.com/questions/24090712/how-do-i-write-a-jpa-criteria-query-that-matches-a-collection-exactly">this</a> which I don't think was totally relevant...I also went thru the pdf jpa documentation, the <a href="https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#criteria" rel="nofollow noreferrer">online user guide</a> and did not see this covered explicitly. Please let me know if I missed something</p>
  
  <p>Also note that I need to match a <em>tag value</em>, as the string is a field of the tag obj. </p>
  
  <p>thanks for any help!!</p>
  
  <p>thanks for any help</p>