I actually never quite understood this behavior in hibernate. I am using a @OneToMany relationship in a Entity called \'Parent\', which is annotated like this:
Hibernate doesn't know about, nor respect, all database constraints (e.g. MySQL unique constraints). It's a known issue they don't plan on addressing anytime soon.
Hibernate has a defined order for the way operations occur during a flush.
Entity deletions will always happen after inserts. The only answers I know about are to remove the constraint or add the additional flush.
EDIT: By the way, the reason for the defined order is that this is the only way to guarantee foreign key constraints (one of the constraints they DO care about) aren't violated, even if the user does something out of order.
For the sake of future readers, one way to resolve this issue is to use deferred constraints. PostgreSQL and Oracle support them, maybe other RDBMS' too. Hibernate will issue all statements within a transaction, and deferral will ensure that constraints are enforced upon transaction commit only. In PostgreSQL, for example:
ALTER TABLE company
ADD CONSTRAINT name_unique UNIQUE (name) DEFERRABLE INITIALLY DEFERRED;
It is not ideal, but it is simple and effective.