简体   繁体   中英

org.hibernate.ObjectDeletedException: deleted instance passed to merge

This is my remove method:

public void removeIletisimAdresi(Integer index){
              getUser().getIletisimBilgileri().remove(getUser().getIletisimBilgileri().get(index))
        }

this is my parent relation

...

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity= IletisimBilgileri.class,  mappedBy = "user")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<IletisimBilgileri> iletisimBilgileri = new ArrayList<IletisimBilgileri(0);

...

this is my child :

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Kullanici")
private Users user ;

so if i call remove method and update parent like entityManager.merge(user),it throws an exception like this:

Caused by: org.hibernate.ObjectDeletedException: deleted instance passed to merge: [com.medikalborsasi.model.IletisimBilgileri#]

..

can you explain and help me how can i solve this problem?

i think issue is in the your code only,

getUser().getIletisimBilgileri().remove(getUser().getIletisimBilgileri().get(index))

Your mapping must be in such a manner that if you remove one entity from hibernate leads to deletion of same entity from the others dependent entity. eg :

Class A
{
     /* List of B entity(child) */
     List<B> listOfB;

}

Class B
{
     /* reference of A(parent) */
     A a;
}

so if you call

hibernateSession.delete(b);

should delete the reference of b from the list that present in A. You don't have to explicitly remove reference of B from the List in A. So as per your method you are doing

getA().getListOfB.remove(getA().getListOfB().get(someIndex));

I hope this small example will clear your issue.

Before an entity removed, this entity must be manage state. We can use some of the function merge , find , etc. Note : After EntityManager is closed, all the entities of its will be detached.

public void remove(Person p) {
    Perosn p2 = em.merge(p);
    em.remove(p2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM