title: Hibernate not saving into mapping table in ManyToMany
body: <p>I'm fairly new to hibernate and I'm trying to save data into a Student table and joining table which holds a composite key of the student and a related student skills table. I'm using spring MVC and a farm to populate this data. I feel I am incorrectly mapping either in the form or in hibernate but I'm unsure where I've gone wrong.</p>
  
  <p>Form:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form action="/SpringMVCProject/submitAdmissionForm" method="POST"&gt;
      First name: &lt;input type="text" name="studentFname" /&gt;
      &lt;br&gt;
      Last name: &lt;input type="text" name="studentLname" /&gt;
      &lt;br&gt;
      Date of birth: &lt;input type="text" name="studentDOB" /&gt;
      &lt;br&gt;
      Mobile num: &lt;input type="text" name="studentMobile" /&gt;
      &lt;br&gt;
        Select skills: &lt;select name="studentActivities.skill_id"&gt;
                          &lt;option value="1"&gt;Java&lt;/option&gt;
                          &lt;option value="2"&gt;C++&lt;/option&gt;
                          &lt;option value="3"&gt;C#&lt;/option&gt;
                          &lt;option value="4"&gt;.NET&lt;/option&gt;
                          &lt;option value="5"&gt;Python&lt;/option&gt;
                          &lt;option value="6"&gt;Ruby&lt;/option&gt;
                      &lt;/select&gt;
      &lt;br&gt;
      Country: &lt;input type="text" name="studentAddress.country" /&gt;
      &lt;br&gt;
      City: &lt;input type="text" name="studentAddress.city" /&gt;
      &lt;br&gt;
      Street: &lt;input type="text" name="studentAddress.street" /&gt;
      &lt;br&gt;
      Postal code: &lt;input type="text" name="studentAddress.pincode" /&gt;
      &lt;br&gt;
      &lt;input type="submit" value="Submit" /&gt;
  
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Student class:</p>
  
  <pre><code>@Entity
  public class Student {
      @Id
      @GeneratedValue
      private int studentID;
  
      private String studentFname;
  
      @Size(min=5, max=8)
      @Pattern(regexp="[a-z]*")
      private String studentLname;
  
      @Embedded
      private Address studentAddress;
  
      @Past
      private Date studentDOB;
  
      private Long studentMobile;
  
      @ManyToMany(cascade = CascadeType.ALL)
      private Set&lt;StudentActivities&gt; studentActivities = new HashSet&lt;StudentActivities&gt;();
  }
  </code></pre>
  
  <p>StudentActivities class:</p>
  
  <pre><code>@Entity
  @Table(name="student_skills")
  public class StudentActivities {    
      @Id
      @GeneratedValue
      private int skill_id;
  
      private String skill_name;
  }
  </code></pre>
  
  <p>Here is how I'm saving it in a controller class:</p>
  
  <pre><code>@RequestMapping(value = "/submitAdmissionForm", method  =   RequestMethod.POST)
  public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student1") Student student1, BindingResult result)
  {
      ModelAndView model;
      if(result.hasErrors())
          model   =   new ModelAndView("AdmissionForm");
      else
      {
          SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
          Session session = sessionFactory.openSession();
  
          session.beginTransaction();
          session.save(student1);
          session.getTransaction().commit();
  
          session.close();
          sessionFactory.close();
  
          model = new ModelAndView("AdmissionSuccess");
      }
  
      return model;
  }
  </code></pre>
  
  <p>So it currently saves all the data into the student table but not into my 'Student_student_skills' table. The 'student_skills' table already has data in it so I'm not entirely sure if it's even worth having the StudentActivities class since I'm not actually wanting to input data into the 'student_skills' table? However the main concern is that the data is not actually being saved into the mapping table. Where have I gone wrong here?</p>
  
  <p>EDIT:</p>
  
  <p>I was able to get it to save by creating a StudentActivities object and creating a seperate field in the Student class to hold the skill id and I just used that to set the skill id in the student activities, not sure if this is the correct way to do it. It seems most of the tutorials I've seen hard code the database entries and let hibernate do the work but in my case I already had data entries and wanted to select them from a list in the form. The only issue is that the skill_name values are left null since I didn't specifiy a skill_name and I have to hard code them in, is there any way I can prevent it from updating my student_skills table? </p>
  
  <pre><code>StudentActivities student_skills = new StudentActivities();
  student_skills.setSkill_id(student1.getSkillID());
  student1.getStudentActivities().add(student_skills);
  </code></pre>