title: Specification for filter joining multiple tables, including embedded primary key
body: <p>I want to build a <code>Specification</code> to filter a property that is not directly included in the entity but in the &quot;second join&quot;.</p>
  <p>The entities look (much simplified) like this:</p>
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class A {
      @Id
      private UUID id;
  
      @OneToMany(mappedBy=pk.a)
      private List&lt;B&gt; b;
  }
  
  @Entity
  public class B {
      @EmbeddedId
      private PK pk;
  }
  
  @Embeddable
  public class PK {
      @ManyToOne
      private A a;
      
      @ManyToOne
      private C c;
  }
  
  @Entity
  public class C {
      @Id
      private UUID id;
  
      @OneToMany(mappedBy=pk.b)
      private List&lt;B&gt; b;
  }
  </code></pre>
  <p>I want to get all A that have a specific C.id and I already have a repository method by name that is working:</p>
  <pre class="lang-java prettyprint-override"><code>List&lt;A&gt; findAllByBPKCId(String id);
  </code></pre>
  <p>Since I want some more filters I need to move to <code>JpaSpecificationExecutor</code> but I'm failing to build the <code>Specification</code> to do the same than the previous repository method.</p>
  <p>What I tried so far is:
  Calling</p>
  <pre class="lang-java prettyprint-override"><code>List&lt;A&gt; findAll(Specification specification);
  </code></pre>
  <p>with</p>
  <pre class="lang-java prettyprint-override"><code>new Specification() {
      @Override
      public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteraBuilder) {
          return criteriaBuilder.equal(root.join(&quot;b&quot;).join(pk.c).get(&quot;id&quot;), ID_TO_COMPARE);
      }
  }
  </code></pre>
  <p>However it won't find &quot;pk.c&quot;, I need to somehow &quot;get&quot; pk first before joining c. It throws an IllegalArgumentException because it is 'Unable to locate Attribute with the given name [pk.b]'.</p>
  <p>How can I join an entity that is nested in an embedded &quot;composite primary key&quot;?</p>