Uploaded image for project: 'Hibernate ORM'
  1. HHH-9864

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

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects versions: 4.3.10, 5.0.0.CR1, 4.2.21, 5.0.11
    • Fix versions: 5.2.4, 5.1.4
    • Components: hibernate-core
    • 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
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      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.

        Attachments

          Issue links

            Activity

              People

              • Votes:
                3 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - Not Specified
                  Not Specified
                  Logged:
                  Time Spent - 3h 26m
                  3h 26m