简体   繁体   English

复合主键的休眠问题

[英]hibernate issues with composite primary key

I am facing some issues with Hibernate. 我在使用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) . 在调用saveOrUpdate(Object)之前,将从程序中设置另一个属性。

But we are not able to save the object. 但是我们无法保存该对象。 Hibernate throws the exception Same identifier is already exists in the session . Hibernate引发异常Same identifier is already exists in the session

I have tried session.clear() , but I get the same exception. 我尝试了session.clear() ,但是遇到了同样的异常。 When I tried session.merge() , only the last child was saved, others were ignored. 当我尝试session.merge() ,只有最后一个孩子被保存,其他孩子被忽略。

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() : 如果不再需要在同一Hibernate Session中保存对象,则可以在使用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 . 另外,在Hibernate论坛中的该条目可能会有所帮助: 在Insert Trigger和ID generator之前 There's an implementation of an AbstractPostInsertGenerator you can integrate to suit your needs. 有一个AbstractPostInsertGenerator的实现,可以将其集成以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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