title: Spring Hibernate inserting parent along with children
body: <p>My program requirement is to add a new parent to the database along with the children. So far I have this method in the parent class:</p>
  
  <pre><code>  @Transient
  public void addLecturer(Lecturer lecturer) {
      if(lecturers == null) {
          lecturers = new ArrayList&lt;Lecturer&gt;();
          }
      lecturer.setChecker(this);
  
      this.lecturers.add(lecturer);
   }
  </code></pre>
  
  <p>Then a save parent in my parent dao:</p>
  
  <pre><code>    @Override
  public void addChecker(Lecturer checker) {
        Session session = sessionFactory.getCurrentSession();
  
        session.save(checker);
  
       }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>     @RequestMapping(value="/matching_page", method = RequestMethod.GET)
  public ModelAndView get(@ModelAttribute("checker") Lecturer checker, BindingResult 
  
    result) { 
  ArrayList&lt;String&gt; lecturers = new ArrayList&lt;String&gt;();   
      lecturers.add("Somma");
      lecturers.add("Trina");
      lecturers.add("Adam");
      lecturers.add("Eve");
  
      HashMap&lt;String, ArrayList&lt;String&gt;&gt; model = new HashMap&lt;String 
  
    ArrayList&lt;String&gt;&gt;(); 
       model.put("lecturerList", lecturers); 
     return new ModelAndView("matching_page", "model", model);
  
  }
  
    @RequestMapping(value="/matching_page", method = RequestMethod.POST)
  public ModelAndView hello(@ModelAttribute("checker") Lecturer checker, 
  
     BindingResult result) { 
      lecturerService.addChecker(checker);
  
      return new ModelAndView ("redirect:/admin");
  }
  </code></pre>
  
  <p>The jsp form:</p>
  
  <pre><code>     &lt;c:url var="saveUrl" value="/matching_page" /&gt;
  &lt;form:form modelAttribute="checker" method="POST" action="${saveUrl}"&gt;
  
  &lt;td&gt;Checker &lt;/td&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;form:hidden path="lecturerId" value="" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
    &lt;tr&gt;
     &lt;td&gt;&lt;form:label path="name"&gt;Checker Name:&lt;/form:label&gt;&lt;/td&gt;
     &lt;td&gt;&lt;form:input path="name"/&gt;&lt;/td&gt;
  
    &lt;/tr&gt;
  
    &lt;tr&gt;
     &lt;td&gt;&lt;form:label path="email"&gt;Email&lt;/form:label&gt;&lt;/td&gt;
     &lt;td&gt;&lt;form:input path="email"/&gt;&lt;/td&gt;
  
    &lt;/tr&gt;
     &lt;td&gt;Lecturers&lt;/td&gt;
     &lt;td&gt;
        &lt;form:select path="name" multiple="true"&gt;
          &lt;form:option value="0" &gt;Select &lt;/form:option&gt;
              &lt;form:options items="${model.lecturerList}" /&gt;
  
        &lt;/form:select&gt;
    &lt;/td&gt;
  
    &lt;div id="lower"&gt;
  &lt;input  type="submit" value="Save Selection" class="button"/&gt;
  &lt;/div&gt;
     &lt;/form:form&gt;
  </code></pre>
  
  <p>My database:</p>
  
  <pre><code>  CREATE TABLE `lecturers` (
   `lecturer_id` BIGINT(10) NOT NULL AUTO_INCREMENT,
   `name` VARCHAR(255) NULL DEFAULT NULL,
   `email` VARCHAR(255) NULL DEFAULT NULL,
   `checker_id` BIGINT(20) NULL DEFAULT NULL,
    PRIMARY KEY (`lecturer_id`),
    FOREIGN KEY (`checker_id`) REFERENCES `lecturers` (`lecturer_id`)
   ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p>My model</p>
  
  <pre><code>   @Entity
     @Table(name = "lecturers")
     @Component
     public class Lecturer implements Serializable{
  
     @ManyToOne(cascade = CascadeType.ALL)
     @JoinColumn(name="checker_id")
     private Lecturer checker;
  
     @OneToMany(mappedBy="checker", orphanRemoval=true)
     private List&lt;Lecturer&gt; lecturers = new ArrayList&lt;Lecturer&gt;();
     }
  </code></pre>
  
  <p>The form has a parent object (checker) bound to it and I want to add one or more children selected from the select list and saved. </p>
  
  <p>The save works but but it saves all new objects as children and doesn't save the checker parent separately with a checker id. I'm not sure what the problem is and would like to know.</p>