Reputation: 28
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?
Upvotes: 0
Views: 18496
Reputation: 385
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.
Upvotes: 2
Reputation: 9935
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);
}
Upvotes: -1