title: Spring Data Join with Specifications
body: <p>I'm trying to convert this raw sql query:</p>
  
  <pre><code>select product.* from following_relationship
  join product on following_relationship.following=product.owner_id
  where following_relationship.owner=input 
  </code></pre>
  
  <p>Into Spring Data specifications, i think that my issue so far is on joining those tables.</p>
  
  <p>Here is my current conversion in Specification:</p>
  
  <pre><code>protected Specification&lt;Product&gt; test(final User user){
     return new Specification&lt;Product&gt;() {
         @Override
         public Predicate toPredicate(Root&lt;Product&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
             Join&lt;FollowingRelationship,Product&gt; pfJoin = query.from(FollowingRelationship.class).join("following");
             pfJoin.on(cb.equal(pfJoin.get("following"),"owner"));
             return  query.where(cb.equal(pfJoin.get("following"),user)).getGroupRestriction();
  
         }
     };
  }
  </code></pre>
  
  <p>And I'm getting this exception :</p>
  
  <pre><code>Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessA
  piUsageException: org.hibernate.hql.internal.ast.InvalidWithClauseException: with clause can only reference columns in the driving table 
  </code></pre>
  
  <p>I will like to add that I'm new at Spring framework for instance this is my first application on spring, so my apologies for the newbie question ;)</p>
  
  <p>Edit: added entities Product, FollowingRelationShip</p>
  
  <pre><code>Entity
  @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "json_id_prop")
  public class FollowingRelationship extends BaseEntity {
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "OWNER", referencedColumnName = "uuid")
      private User owner;
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "FOLLOWING", referencedColumnName = "uuid")
      private User following;
  
      public User getOwner() {
          return owner;
      }
  
      public void setOwner(User owner) {
          this.owner = owner;
      }
  
      public User getFollowing() {
          return following;
      }
  
      public void setFollowing(User following) {
          this.following = following;
      }
  
  }
  
  @Entity
  @Table(name = "product")
  @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "json_id_prop")
  public class Product extends BaseEntity {
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "OWNER_ID", referencedColumnName = "uuid")
      private User owner;
      @NotNull
      private String name;
      @NotNull
      private String description;
      @NotNull
      private String price;
      @NotNull
      private String brand;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public String getPrice() {
          return price;
      }
  
      public void setPrice(String price) {
          this.price = price;
      }
  
      public String getBrand() {
          return brand;
      }
  
      public void setBrand(String brand) {
          this.brand = brand;
      }
  
      public User getOwner() {
          return owner;
      }
  
      public void setOwner(User owner) {
          this.owner = owner;
      }
  
  
  }
  </code></pre>
  
  <p>Product and FollowingRelationShip entities do no have any explicit relationship, hence the join on my implementation about.What i want to achieve is to get all products from all users which another user follow in Spring data Specifications. </p>