title: Issue with JPA @ManyToMany From A to B(NOT bidirectional) via C table, but the connection to B not by primary key
body: <p>I have an JPA <code>@ManyToMany</code> issue</p>
  <p>I have One Main entity called <code>Report</code>. I have several entities connect to <code>Report</code> with <code>ManyToMany</code> Relation. The connection is done via extra table. i.e with <code>@JoinTable</code> annotation. Each relation is <code>lazy</code>.</p>
  <p>I have a function that fetch the <code>report</code> with all its relations. now, if i do fetch by accessing the property (i.e mark the function as <code>@Transcational</code> and access to each one of the property) everything works as expected. <strong>But</strong>, if I do</p>
  <pre><code> CriteriaBuilder cb = em.getCriteriaBuilder();
   CriteriaQuery&lt;Report&gt; query = cb.createQuery(Report.class);
   Root&lt;Report&gt; report = query.from(Report.class);
  
   ArrayList&lt;Predicate&gt; preds = new ArrayList&lt;Predicate&gt;();
   DomainSpecification.&lt;Report&gt;eager().toPredicate(report, query, cb);
   preds.add(DomainSpecification.&lt;Report&gt;idIsEqual(id).toPredicate(report, query, cb));
   query.where(cb.and(preds.toArray(new Predicate[preds.size()])));
  
   query.select(report);
   List&lt;Report&gt; results = em.createQuery(query).setMaxResults(1).get
  </code></pre>
  <p>I get all the relations as well but <strong>One of the relation</strong> (that has been connected to another table <strong>NOT</strong> by primary key) is fetching <strong>alots</strong> times. This means that the length of r.getProjects() is 13823 of the same value. i.e  of the same Project.</p>
  <p>I'm attaching here the relevant source from <code>Domain Specification</code>/<code>Report.java</code>/<code>Project.java</code> and <code>Keyword.java</code> (the keyword is just to show an example for an entity that works good with this kind of fetching)
  Maybe one can throw some light on this issues.</p>
  <p>Thanks,
  Oak</p>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <h1>Additional Information</h1>
  <h2>Code</h2>
  <p><code>Report.java</code></p>
  <pre><code>@Entity(name = &quot;reports&quot;) public class Report implements Serializable, Auditing,JppDbObject {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = &quot;REPORT_ID&quot;, unique = true, nullable = false)
  private Long id;
  
  @JsonIgnore
  @OneToOne(cascade = CascadeType.ALL, mappedBy = &quot;report&quot;)
  private ReportId report;
  /*
   * this here for updating the reports_ids table.
   * 
   * set json ignore over this field.
   */
  
  @Column(name = &quot;txt&quot;, nullable = false)
  private String txt;
  
  @Column(name = &quot;cleanTxt&quot;)
  private String cleanTxt;
  
  @CreatedDate
  @Column(name = &quot;CREATION_DATE&quot;, nullable = false)
  private Date creationDate;
  
  @LastModifiedDate
  @Column(name = &quot;LAST_MODIFIED&quot;, nullable = false)
  private Date lastModified;
  
  @Column(name = &quot;status&quot;, nullable = false)
  private Integer status;
  
  @CreatedBy
  @JsonIgnore
  @ManyToOne(optional = true)
  @JoinColumn(name=&quot;CREATOR_ID&quot;, referencedColumnName = &quot;ID&quot;)
  private User createdBy;
  
  @LastModifiedBy
  @ManyToOne(optional = true)
  @JoinColumn(name=&quot;MODIFIER_ID&quot;, referencedColumnName = &quot;ID&quot;)
  private User modifiedBy;
  
  @JsonIgnore
  @OneToMany(mappedBy=&quot;reportCommented&quot;)
  //TODO make sure comments works good with lazy. @OneToMany(fetch=FetchType.EAGER, mappedBy=&quot;reportCommented&quot;)
  private Set&lt;ReportComment&gt; comments;
   
  /* this is the important line*/
  @JsonIgnore
  @ManyToMany
  @JoinTable(
        name=&quot;reports_projects&quot;,
        joinColumns={@JoinColumn(name=&quot;report_id&quot;, referencedColumnName=&quot;REPORT_ID&quot;)},
        inverseJoinColumns={@JoinColumn(name=&quot;project&quot;, referencedColumnName=&quot;CODE&quot;)})
  private List&lt;Project&gt; projects;
  
  @JsonIgnore
  @ManyToMany
  @JoinTable(
          name=&quot;reports_keywords&quot;,
          joinColumns={@JoinColumn(name=&quot;report_id&quot;, referencedColumnName=&quot;REPORT_ID&quot;)},
          inverseJoinColumns={@JoinColumn(name=&quot;keyword_id&quot;, referencedColumnName=&quot;keyword_id&quot;)})
  private Set&lt;Keyword&gt; keywords;
  </code></pre>
  <p><code>Project.java</code></p>
  <p>@Entity(name = &quot;projects&quot;)
  public class Project implements Serializable{</p>
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = &quot;PROJECT_ID&quot;)
  private long id;
  
  @Column(name = &quot;CODE&quot;, nullable = false,unique = true)
  private String code;
  
  @Column(name = &quot;DESCRIPTION&quot;, nullable = false)
  private String description;
  
  @CreatedDate
  @Column(name = &quot;CREATION_DATE&quot;, nullable = false)
  private Date creationDate;
  
  @JsonIgnore
  @OneToMany(mappedBy = &quot;id.proj&quot;, cascade = {CascadeType.ALL}, orphanRemoval=true)
  private List&lt;ProjectPermission&gt; permissions;
  </code></pre>
  <p><code>Domain Specification</code></p>
  <p>public class DomainSpecification {</p>
  <pre><code>public static &lt;T&gt; Specification&lt;T&gt; eager() {
  
      return new Specification&lt;T&gt;() {
  
          @Override
          public Predicate toPredicate(Root&lt;T&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
              for (PluralAttribute&lt;? super T, ?, ?&gt; fetch : root.getModel().getPluralAttributes()) {
                  root.fetch(fetch, JoinType.LEFT);
              }
              query.distinct(true);
              return null;
          }
  
      };
  }
  
  public static &lt;T&gt; Specification&lt;T&gt; idIsEqual(final Long id) {
  
      return new Specification&lt;T&gt;() {
  
          @Override
          public Predicate toPredicate(Root&lt;T&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
              
                          return cb.equal(root.get(&quot;id&quot;),id);
          
          }
  
      };
  }
  </code></pre>
  <p>}</p>
  <h2>System</h2>
  <ul>
  <li>Spring (4.0.6.RELEASE)</li>
  <li>Spring-Data (1.6.2.RELEASE)</li>
  <li>Hibernate EntityManager (4.3.6.Final - 4.2.1)</li>
  <li>MySQl Connector (5.1.13)</li>
  <li>cglib (3.1)</li>
  <li>aspectj (1.8.1)</li>
  </ul>