title: How to debug "Found two representations of same collection"?
body: <p>I have found <a href="https://stackoverflow.com/questions/6033314/use-map-as-property-of-model-in-play">several</a> <a href="https://stackoverflow.com/questions/7002129/play-framework-and-the-use-of-collections-in-model-classes">questions</a> <a href="https://stackoverflow.com/questions/8318692/hibernateexception-found-two-representations-of-same-collection">about</a> this, but none with a complete explaintation of the problem, and how to debug it - the answers are all anecdotal.</p>
  
  <p>The problem is that in a Play 1.2.4 JPA test, I'm getting this exception when I <code>save()</code> a model:</p>
  
  <blockquote>
    <p>org.hibernate.HibernateException: Found two representations of same
    collection: models.Position.projects</p>
  </blockquote>
  
  <p>I would like to know:</p>
  
  <ol>
  <li>Is there a documentation of this problem in general, unrelated to Play? The issue is in hibernate, yet a lot of the Google results on this are within Play apps.</li>
  <li>What are some basic best practices to avoid this problem?</li>
  <li>Is it caused by Play? Or something I'm doing wrong?</li>
  <li>How to resolve in my specific case?</li>
  </ol>
  
  <p><a href="https://github.com/ripper234/play-two-representation-same-collection" rel="nofollow noreferrer">Here is a reproduction of the problem on github</a>. I have four entities:</p>
  
  <pre><code>@Entity
  public class Person extends Model {
      public String name;
  
      @OneToMany(cascade = CascadeType.ALL)
      public List&lt;Position&gt; positions;
  }
  
  
  @Entity
  public class Position extends Model {
      public Position(){}
      public Position(Company companies) {
          this.companies = companies;
          this.projects = new ArrayList&lt;Project&gt;();
      }
  
      @OneToOne
      public Company companies;
  
      @ManyToOne
      public Person person;
  
      @OneToMany
      public List&lt;Project&gt; projects;
  }
  
  @Entity
  public class Company extends Model {
      public String name;
  }
  
  @Entity
  public class Project extends Model {
      public Project(){}
      public Project(String field, String status){
          this.theField = field;
          this.status = status;
      }
  
      @ManyToOne
      public Position position;
  
      public String theField;
      public String status;
  }
  </code></pre>
  
  <p>And my persistence code:</p>
  
  <pre><code>Company facebook = new Company();
  facebook.name = "Facebook";
  facebook.save();
  Company twitter = new Company();
  twitter.name = "Twitter";
  twitter.save();
  
  Person joe = new Person();
  joe.name = "Joe";
  joe.save();
  
  joe.positions = new ArrayList&lt;Position&gt;();
  
  Position joeAtFacebook = new Position(facebook);
  joeAtFacebook.projects.add(new Project("Stream", "Architect"));
  joeAtFacebook.projects.add(new Project("Messages", "Lead QA"));
  joe.positions.add(joeAtFacebook);
  
  Position joeAtTwitter = new Position(twitter);
  joeAtTwitter.projects.add(new Project("Steal stuff from Facebook", "CEO"));
  joe.positions.add(joeAtTwitter);
  joe.save();
  </code></pre>
  
  <p>BTW, I've tried adding the <a href="http://www.playframework.org/modules/associations-1.0.1/home" rel="nofollow noreferrer">Play associations module</a> as one person suggested, and it does't seem to help.</p>
  
  <p>I see that indeed that tables that are created are duplicate in a sense:</p>
  
  <p>I have both a <code>person_position</code> table and a <code>position table</code>, where both contain similar fields: <code>person_position</code> contains a <code>Person_id</code> and <code>positions_id</code>, while the <code>position</code> table contain <code>id</code> (meaning position id), <code>person_id</code>, and <code>companies_id</code>. So I understand some kind of unintended redundancy is created by my model definition, but I don't really understand how to solve it. </p>
  
  <p>I thought this might be related to bi-directional mappings, but here is a <a href="https://github.com/ripper234/play-two-representation-same-collection/tree/no-bidirectional" rel="nofollow noreferrer">branch where the model is uni-directional</a> (I removed some back-references) - and the problem still occurs.</p>