title: Many to many relationship in hibernate deleting the join table data?
body: <p>i have many to many relationship in hibernate.
  DB tables are:</p>
  
  <pre><code>events(event_id,name)
  speaker(speaker_id,name)
  event_speaker(event_id,speaker_id)
  </code></pre>
  
  <p>event.hbm.xml</p>
  
  <pre><code>&lt;set name="speakers" table="event_speakers" cascade="save-update"&gt;
  &lt;key column="event_id"/&gt;
  &lt;many-to-many class="com.manytomany.model.Speaker"/&gt;
  &lt;/set&gt;
  </code></pre>
  
  <p>speaker.hbm.xml</p>
  
  <pre><code>&lt;set name="events" table="event_speakers" cascade="save-update"&gt;
  &lt;key column="speaker_id"/&gt;
  &lt;many-to-many class="com.manytomany.model.Event"/&gt;
  &lt;/set&gt;
  </code></pre>
  
  <p>and i have implemented .equals and hashcode methods in Event.java and Speaker.java</p>
  
  <p>Event.java</p>
  
  <pre><code>@Override
  public boolean equals(Object obj) {
      // TODO Auto-generated method stub
      Event event = (Event)obj;
      return this.id == event.id;
  }
  @Override
  public int hashCode() {
      // TODO Auto-generated method stub
      return (int)id;
  }
  </code></pre>
  
  <p>Speaker.java</p>
  
  <pre><code>@Override
      public boolean equals(Object obj) {
          // TODO Auto-generated method stub
          Speaker speaker = (Speaker)obj;
          return this.id == speaker.id;
      }
  
      @Override
      public int hashCode() {
          // TODO Auto-generated method stub
          return (int)id;
      }
  </code></pre>
  
  <p>here is my logic</p>
  
  <pre><code>        session.beginTransaction();
          Event event = (Event) session.createCriteria(Event.class).add(Restrictions.eq("id", 1L)).uniqueResult();
          event.setSpeakers(new HashSet());
          Speaker speaker = new Speaker();
          speaker.setName("11");
          Speaker speaker2 = new Speaker();
          speaker2.setName("12");
  
          event.getSpeakers().add(speaker);
          event.getSpeakers().add(speaker2);
          session.save(event);
          session.getTransaction().commit();
  </code></pre>
  
  <p>when i run above code hibernate is executing like this</p>
  
  <pre><code>Hibernate: select this_.event_id as event1_0_0_, this_.event_name as event2_0_0_ from events this_ where this_.event_id=?
  Hibernate: select max(speaker_id) from speakers
  Hibernate: insert into speakers (speaker_name, speaker_id) values (?, ?)
  Hibernate: delete from event_speakers where event_id=?
  Hibernate: insert into event_speakers (event_id, elt) values (?, ?)
  </code></pre>
  
  <p>why it is deleting from the join table, i want to insert new row instead of deleting in join table.</p>