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 Bug
    • Status: Open
    • Priority: Critical Critical
    • Resolution: Unresolved
    • Affects Version/s: 3.3.1, 3.6.0
    • Fix Version/s: None
    • 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()))
      1. CascadeMergeTest.zip
        210 kB
        Seth Martin
      2. CascadeMergeTest.zip
        6 kB
        Seth Martin

        Activity

        Hide
        Mihalcea Vlad added a comment -

        Check my HHH-3332-again.zip attachement to see it still replicates on 4.x version too.

        Or:

        https://github.com/vladmihalcea/vladmihalcea.wordpress.com/blob/master/hibernate-facts/src/test/java/com/vladmihalcea/HibernateBagDuplicateTest.java

        Starting at line 75:

        //assertEquals(2, parent.getChildren().size());
        if (parent.getChildren().size() == 4)

        { LOG.error("Duplicates rows generated!"); }
        Show
        Mihalcea Vlad added a comment - Check my HHH-3332 -again.zip attachement to see it still replicates on 4.x version too. Or: https://github.com/vladmihalcea/vladmihalcea.wordpress.com/blob/master/hibernate-facts/src/test/java/com/vladmihalcea/HibernateBagDuplicateTest.java Starting at line 75: //assertEquals(2, parent.getChildren().size()); if (parent.getChildren().size() == 4) { LOG.error("Duplicates rows generated!"); }
        Hide
        Seth Martin added a comment -

        @Mihalcea Thank you for uploading the test case for hibernate 4.

        Show
        Seth Martin added a comment - @Mihalcea Thank you for uploading the test case for hibernate 4.
        Hide
        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
        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
        Mihalcea Vlad added a comment -
        Show
        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
        Rowland Mynhardt added a comment -

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

        Show
        Rowland Mynhardt added a comment - Confirmed on Version 4.3.4.Final using HHH-3332 Again code attached

          People

          • Votes:
            14 Vote for this issue
            Watchers:
            16 Start watching this issue

            Dates

            • Created:
              Updated:

              Development