title: SpringMVC/JSTL/JPA Integration: update existing child element and add new child element to a parent entity using Spring MVC
body: <p>I'm new to SpringMVC and JPA. I'm doing a student project using these frameworks. In the project, I want to implement a JSP to update existing child element and add new child element to a parent entity.
  My solution worked. But I think there should be a more elegant or "standard" way to do it. Following is what my code looks like:</p>
  
  <p>JPA Entity</p>
  
  <pre><code>@Entity
  public class Parent {
      @Id
      @GeneratedValue
      private Integer id;
      private String name;
      @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
      private Set&lt;Child&gt; children = new HashSet&lt;Child&gt;();
      // constructors, getters and setters
  }
  
  @Entity
  public class Child{
      @Id
      @GeneratedValue
      private Integer id;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "parentId")
      private Parent parent;
  
      private String name;
      // constructor getter and setter
  }
  </code></pre>
  
  <p>Command Name class</p>
  
  <pre><code>public class CommandName {
      private Parent parent;
      private List&lt;Child&gt; existingChildren = new ArrayList&lt;Child&gt;();
      private List&lt;Child&gt; newChildren = new ArrayList&lt;Child&gt;();
  
      private static final int MAX_NUM_NEW_CHILDREN = 3;
  
      public CommandName(Parent parent) {
            this.parent = parent;
            this.existingChildren.addAll(parent.getChildren());
            for (int i = 0; i &lt; MAX_NUM_NEW_CHILDREN; ++i) {
                  newChildren.add(new Child());
            }
      } 
      // getters and setters
  }
  </code></pre>
  
  <p>Controller class</p>
  
  <pre><code>@Controller
  public class MyController {
      @Autowired
      private ParentRepository parentRepository;
  
      @RequestMapping(value = "/editParent/{parentID}", method = RequestMethod.GET)
      public String editParent(ModelMap model, @PathVariable("parentID") Integer parentID) {
          CommandName cn = new CommandName(parentRepository.findOne(parentID));
          model.addAttribute("cn", cn);
          return "edit";
      }
  
      @RequestMapping(value = "/saveParent", method = RequestMethod.POST)
      public String saveParent(CommandName cn, BindingResult bindingResult) {
          Parent parent = cn.Parent();
          List existChildren = cn.getExistingChildren();
          for (Child c : existChildren) {
              c.setParent(parent);
          }
          List newChildren = cn.getNewChildren();
          for (Child c: newChildren) {
              c.setParent(parent);
          }
          parent.getChildren().addAll(existChildren);
          parent.getChildren().addAll(newChildren);
          parentRepository.save(parent);
          return "redirect:/view";
      }
  }
  </code></pre>
  
  <p>form in the edit.jsp</p>
  
  <pre><code>&lt;f:form role="form" action="/saveParent" commandName="cn"&gt;
  
  &lt;f:hidden path="parent.id"/&gt;
  &lt;f:label path="parent.name"&gt;Parent Name&lt;/f:label&gt;
  
  &lt;f:input type="text" path="parent.name"/&gt;
  
  &lt;label&gt;Children&lt;/label&gt;
  &lt;c:forEach items="${cn.existingChildren}" var="child" varStatus="status"&gt;
      &lt;f:hidden path="child.id"/&gt;
      &lt;f:input type="text" path="existingChildren[${status.index}].name"/&gt;
  &lt;/c:forEach&gt;
  
  &lt;c:forEach items="${cn.newChildren}" var="child" varStatus="status"&gt;
      &lt;f:input type="text" path="newChildren[${status.index}].name"/&gt;
  &lt;/c:forEach&gt;
  
  &lt;button type="submit"&gt;Submit&lt;/button&gt;
  &lt;/f:form&gt;
  </code></pre>
  
  <p>These set of code worked: the parent and the existing children are updated. The new children are created. But in the saveParent() method, I have to do the following to relink everything. Is there any better way to approach this (a better commandName class, a better jsp form?) Thanks a lot!</p>
  
  <pre><code>List existChildren = cn.getExistingChildren();
  for (Child c : existChildren) {
      c.setParent(parent);
  }
  </code></pre>