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: Resolved
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 3.3.1, 3.6.0, 4.3.11, 5.0.2
    • Fix Version/s: 5.0.8
    • 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()))

        Attachments

        1. CascadeMergeTest.zip
          210 kB
        2. CascadeMergeTest.zip
          6 kB
        3. HHH-3332-again.zip
          19 kB
        4. HHH-5855.zip
          26 kB
        5. hhh-5855-master.zip
          12 kB
        6. hhh-5855-master-hibernate-5.0.2.zip
          12 kB
        7. hhh-5855-master-hibernate-5.0.4.zip
          12 kB
        8. hhh-5855-master-hibernate-5.0.5.zip
          12 kB

          Activity

          Hide
          steve Steve Ebersole added a comment -

          Gail asked that we hold off applying this for 5.0.7 as we need to discuss. She is on holidays, so we will discuss when she gets back.

          Show
          steve Steve Ebersole added a comment - Gail asked that we hold off applying this for 5.0.7 as we need to discuss. She is on holidays, so we will discuss when she gets back.
          Hide
          mih_vlad Vlad Mihalcea added a comment -

          This PR fixes the issue. I'm looking forward to integrating this if Gail thinks there's nothing to be added to this commit.

          Show
          mih_vlad Vlad Mihalcea added a comment - This PR fixes the issue. I'm looking forward to integrating this if Gail thinks there's nothing to be added to this commit.
          Hide
          gbadner Gail Badner added a comment -

          Fixed in master and 5.0 branches.

          Show
          gbadner Gail Badner added a comment - Fixed in master and 5.0 branches.
          Hide
          mih_vlad Vlad Mihalcea added a comment -

          Thanks Gail for taking care of this issue.

          Show
          mih_vlad Vlad Mihalcea added a comment - Thanks Gail for taking care of this issue.
          Hide
          mpostelnicu Mihai Postelnicu added a comment -

          Great news !
          Thanks a lot for addressing this !

          Show
          mpostelnicu Mihai Postelnicu added a comment - Great news ! Thanks a lot for addressing this !

            People

            • Votes:
              28 Vote for this issue
              Watchers:
              29 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: