title: Only mapping from department side is mapped and not from inverse side.How can i say this is a Many to Many
body: <p>Below is the code   </p>
  
  <pre><code> package com.hibernate.mapping.mtm;
  
      import java.util.ArrayList;
      import java.util.List;
  
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.Transaction;
      import org.hibernate.cfg.Configuration;
  
      /**
       * @author Prajapati
       *
       */
      public class Main {
  
  
          /**
           * @param args
           */
          public static void main(String[] args) {
              // TODO Auto-generated method stub
              SessionFactory sf= new Configuration().configure("com/hibernate/mapping/mtm/hibernate.cfg.xml").buildSessionFactory();
              Session sn  = sf.openSession();
              Transaction tx = sn.beginTransaction();
  
  
              Department dept1  = new Department();
              dept1.setId(20l);
              dept1.setDepartmentName("Development");
  
  
              Department dept2  = new Department();
              dept2.setId(10l);
              dept2.setDepartmentName("Accounts");
  
              Department dept3  = new Department();
              dept3.setId(30l);
              dept3.setDepartmentName("QA");
  
              Department dept4  = new Department();
              dept4.setId(40l);
              dept4.setDepartmentName("Designing");
  
  
              Employee emp1 = new Employee();
              emp1.setEmployeeID(30l);
              emp1.setEmployeeName("Michele");
  
              Employee emp2 = new Employee();
              emp2.setEmployeeID(40l);
              emp2.setEmployeeName("Devendra");
  
  
              Employee emp3 = new Employee();
              emp3.setEmployeeID(50l);
              emp3.setEmployeeName("Prabhat");
  
              Employee emp4 = new Employee();
              emp4.setEmployeeID(60l);
              emp4.setEmployeeName("Himanshi");
  
              Employee emp5 = new Employee();
              emp5.setEmployeeID(70l);
              emp5.setEmployeeName("Kanika");
  
  
              List&lt;Employee&gt; employeesQA = new ArrayList&lt;Employee&gt;();
  
              employeesQA.add(emp3);
              employeesQA.add(emp2);
  
  
              List&lt;Employee&gt; employeesDev = new ArrayList&lt;&gt;();
  
              employeesDev.add(emp3);
              employeesDev.add(emp4);
  
  
              List&lt;Employee&gt; employeesAccounts = new ArrayList&lt;&gt;();
  
              employeesAccounts.add(emp5);
              employeesAccounts.add(emp1);
  
  
  
  
              dept3.setEmployees(employeesQA);
              dept1.setEmployees(employeesDev);
  
              List&lt;Department&gt; departments = new ArrayList&lt;Department&gt;();
  
              departments.add(dept1);
              departments.add(dept2);
              departments.add(dept3);
              departments.add(dept4);
  
              //emp5 works in all the departments
              emp5.setDepartments(departments);
  
  
              /*List&lt;Department&gt; departments3 = new ArrayList&lt;Department&gt;();
  
              departments3.add(dept1);
              departments3.add(dept2);
  
              emp3.setDepartments(departments3);
               */
  
  
              sn.save(emp5);
              sn.save(dept3);
              sn.save(dept1);
  
              /**
               * When i am running this program. 
               * Only mapping from department side is mapped and not from inverse side.How can i say this is a M2M 
               * while i am unable to Map from both the sides in terms of Hibernate
               */
  
              //      sn.save(p);
              tx.commit();
              sn.close();
  
  
          }
  
      }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.hibernate.mapping.mtm;
  
      import java.util.List;
  
      import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.ManyToMany;
  
  
      @Entity
      public class Department {
  
          @Id
          private Long id;
  
  
          private String departmentName;
  
          //@ManyToMany(cascade=CascadeType.ALL,mappedBy="departments")
          @ManyToMany(cascade=CascadeType.ALL)
          private List&lt;Employee&gt; employees;
  
          public Long getId() {
              return id;
          }
  
          public void setId(Long id) {
              this.id = id;
          }
  
          public String getDepartmentName() {
              return departmentName;
          }
  
          public void setDepartmentName(String departmentName) {
              this.departmentName = departmentName;
          }
  
          public List&lt;Employee&gt; getEmployees() {
              return employees;
          }
  
          public void setEmployees(List&lt;Employee&gt; employees) {
              this.employees = employees;
          }
  
  
  
      }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.hibernate.mapping.mtm;
  
  import java.util.List;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.ManyToMany;
  
  @Entity
  public class Employee {
  
      @Id
      private Long employeeID;
  
      private String employeeName;
  
  
      @ManyToMany(mappedBy="employees",cascade=CascadeType.ALL)
      //Mapped by says here that go to employees in opposite table to get details of departments for employees and the key of the relationship is on the other side.
      //i.e. you are considering department side the owner side now by saying mappedby.
      //@ManyToMany(cascade=CascadeType.ALL)
      private List&lt;Department&gt; departments;
  
      public Long getEmployeeID() {
          return employeeID;
      }
  
      public void setEmployeeID(Long employeeID) {
          this.employeeID = employeeID;
      }
  
      public String getEmployeeName() {
          return employeeName;
      }
  
      public void setEmployeeName(String employeeName) {
          this.employeeName = employeeName;
      }
  
      public List&lt;Department&gt; getDepartments() {
          return departments;
      }
  
      public void setDepartments(List&lt;Department&gt; departments) {
          this.departments = departments;
      }
  
  
  
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;org.gjt.mm.mysql.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;root&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://127.0.0.1/mapping&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
          &lt;!-- &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt; --&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/property&gt;
  
  
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
       &lt;mapping class="com.hibernate.mapping.mtm.Employee"/&gt;
      &lt;mapping class="com.hibernate.mapping.mtm.Department"/&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <hr>
  
  <p>I am very confused over why didn't Hibernate saved all the entries for emp5 as it is in all the departments and the relationship is many to many
      there is no entry done for the employee emp5.</p>
  
  <p>Below are the tables with respective data:</p>
  
  <pre><code>mysql&gt; select * from employee;
  
  
      | employeeID | employeeName |
  
  
      |         40 | Devendra     |
  
      |         50 | Prabhat      |
  
      |         60 | Himanshi     |
  
      |         70 | Kanika       |
  
  
  
      mysql&gt; select * from department;
  
  
  
      | id | departmentName |
  
  
      | 10 | Accounts       |
  
      | 20 | Development    |
  
      | 30 | QA             |
  
      | 40 | Designing      |
  
      mysql&gt; select * from department_employee;
  
  
  
      | departments_id | employees_employeeID |
  
      |             20 |                   50 |
  
      |             20 |                   60 |
  
      |             30 |                   50 |
  
      |             30 |                   40 |
  </code></pre>