The case:
there is Many-to-One relation.
In parent I'm iterating through set of children updating child property which is part of unique constraint (unique key in table).
The problem:
saving parent in session Hibernate throws exception
java.sql.SQLException: ERROR: Cannot insert a duplicate key into unique index
The problem is that Hibernate should execute SQL's in "right" order.
1. Possible fix No1:
Of course I could call saveOrUpdate() on every child to ensure Hibernate will generate SQL's in right order. But I want parent class, where this logic is, not to know anything about Hibernate and being persisted in third place like parent.saveOrUpdate().
2. Possible fix No2:
Unreference children set from parent, so that Hibernate first DELETE children. That update children set, set children id's to null and assing set back to parent. But it doesn't work, perhaps because Hibernate first processes inserts and updates and only than DELETES. So that error on update is thrown first.
May be anyone knows how to resolve this problem gracefully?
Thanx
|