title: not saving child entity
body: <p>In a spring mvc project using hibernate and jpa, I am have a <code>person</code> entity, and a <code>role</code> entity.  Each <code>person</code> can have many <code>roles</code>, but each <code>role</code> can only have one <code>person</code>.  How can I save a new <code>role</code> for an existing <code>person</code>?  </p>
  
  <p>I have read many postings on this.  If I try to save the new <code>role</code>, I get an error about not being able to save a detached entity.  But if I try to save the existing <code>person</code>, the role does not get saved.  I read <a href="https://stackoverflow.com/questions/13370221/jpa-hibernate-detached-entity-passed-to-persist/13370397?noredirect=1#comment42271783_13370397">this posting</a> suggesting that the hibernate annotations should be moved to the getters, but when I do that, I get a hibernate mapping error referencing an unknown property.  And <a href="https://stackoverflow.com/questions/16871012/hibernate-one-to-many-mapping-error-mappedby-reference-an-unknown-target-entit">this posting</a> says the resolution of the new error is to move the annotations back to the getters, without mentioning that results in the detached entity error.  The various stack overflow questions thus seem to point in a circle.  <strong>So how can I fix my code to actually save the new child entity?</strong>  </p>
  
  <p>Here is the Person:  </p>
  
  <pre><code>@Entity(name = "RimPerson")
  @Table(name = "rim_person")
  public class RimPerson {
  
      @Id
      @Column(name="hppid")
      @GenericGenerator(name = "generator", strategy = "native")
      @GeneratedValue(generator = "generator")
      private Long hppid;
  
      @OneToMany(targetEntity = RimRole.class, cascade = {CascadeType.ALL
      }, fetch = FetchType.EAGER)
      @JoinColumn(name = "player_rimperson_HJID")
      private List&lt;RimRole&gt; playedRoles;
  
      //other properties and getters and setters
  }
  </code></pre>
  
  <p>Role is:  </p>
  
  <pre><code>@Entity(name = "RimRole")
  @Table(name = "rim_role")
  public class RimRole {
  
      @Id
      @Column(name="hppid")
      @GenericGenerator(name = "generator", strategy = "native")
      @GeneratedValue(generator = "generator")
      private Long hppid;
  
      @ManyToOne(targetEntity = RimPerson.class, cascade = {CascadeType.ALL
      }, fetch = FetchType.LAZY)
      @JoinColumn(name = "player_rimperson_HJID", nullable=true)
      private RimPerson player;  
  
      //other stuff and getters and setters
  
  }  
  </code></pre>
  
  <p>Here is the JPQL in the repository layer:  </p>
  
  <pre><code>@Override
  public void saveRIMPerson(RimPerson myperson) throws DataAccessException{
      if (myperson.getHppid() == null) {this.em.persist(myperson);}
      else {this.em.merge(myperson);}
  }
  </code></pre>
  
  <p>The complete code for the relevant classes can be found <a href="https://jumpshare.com/b/XYRRAn5kdqzvr8g2zO7e" rel="nofollow noreferrer">at this link</a>.</p>