We're updating the issue view to help you get more done. 

foreign key violation with order_inserts=true and cascading persist

Description

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:

  1. EntityInsertAction[test.orderproblem.MarketResult#1]

  2. EntityInsertAction[test.orderproblem.MarketBidGroup#2]

  3. EntityInsertAction[test.orderproblem.MarketBid#2]

  4. EntityInsertAction[test.orderproblem.MarketResult#2]

If "order_inserts" is "true", the inserts are wrongly sorted into this order:

  1. EntityInsertAction[test.orderproblem.MarketBid#2]

  2. EntityInsertAction[test.orderproblem.MarketResult#1]

  3. EntityInsertAction[test.orderproblem.MarketResult#2]

  4. 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 "true", resulting in a ConstraintViolationException.

Environment

None

Status

Assignee

Chris Cranford

Reporter

Christian Ciach

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.0.12

Components

Affects versions

5.2.10
5.1.7

Priority

Major