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

The method "merge" from the EntityManager causes a duplicated "insert" of a child entity.

    Details

    • Type: Bug
    • Status: Open
    • Priority: Critical
    • Resolution: Unresolved
    • Affects Version/s: 3.3.1, 3.6.0, 4.3.11
    • Fix Version/s: triage
    • Labels:
      None
    • Environment:
      Hibernate-Core 3.6.0, Hibernate EntityManager 3.6.0, Derby, Spring 3.0.5 (Configuration), JUnit 4.5
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true

      Description

      The method "merge" from the EntityManager causes a duplicated "insert" of a child entity.
      Related to HHH-3332

      Observed Behavior:

      When you have an Entity A with a OneToMany(fetch=LAZY, cascade=ALL or (MERGE and PERSIST)) relationship to another Entity B and attempt to add an new B instance to A hibernate will generate a duplicate insert command upon commit.

      I have witnessed this problem occurring when this procedure is followed:

      1. retrieve an instance of A from persistence (A a = em.find(A.class, id))
      2. add a new B to A (a.getBs.add(new B()))
      3. merge instance 'a' back into persistence (em.merge(a))

      Upon transaction commit hibernate will generate an additional "insert" statement and insert the new instance of b into the data source twice.

      Expected behavior:

      I would expect hibernate to only perform a single insertion when 'a' is merged back to persistence and the B's are created through the cascade.

      Notes (Discoveries):

      This problem can be worked around by:

      • Changing the OneToMany relationship from a List to a Set
      • Only using either Cascade MERGE or PERSIST not both of ALL
      • Add a new B to A.getBs by adding to the '0' index (a.getBs.add(0,new B()))

        Gliffy Diagrams

          Attachments

          1. CascadeMergeTest.zip
            210 kB
          2. CascadeMergeTest.zip
            6 kB
          3. HHH-3332-again.zip
            19 kB
          4. hhh-5855-master.zip
            12 kB

            Activity

            Hide
            specialseth Seth Martin added a comment -

            Also, if the bug is still present in version 4 my test cases should still fail. i will dbl check them when i have some extra time.

            Show
            specialseth Seth Martin added a comment - Also, if the bug is still present in version 4 my test cases should still fail. i will dbl check them when i have some extra time.
            Hide
            mih_vlad Mihalcea Vlad added a comment -
            Show
            mih_vlad Mihalcea Vlad added a comment - For the past 5 years this was my workaround: http://vladmihalcea.com/2013/10/16/hibernate-facts-favoring-sets-vs-bags/
            Hide
            roloreaper Rowland Mynhardt added a comment -

            Confirmed on Version 4.3.4.Final using HHH-3332 Again code attached

            Show
            roloreaper Rowland Mynhardt added a comment - Confirmed on Version 4.3.4.Final using HHH-3332 Again code attached
            Hide
            mpostelnicu Mihai Postelnicu added a comment -

            Confirmed on Hibernate 4.3.11.Final.

            In the hope that someone will address this, i have created a dedicated Github project for the test case https://github.com/mpostelnicu/hhh-5855
            I have also attached the test case here (hhh-5855-master.zip)

            Thanks much!!
            Mihai

            Show
            mpostelnicu Mihai Postelnicu added a comment - Confirmed on Hibernate 4.3.11.Final. In the hope that someone will address this, i have created a dedicated Github project for the test case https://github.com/mpostelnicu/hhh-5855 I have also attached the test case here (hhh-5855-master.zip) Thanks much!! Mihai
            Hide
            mpostelnicu Mihai Postelnicu added a comment -

            updated test case to also do child.setParent besides parent.add(child).

            https://github.com/mpostelnicu/hhh-5855/commit/f94b6d906c7c7278605f09de46c9429c639ad16a

            There is, however, no difference in terms of the end result, the bug is still there (it saves 4 children instead of just 2).

            Show
            mpostelnicu Mihai Postelnicu added a comment - updated test case to also do child.setParent besides parent.add(child). https://github.com/mpostelnicu/hhh-5855/commit/f94b6d906c7c7278605f09de46c9429c639ad16a There is, however, no difference in terms of the end result, the bug is still there (it saves 4 children instead of just 2).

              People

              • Votes:
                25 Vote for this issue
                Watchers:
                24 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Development