title: Spring data jpa Specification: How to filter a parent object by its children object property
body: <p>My entity classes are following</p>
  
  <pre><code>@Entity
  @table
  public class User {
  
      @OneToOne
      private UserProfile userProfile;
  
      // others
  }
  
  
  @Entity
  @Table
  public class UserProfile {
  
      @OneToOne
      private Country country;
  }
  
  @Entity
  @Table
  public class Country {
      @OneToMany
      private List&lt;Region&gt; regions;
  }
  </code></pre>
  
  <p>Now I want to get all the user in a particular region. I know the sql but I want to do it by spring data jpa Specification. Following code should not work, because regions is a list and I am trying to match with a single value. How to fetch regions list and compare with single object?</p>
  
  <pre><code> public static Specification&lt;User&gt; userFilterByRegion(String region){
  
  
          return new Specification&lt;User&gt;() {
              @Override
              public Predicate toPredicate(Root&lt;User&gt; root, CriteriaQuery&lt;?&gt; criteriaQuery, CriteriaBuilder criteriaBuilder) {
  
                  return criteriaBuilder.equal(root.get("userProfile").get("country").get("regions").get("name"), regionalEntity);
              }
          };
      }
  </code></pre>
  
  <p>Edit: Thanks for the help. Actually I am looking for the equivalent criteria query for the following JPQL</p>
  
  <pre><code>SELECT u FROM User u JOIN FETCH u.userProfile.country.regions ur WHERE ur.name=:&lt;region_name&gt;
  </code></pre>