title: Detached entity passed to persist when save the child data
body: <p>I'm getting this error when submitting the form:</p>
  
  <blockquote>
    <p>org.hibernate.PersistentObjectException: detached entity passed to persist: com.project.pmet.model.Account; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.project.pmet.model.Account</p>
  </blockquote>
  
  <p>Here are my entities:</p>
  
  <p><strong>Account:</strong> </p>
  
  <pre><code>@Entity
  @DynamicInsert
  @DynamicUpdate
  public class Account {
  
      @Id
      @GeneratedValue
      private Integer id;
  
      @Column(nullable = false)
      private String login;
  
      @Column(nullable = false)
      private String password;
  
      @Column(nullable = false)
      private String email;
  
      @ManyToOne
      @JoinColumn(name = "team_id")
      private Team team;
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
      private List&lt;Team&gt; ownedTeams;
  
      ...
  </code></pre>
  
  <p><strong>Team:</strong></p>
  
  <pre><code>@Entity
  @DynamicInsert
  @DynamicUpdate
  public class Team {
  
      @Id
      @GeneratedValue
      private Integer id;
  
      @Column(nullable = false)
      private String name;
  
      @ManyToOne
      @JoinColumn(name = "owner_id", nullable = false)
      private Account owner;
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "team")
      private List&lt;Account&gt; members;
  
      ...
  </code></pre>
  
  <p>Here's a part of the Controller:</p>
  
  <pre><code>    @ModelAttribute("team")
      public Team createTeamObject() {
          return new Team();
      }
  
      @RequestMapping(value = "/teams/create-team", method = RequestMethod.GET)
      public String getCreateTeam(@ModelAttribute("team") Team team, Principal principal) {
          logger.info("Welcome to the create team page!");
  
          Account owner = accountService.findOneByLogin(principal.getName());
          team.setOwner(owner);
          team.setMembers(new AutoPopulatingList&lt;Account&gt;(Account.class));
  
          return "teams";
      }
  
      @RequestMapping(value = "/teams/create-team", method = RequestMethod.POST)
      public String postCreateTeam(@ModelAttribute("team") Team team) {
          logger.info("Team created!");
  
          teamService.save(team);
  
          return "redirect:/teams.html";
      }
  </code></pre>
  
  <p>And the form:</p>
  
  <pre><code>&lt;form:form commandName="team" id="teamForm"&gt;
        &lt;div class="form-group"&gt;
            &lt;label&gt;Name&lt;/label&gt;
            &lt;form:input path="name" cssClass="form-control" /&gt;
        &lt;/div&gt;
        &lt;div class="form-group" id="row-template"&gt;
            &lt;label&gt;Members&lt;/label&gt;
            &lt;form:select path="members[0].id" cssClass="form-control" data-live-search="true" &gt;
               &lt;form:options items="${accounts}" itemValue="id" /&gt;
            &lt;/form:select&gt;
            ...
        &lt;/div&gt;
     &lt;form:hidden path="owner.id" /&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>What am I doing wrong?</p>