title: JPA Self referencing entity with relation to sibling join table
body: <p>I have an self referencing entity with children and parent. I have a join table to store the mutually exclusive siblings. When create the entity in the database, I get this exception...</p>
  
  <p>Caused by: org.springframework.orm.jpa.JpaSystemException: org.hibernate.PersistentObjectException: detached entity passed to persist</p>
  
  <p>the main table will have </p>
  
  <pre><code>ID Text Parent
  1  ABC   null
  2  CBD    1
  3  XYZ    1
  </code></pre>
  
  <p>Join table should have </p>
  
  <pre><code>entity sibling
  2        3
  </code></pre>
  
  <p>is the Entity mapping correct?</p>
  
  <pre><code>@Entity
  public class TestEntity {
     @id
     @GeneratedValue
     private int id;
     private String Text;
     @ManyToOne
     private TestEntity parent;
  
  @OneToMany(cascade=CascadeType.ALL)
  @JoinTable(name="mutuallyExclusiveSiblings",
          joinColumns={@JoinColumn(name="entity", referencedColumnName="id")},
          inverseJoinColumns={@JoinColumn(name="sibling", referencedColumnName="id")})
          private Set&lt;TestEntity&gt; exclusiveSiblings;
  
  
  }
  </code></pre>
  
  <p>Any help implementing this is appreciated. Thanks.</p>