jan
jan

Reputation: 4053

Hibernate - "detached entity passed to persist" error for already persisted child

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?

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).

Here my 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());

    }
}

My "child" entity:

@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;
    }



}

My "parent" entity:

@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<Child> children = new HashSet<Child>();

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    public Set<Child> getChildren() {
        return children;
    }
    public void setChildren(Set<Child> children) {
        this.children = children;
    }
    public void addChildren(Child child){
        children.add(child);
        child.setParent(this);
    }
}

The persist helper method (looks the same for the child)

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();
    }
}

Upvotes: 1

Views: 3984

Answers (1)

German
German

Reputation: 3606

An option would be to always use EntityManager.merge. In case a new entity is passed it is persisted, in case a detached entity is passed it is merged into the current persistence context.

Upvotes: 1

Related Questions