简体   繁体   中英

hibernate issues with composite primary key

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.

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.

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