Inserts statements are executed in wrong order when hibernate.order_inserts=true is set in the configuration, leading to a foreign key violation.
The problem occurs when both inserting a new entity that references an existing entity and inserting new entity that references another new entity at the same time.
The problem is hard to explain in words, so please see the attached simple test case that is broken since Hibernate ORM 5.1.
If "order_inserts" is "false", the second transaction inside the test case creates inserts in this order:
# EntityInsertAction \ [test.orderproblem.MarketResult#1] # EntityInsertAction \ [test.orderproblem.MarketBidGroup#2] # EntityInsertAction \ [test.orderproblem.MarketBid#2] # EntityInsertAction \ [test.orderproblem.MarketResult#2]
If "order_inserts" is "true", the inserts are wrongly sorted into this order:
# EntityInsertAction[test.orderproblem.MarketBid#2] # EntityInsertAction[test.orderproblem.MarketResult#1] # EntityInsertAction[test.orderproblem.MarketResult#2] # EntityInsertAction[test.orderproblem.MarketBidGroup#2]
Because MarketBid#2 references MarketBidGroup#2, the latter must be saved first, which is not the case with "order_inserts" set to "false", resulting in a ConstraintViolationException. |
|