title: Spring Many to many bidirectional saving data
body: <p>I have two classes, whose tables have bidirectional relationship with each other. 
  An Employee can be associated with multiple service categories and vice versa. The join table is declared within Service Categories class.</p>
  
  <p>When I wish to save Employee object then nothing is saved within the join table, but if I try to print the employee related service categories, then I am getting all the details</p>
  
  <pre><code>if(employee.getServiceCategories()!= null &amp;&amp; employee.getServiceCategories().size()&gt;0){
    for(ServiceCategory serviceCategory : employee.getServiceCategories()){
      logger.debug("employee's serviceCategory id : " + serviceCategory.getId());
      logger.debug("employee's ServiceCat Name  : " + serviceCategory.getServiceCatName());
    }
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Employee.java</strong></p>
  </blockquote>
  
  <pre><code>@Entity
  @Table(name = "employee")
  public class Employee extends BaseEntity {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
  
    @ManyToMany(mappedBy="employees")
    private List&lt;ServiceCategory&gt; serviceCategories = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <blockquote>
    <p><strong>ServiceCategory.java</strong></p>
  </blockquote>
  
  <pre><code>@Entity
  @Table(name = "servicecategory")
  public class ServiceCategory  extends BaseEntity {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
  
    @ManyToMany(targetEntity = Employee.class, cascade = { CascadeType.ALL })
    @JoinTable(name = "servicecategory_employee", joinColumns = { @JoinColumn(name = "servicecatid") }, inverseJoinColumns = { @JoinColumn(name = "empid") })
    private List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p>Thanks for your help.</p>
  
  <hr>
  
  <blockquote>
    <p><strong>UPDATE</strong></p>
  </blockquote>
  
  <p>I changed my code in Employee.java to </p>
  
  <pre><code>@ManyToMany(mappedBy="employees", cascade = CascadeType.ALL)
  private List&lt;ServiceCategory&gt; serviceCategories = new ArrayList&lt;&gt;(); 
  </code></pre>
  
  <p>Also I am now using following code while saving the Employee in my DAO class `  </p>
  
  <pre><code>public void addEmployee(Employee employee) throws SmrException {
          Session session = this.sessionFactory.getCurrentSession();
          session.merge(employee);
          logger.info("Employee saved successfully, employee Details="
                  + employee);
      }
  </code></pre>
  
  <p>But still the result remain same. I am unable to save data in the join table</p>