A different object with the same identifier value was already associated with the session cascade merge

977 Views Asked by At

I read about the error in title but I can't find suitable solution for my case, I have the following relation :

@Entity
public class Parent {


    @OneToMany(fetch = FetchType.EAGER,mappedBy = "parent",cascade = CascadeType.MERGE)
    @Fetch(FetchMode.SUBSELECT)
    private List<Child> child;
}


@Entity
public class Child {

    @EmbeddedId
    @JsonIgnore
    private CompositePK compositePK;

    @MapsId("compositeId")
    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    @JsonIgnore
    private Parent parent;

    @MapsId("AId")
    @JoinColumn(name = "A_ID")
    @OneToOne
    private A a;

}

I don't have a transnational over my method and no open sessions , the following line throws error when trying to merge parent and merge its Child entites: parent= parentRepository.save(parent);

A different object with the same identifier value was already associated with the session : [....CompositePK@22bf8646]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : .......CompositeMedicinePK@22bf8646]"

any explanation ?

1

There are 1 best solutions below

0
On

It looks like this issue was fixed in hibernate version 5.0.10, 5.1.1, and 5.2.0. More information here: https://hibernate.atlassian.net/browse/HHH-10476