title: Hibernate - "detached entity passed to persist" error for already persisted child
body: <p>I have an entity, which is already persisted and want to add it to a newly generated parent entity (not yet persisted). If i try to persisted the parent then, i get the error "detached entity passed to persist: model.Child". I think i have to somehow call a "entityManager.merge()" for the child instead of a "entityManager.persist()". But i do not explicitly call the persist. This is handled by the "cascade = CascadeType.ALL" annotation. Can i tell hibernate somehow to do a merge here, if the entity already exists?</p>
  
  <p>By the way: If i first persist the parent, then add the child and then persist the parent again -> It works (But makes my application logic much more complicated).</p>
  
  <p>Here my code:</p>
  
  <pre><code>public class App 
  {
      @Test
      public void test()
      {
  
          // I have a child object (in the real app 
          //this is a user object and already persisted
          Child child = new Child();
          HibernateHelper.persist(child);
  
          Parent parent = new Parent();
  
          parent.addChildren(child);
          // throws the exception "detached entity passed to persist: model.Child"
          HibernateHelper.persist(parent);
  
          Parent newParent = HibernateHelper.find(Parent.class, parent.getId());
          assertEquals(1,  newParent.getChildren().size());
  
      }
  }
  </code></pre>
  
  <p>My "child" entity:</p>
  
  <pre><code>@Entity
  @Table(name = "child")
  public class Child {
  
      public Child(){}
  
      private Long id;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      private Parent parent;
  
      @ManyToOne
      @JoinColumn
      public Parent getParent() {
          return parent;
      }
  
      public void setParent(Parent parent) {
          this.parent = parent;
      }
  
  
  
  }
  </code></pre>
  
  <p>My "parent" entity:</p>
  
  <pre><code>@Entity
  @Table(name="parent")
  public class Parent {
  
      public Parent(){}
  
      private Long id;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      private Set&lt;Child&gt; children = new HashSet&lt;Child&gt;();
  
      @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
      public Set&lt;Child&gt; getChildren() {
          return children;
      }
      public void setChildren(Set&lt;Child&gt; children) {
          this.children = children;
      }
      public void addChildren(Child child){
          children.add(child);
          child.setParent(this);
      }
  }
  </code></pre>
  
  <p>The persist helper method (looks the same for the child)</p>
  
  <pre><code>public static void persist(Parent entity){
  
      EntityManager entityManager = null;
      try {
          entityManager = beginTransaction();
  
          if(entity.getId()!=null){
              entityManager.merge(entity);
          }else{
              entityManager.persist(entity);
          }
          entityManager.getTransaction().commit();
      } catch (Exception e) {
          System.out.println(e);
          return;
      }finally{
          if(entityManager != null)
              entityManager.close();
      }
  }
  </code></pre>