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

foreign key violation with order_inserts=true and batches with mixed subclass entities

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 with a simple derived class with a one-to-many association.
We do huge batch imports and need the order_inserts-parameter for sufficient performance.

Error message with Derby:
java.sql.SQLIntegrityConstraintViolationException: INSERT on table 'ADDRESS' caused a violation of foreign key constraint 'FK_I58AVXHL07TR4
KQ5X1O6LKIUN' for key (-107374182399). The statement has been rolled back.

A Minimal test case is appended:
Address: simple class
Person class: has a one-to-many-association to Address
SpecialPerson: subclass of person, mapped on same table

If we save a Person and a SpecialPerson, each with one address, the error occurs. When order_inserts is set to false, the problem disappears.

Steps to run the test case:

  • unzip jpaorderproblem.zip

  • cd jpaorderproblem

  • mvn test

Without order_inserts=false, the sql insert order is:
PERSON (Person)
ADDRESS (from Person)
PERSON (SpecialPerson)
ADDRESS (from SpecialPerson)

Without order_inserts=true, the sql insert order is:
PERSON (Person)
ADDRESS (from Person)
ADDRESS (from SpecialPerson)
PERSON (SpecialPerson)

The third insert fails, since the containing SpecialPerson isn't inserted yet.
We think the problem is in the org.hibernate.engine.ActionQueue.InsertActionSorter class.
This class reorders the statements and adds the second address insert to the batch list created for the first address, so it is executed
before the SpecialPerson insert. The Sorter does not recognize that there are multiple classes mapped to the same table (Person, SpecialPerson).

https://hibernate.atlassian.net/browse/HHH-5454 is a similar issue.

Environment

Hibernate core 4.3.10 with Oracle 11gR2, Derby 10.11.1.1 , DB2 9.7
Reproduced with Hibernate core 5.0.0.CR1

Status

Assignee

Vlad Mihalcea

Reporter

Former user

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.0.11
5.0.0.CR1
4.3.10
4.2.21

Priority

Major