title: Hibernate Many-to-Many self join duplicate entries
body: <p>I try do save a graph to database.
  It consists a simple class with "tag" attribute.
  This class may have parents and children.</p>
  
  <p>After save the root class, all graph is saved too, but when a node have two parents, these two parents are duplicated in database.</p>
  
  <p>Here is the mapped node class:</p>
  
  <pre><code>@Entity
  @Table(name="acttest")
  public class ActivityTest {
  
      @Id
      @Column(name="id_activity")
      @GeneratedValue
      private Long idActivity;
  
  
      @Column(name="tag")
      private String tag;
  
  
      @ManyToMany(cascade=CascadeType.ALL,  fetch = FetchType.EAGER )
      @JoinTable(name="dependencies",
          joinColumns={@JoinColumn(name="id_master")},
          inverseJoinColumns={@JoinColumn(name="id_slave")})
      private Set&lt;ActivityTest&gt; nextActivities = new HashSet&lt;ActivityTest&gt;();
  
  
      @ManyToMany(mappedBy="nextActivities",  fetch = FetchType.EAGER)
      private Set&lt;ActivityTest&gt; previousActivities = new HashSet&lt;ActivityTest&gt;();
  
      (getters and setters)
  }
  </code></pre>
  
  <p>Here is my test class:</p>
  
  <pre><code>    public static void main(String[] args) {
          Session s;
          ActivityList al = new ActivityList();
  
          al.addRoot("A");
          al.addNextActivity("A", "A1");
          al.addNextActivity("A", "A2");
          al.addNextActivity("A", "A3");
  
          al.addNextActivity("A1", "B");
          al.addNextActivity("A1", "BA12");
  
          al.addNextActivity("A2", "BA12");
  
          al.addNextActivity("B", "B1");
          al.addNextActivity("B", "B2");
  
          al.addNextActivity("B2", "B2.2");
  
  
          s = ConnFactory.getSession();
          s.beginTransaction();
  
          s.save( al.getRoot() );
  
          s.getTransaction().commit();
          s.close();
  }
  </code></pre>
  
  <p>Here is the wrapper list:</p>
  
  <pre><code>public class ActivityList {
      private List&lt;ActivityTest&gt; activities;
      private ActivityTest root = null;
  
      public void addRoot( String tag ) {
          if ( root == null ) {
              ActivityTest activity = new ActivityTest( tag );
              activities.add(activity);
              root = activity;
          }
      }
  
      public ActivityList() {
          activities = new ArrayList&lt;ActivityTest&gt;();
      }
  
      public void addNextActivity( String master, String next ) {
          ActivityTest newAct = new ActivityTest( next );
          activities.add(newAct);
          for ( ActivityTest act : activities  ) {
              if ( act.getTag().equalsIgnoreCase( master )  ) {
                  act.getNextActivities().add( newAct );
              }
          }
      }
  
      public ActivityTest getRoot() {
          return root;
      }
  
      public List&lt;ActivityTest&gt; getActivities() {
          return activities;
      }
  
      public void setActivities( List&lt;ActivityTest&gt; activities ) {
          root = null;
          this.activities = activities;
          for ( ActivityTest act : activities  ) {
              if ( act.getPreviousActivities().size() == 0 ) {
                  root = act;
                  break;
              }
          }
      }
  
  
  }
  </code></pre>
  
  <p>The graph I design to test:</p>
  
  <p><img src="https://i.stack.imgur.com/6ApFh.jpg" alt="enter image description here"></p>
  
  <p>The graph table:</p>
  
  <p><img src="https://i.stack.imgur.com/7kd1N.png" alt="enter image description here"></p>
  
  <p>And the dependency table:</p>
  
  <p><img src="https://i.stack.imgur.com/w7XKr.png" alt="enter image description here"></p>
  
  <p>Note the duplicity of "BA12" node: onde for "A1" child and other for "A2" child.</p>
  
  <p>How can I make one instance of "BA12" node?</p>