title: How to use javax.persistence.criteria.Predicate with complex types?
body: <p>I have a method that overrides the method "toPredicate":</p>
  
  <pre><code>@Override
  public Predicate toPredicate(Root&lt;Person&gt; root, CriteriaQuery&lt;?&gt; cq, CriteriaBuilder cb) {
      List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>I have to build the predicates.
  It's easy with simple types, for example:</p>
  
  <pre><code>@Entity
  @Table
  public class Person {
  
      @Column
      private String name;
  
      @Column
      private String surname;
  
  }
  </code></pre>
  
  <p>With this simple class I can do:</p>
  
  <pre><code>@Override
  public Predicate toPredicate(Root&lt;Person&gt; root, CriteriaQuery&lt;?&gt; cq, CriteriaBuilder cb) {
      List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
  
      if (StringUtils.isNotBlank(surname)) {
          predicates.add(cb.equal(root.get("surname"), surname));
      }
  
      if (StringUtils.isNotBlank(name)) {
          predicates.add(cb.equal(root.get("name"), name));
      }
  </code></pre>
  
  <p>But if the attribute is a complex type, how can I find the simple attribute contained in the complex type?</p>
  
  <p>This is a potential situation:</p>
  
  <pre><code>@Entity
  @Table
  public class Person {
  
      @Column
      private String name;
  
      @Column
      private String surname;
  
      @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      private List&lt;Document&gt; documents;
  
  }
  
  @Entity
  @Table
  public class Document {
  
      @Column
      private String type;
  
      @Column
      private String code;
  
  }
  </code></pre>
  
  <p>What I have to do if I want to make a predicate with the complex attribute "documents"?</p>
  
  <p>I tried:</p>
  
  <pre><code>@Override
  public Predicate toPredicate(Root&lt;Person&gt; root, CriteriaQuery&lt;?&gt; cq, CriteriaBuilder cb) {
      List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
  
      if (StringUtils.isNotBlank(type)) {
          predicates.add(cb.equal(root.get("documents").get("type"), type));
      }
  </code></pre>
  
  <p>But I have this Exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Illegal attempt to dereference path source [null.documents] of basic type
      at org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98)
      at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:191)
  </code></pre>
  
  <p>My task is to find every Person with a determinate kind of document.
  How can I do it?</p>
  
  <p>Thanks.</p>