Hibernate inserts duplicates into a @OneToMany collection

前端 未结 5 1641
野性不改
野性不改 2020-12-01 20:52

I have a question concerning Hibernate 3.6.7 and JPA 2.0.

Consider following entities (some getters and setters are omitted for brevity):

@Entity
pub         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 21:41

    It's a bug in Hibernate. Surprisingly, it's not reported yet, feel free to report it.

    Operations against non-initialized lazy collections are queued in order to execute them after collection is initialized, and Hibernate doesn't handle the situation when these operations conflict with the data from the database. Usually it's not a problem, because this queue is cleared upon flush(), and possible conflicting changes are propagated to the database upon flush() as well. However, some changes (such as persisting of entities with ids generated by generator of type IDENTITY, I guess, it's your case) are propagated to the database without the full flush(), and in these cases conflicts are possible.

    As a workaround you can flush() the session after persisting the child:

    em.persist(child); 
    em.flush();
    

提交回复
热议问题