title: Spring Data JPA: Creating Specification Subquery from different tables
body: <p>I am trying to build a specifications to predicate subquery for the below query.
  Select u.* from User u where u.login in (select ur.role_id from userRoles ur where ur.role_Id = roleId).</p>
  
  <p>till now this is the part I have built</p>
  
  <pre><code>public static Specification&lt;User&gt; userRoleId(String roleId) {
          return new Specification&lt;User&gt;() {
              @Override
              public Predicate toPredicate(Root&lt;User&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder builder) {
                  //return builder.equal(root.&lt;LocalDate&gt; get("scheduledDisbursementDate"), scheduledDisbursementDate);
                  Subquery&lt;UserRole&gt; subquery = query.subquery(UserRole.class);
                  Root&lt;UserRole&gt; subqueryRoot = subquery.from(UserRole.class);
                  subquery.select(subqueryRoot);
                  Predicate roleIdList = builder.equal(subqueryRoot.get("roleId"), roleId);
                  subquery.select(subqueryRoot).where(roleIdList);
                  return builder.exists(subquery);
              }
          };
      }
  </code></pre>
  
  <p>Can you please help me out to link the subquery with main query.</p>
  
  <p>Note: There are no joins defined in the Entity classes. evrything should be done through subquerys only</p>