title: Hibernate trying to persist same object twice
body: <p>I have 3 classes corresponding to 3 tables <code>V</code>, <code>D</code> and <code>P</code>. <code>D</code> had a FK to <code>V</code> (<code>FK_V</code>) and is join using OneToMany relationship. Also their exits a 4th table  <code>V_D_P</code> which has the relationship of these <code>V</code>, <code>D</code> and <code>P</code>.</p>
  
  <p>Following is what the data model for these looks like: </p>
  
  <pre><code>@Entity
  @Table(name = "V")                                                   
  public class V {
  
       @Id
       @GeneratedValue(strategy = GenerationType.SEQUENCE)
       @Column(name = "ID")
       private Long id;
  
      @OneToMany(cascade=CascadeType.ALL)
      @JoinColumn(name = "FK_V", referencedColumnName="Id", nullable = false)
      private Set&lt;D&gt; d;
  
      @OneToMany(cascade=CascadeType.ALL)
      @JoinColumn(name = "FK_V", referencedColumnName="Id", nullable = false)
      private Set&lt;V_D_P&gt; vdp;
  
      //Getters Setters etc.
  }
  
  
  @Entity
  @Table(name = "V_D_P")
  public class V_D_P {
  
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "ID")
        private Long id;
  
       @ManyToOne(cascade=CascadeType.ALL)
       @JoinColumn(name = "FK_D", nullable = false)
       private D d;
  
       @ManyToOne(cascade=CascadeType.ALL)
       @JoinColumn(name = "FK_P", nullable = false)
       private P p;
  
       //Getters Setters etc.       
  }
  
  @Entity
  @Table(name = "D")                                                   
  public class D {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "ID")
        private Long id;
  
      //Getters Setters etc.
  }
  
  @Entity
  @Table(name = "P")                                                  
  public class P {
  
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "ID")
        private Long id;
  
      //Getters Setters etc.
  }
  </code></pre>
  
  <p>Now I want to persist <code>V</code>, <code>D</code> and <code>P</code> along with their relationship. I am </p>
  
  <pre><code>V v = new V();
  
  D d = new D();
  Set&lt;D&gt; dSet = new HashSet&lt;&gt;();
  dSet.add(d);
  v.setD(dSet); // Adding d to V ....(1)
  
  P p = new P();
  V_D_P vdp = new V_D_P();
  
  vdp.setD(d); // Adding d to V_D_P ....(2)
  vdp.setP(p);
  
  Set&lt;V_D_P&gt; vdpSet = new HashSet&lt;V_D_P&gt;();
  vdpSet.add(vdp);
  v.setVdp(vdpSet);
  
  entityManager.persist(v);
  </code></pre>
  
  <p>Now you can see the I am adding the same <code>d</code> object twice. Once to P and once to V_D_P. However since these are the same objects, only once of them should persist. However based on the hibernate logs I see that hibernate is trying to insert 2 different objects. </p>
  
  <p>I also see the following exception: <strong>ORA-00001: unique constraint</strong></p>
  
  <p>Is there a way to let hibernate that these are the same objects and to persis them only once ?</p>