Reputation: 60
I am facing some issues with Hibernate. We need to save an object with its children. Each child has a composite primary key. One property of the key will be inserted by a trigger. Another property will be set from the program before calling saveOrUpdate(Object)
.
But we are not able to save the object. Hibernate throws the exception Same identifier is already exists in the session
.
I have tried session.clear()
, but I get the same exception. When I tried session.merge()
, only the last child was saved, others were ignored.
Upvotes: 0
Views: 679
Reputation: 27880
If you aren't going to need in the same Hibernate Session the objects once saved, you could detach them right after saving with Session.evict()
:
// children is the collection of detached children, ready to save
for (Child child : children){
session.save(child);
session.evict(child);
}
Alternatively, this entry in the Hibernate Forums might be helpful: Before Insert Trigger and ID generator. There's an implementation of an AbstractPostInsertGenerator
you can integrate to suit your needs.
Upvotes: 1