| I came across this bug when updating a project for a customer. The code uses merge() to persist new entities. It then calls flush() to get the generated PK and tries to insert different entities, I have reproduced the problem with a simple parent-child relationship. When merging a parent-child, one-to-many relationship with cascade-all and orphan-removal true, the following HibernateException is thrown: org.springframework.orm.hibernate5.HibernateSystemException: Don't change the reference to a collection with delete-orphan enabled A full project is attached but here is the code which causes the problem: {{Parent p = new Parent(); p.addChild(new Child()); p.addChild(new Child()); getHibernateTemplate().merge(p); getHibernateTemplate().flush();}} The exception is thrown when the transaction is committed when the method returns but I believe it will occur if flush() is called twice at the time the second one is called. If flush() is not called, the transaction completes successfully. |