title: how to delete or save a many to many relationship in hibernate & spring
body: <p>i have a many to many relationship between 2 tables.
  below are the two tables with mappings.
  StaffSearchCriteria is used to search staffs having skills selected.
  this search criteria is persisted in DB so that we can again lookup it later.</p>
  <p>the issue i am facing is that i am not able to properly save this data.
  i am not understanding the &quot;cascade&quot; part of the mapping.
  due to which, if i do &quot; <code>Cascade.ALL</code> &quot;, the data is saved properly, but when i delete the search criteria, then it also deletes the <code>Skill</code> entries associated with it, which is wrong.
  i just want that if i delete Skill, <code>StaffSearchCriteria</code> entry should not get deleted and similarly for the <code>Skill</code>;
  Only the selected data should be deleted and its entry in the mapping table.
  the other table should not be affected by that action.</p>
  <h3>StaffSearchCriteria</h3>
  <pre><code>    @Entity
  @Table(name = &quot;staff_search_criteria&quot;)
  @NamedQueries({
      @NamedQuery(name = &quot;StaffSearchCriteria.findAll&quot;, query = &quot;SELECT s FROM StaffSearchCriteria s&quot;)})
  public class StaffSearchCriteria implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = &quot;id&quot;)
      private Long id;
  
      @Basic(optional = false)
      @NotNull
      @Column(name = &quot;version&quot;)
      private long version;
  
      @Lob
      @Size(max = 2147483647)
      @Column(name = &quot;description&quot;)
      private String description;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 200)
      @Column(name = &quot;name&quot;)
      private String name;
  
      @ManyToMany(mappedBy = &quot;staffSearchCriteriaCollection&quot;, cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
      private Collection&lt;Skill&gt; skillCollection;
  </code></pre>
  <p>==================================================</p>
  <h3>Skill</h3>
  <pre><code>@Entity
  @Table(name = &quot;skill&quot;)
  @NamedQueries({
      @NamedQuery(name = &quot;Skill.findAll&quot;, query = &quot;SELECT s FROM Skill s&quot;)})
  public class Skill implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = &quot;id&quot;)
      private Long id;
  
      @Basic(optional = false)
      @NotNull
      @Column(name = &quot;version&quot;)
      private long version;
  
      @Lob
      @Size(max = 2147483647)
      @Column(name = &quot;description&quot;)
      private String description;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 100)
      @Column(name = &quot;name&quot;)
      private String name;
  
      @JoinTable(name = &quot;mission_skill&quot;, joinColumns = {
          @JoinColumn(name = &quot;skill_id&quot;, referencedColumnName = &quot;id&quot;)}, inverseJoinColumns = {
          @JoinColumn(name = &quot;mission_skills_id&quot;, referencedColumnName = &quot;id&quot;)})
      @ManyToMany(fetch = FetchType.LAZY)
      private Collection&lt;Mission&gt; missionCollection;
  
      @JoinTable(name = &quot;staff_search_criteria_skill&quot;, joinColumns = {
          @JoinColumn(name = &quot;skill_id&quot;, referencedColumnName = &quot;id&quot;)}, inverseJoinColumns = {
          @JoinColumn(name = &quot;staff_search_criteria_skills_id&quot;, referencedColumnName = &quot;id&quot;)})
      @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
      private Collection&lt;StaffSearchCriteria&gt; staffSearchCriteriaCollection;
  </code></pre>
  <h3>Save method</h3>
  <pre><code>public StaffSearchCriteria saveStaffSearchCriteria(StaffSearchCriteria staffSearchCriteria) {
          logger.info(&quot; [StaffSearchCriteriaDAOImpl] saveStaffSearchCriteria method called. - staffSearchCriteria = &quot; + staffSearchCriteria);
          Session session = sessionFactory.getCurrentSession();
          session.saveOrUpdate(staffSearchCriteria);
          return staffSearchCriteria;
      }
  </code></pre>
  <h3>delete method</h3>
  <pre><code> public void deleteStaffSearchCriteria(Long id) {
              logger.info(&quot; [StaffSearchCriteriaDAOImpl] deleteStaffSearchCriteria method called. - id = &quot; + id);
              Session session = sessionFactory.getCurrentSession();
              Query query = session.createQuery(&quot;FROM StaffSearchCriteria ssc where ssc.id = &quot; + id);
              if(null != query.uniqueResult()){
                  StaffSearchCriteria staffSearchCriteria = (StaffSearchCriteria)query.uniqueResult();
                  session.delete(staffSearchCriteria);
              }
          }
  </code></pre>
  <p>Please help me here.What am i doing wrong?</p>