OpenJPA
  1. OpenJPA
  2. OPENJPA-231

Incorrect handling of cascading bidirectional collections during merge/attach

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 0.9.7
    • Fix Version/s: 1.1.0
    • Component/s: kernel
    • Labels:
      None
    • Environment:
      platform-independent

      Description

      As posted on open-jpa-dev mailing list:


      I'm having a problem merging an entity, here's the simplified structure:

      Class A { @OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
      Set<B> b_set;
      ...
      }

      Class B {
      @ManyToOne
      A parent;

      @OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
      Set<C> c_set;
      ...
      }

      Class C {
      @ManyToOne
      B parent;
      ...
      }

      New instances of A,B,C are persisted correctly. However when I retrieve A, add some entries to A.b_set (with some of the new B entries have attached C's), em.merge(A) fails:

      • If A is detached:

      org.apache.openjpa.persistence.ArgumentException: Encountered new object "B@..." in persistent field "C.parent" of managed object "C@..." during attach. However, this field does not allow cascade attach. You cannot attach a reference to a new object without cascading.

      • If A is not detached:
        C instances are inserted before B instances, resulting in a foreign key violation.

      Setting openjpa.jdbc.SchemaFactory to 'native(ForeignKeys=true)' does not seem to have an effect.


      1. openjpa-jira-231-255-new.patch
        19 kB
        Xiaoqin Feng
      2. openjpa-231-1.1.x.patch
        17 kB
        Xiaoqin Feng
      3. openjpa-231.patch
        0.8 kB
        Markus Fuchs
      4. jira-test-case.zip
        5 kB
        Gokhan Ergul

        Issue Links

          Activity

          Hide
          Xiaoqin Feng added a comment -

          This fix is for openjpa1.1.x branch. Test case is also included in this patch.

          Show
          Xiaoqin Feng added a comment - This fix is for openjpa1.1.x branch. Test case is also included in this patch.
          Hide
          Xiaoqin Feng added a comment -

          I added TestNoCascadeOneToManyMerge.java based on the test case provided in this bug.
          The attached patch includes both tests and fixes for open-231 and open-255.

          Show
          Xiaoqin Feng added a comment - I added TestNoCascadeOneToManyMerge.java based on the test case provided in this bug. The attached patch includes both tests and fixes for open-231 and open-255.
          Hide
          Gokhan Ergul added a comment -

          That'll do too, but still we'd need the isManaged() check to fix OPENJPA-255 as well, see https://issues.apache.org/jira/secure/attachment/12359302/openjpa-jira-255.patch.

          Show
          Gokhan Ergul added a comment - That'll do too, but still we'd need the isManaged() check to fix OPENJPA-255 as well, see https://issues.apache.org/jira/secure/attachment/12359302/openjpa-jira-255.patch .
          Hide
          Markus Fuchs added a comment -

          I propose a slight change to Gokhan's patch, please see openjpa-231.patch.

          Show
          Markus Fuchs added a comment - I propose a slight change to Gokhan's patch, please see openjpa-231.patch.
          Hide
          Gokhan Ergul added a comment -

          Kevin,

          Just reran tests against latest svn with CascadeType.ALL on ManyToOne annotations, detached testcase fails with duplicate key exceptions, attached testcase passes --openjpa-friendly version of testcases are at https://issues.apache.org/jira/secure/attachment/12360321/merge-testcases.patch in case you want to run them yourself. Also see https://issues.apache.org/jira/browse/OPENJPA-255 for a similar issue with detached objects, it's basically a different manifestation of the same problem.

          Show
          Gokhan Ergul added a comment - Kevin, Just reran tests against latest svn with CascadeType.ALL on ManyToOne annotations, detached testcase fails with duplicate key exceptions, attached testcase passes --openjpa-friendly version of testcases are at https://issues.apache.org/jira/secure/attachment/12360321/merge-testcases.patch in case you want to run them yourself. Also see https://issues.apache.org/jira/browse/OPENJPA-255 for a similar issue with detached objects, it's basically a different manifestation of the same problem.
          Hide
          Kevin Sutter added a comment -

          Just curious... If you put the CascadeType.ALL on the ManyToOne annotations, does your merge processing work okay? That should not be required, but I am wondering whether we are dealing with similar problems. Thanks.

          Show
          Kevin Sutter added a comment - Just curious... If you put the CascadeType.ALL on the ManyToOne annotations, does your merge processing work okay? That should not be required, but I am wondering whether we are dealing with similar problems. Thanks.
          Hide
          Gokhan Ergul added a comment -

          Further testing reveals that the patch I submitted solves only detached object merge case. Persisting and later merging in a single EntityManager session produces unreliable results, sometimes insert statements of C precede insert statements of B resulting in a FK violation, other times the statements are executed in the correct order --I have no idea as to why the order would change in different test runs with identical configuration.

          Show
          Gokhan Ergul added a comment - Further testing reveals that the patch I submitted solves only detached object merge case. Persisting and later merging in a single EntityManager session produces unreliable results, sometimes insert statements of C precede insert statements of B resulting in a FK violation, other times the statements are executed in the correct order --I have no idea as to why the order would change in different test runs with identical configuration.
          Hide
          Gokhan Ergul added a comment -

          Note that the patch is developed against a nightly-snapshot of 0.9.7.

          Bug is reproducible both with released 0.9.7 and latest 0.9.8-snapshot.

          Show
          Gokhan Ergul added a comment - Note that the patch is developed against a nightly-snapshot of 0.9.7. Bug is reproducible both with released 0.9.7 and latest 0.9.8-snapshot.
          Hide
          Gokhan Ergul added a comment -

          Attaching test-case to reproduce the issue and suggested patch.

          Show
          Gokhan Ergul added a comment - Attaching test-case to reproduce the issue and suggested patch.

            People

            • Assignee:
              Unassigned
              Reporter:
              Gokhan Ergul
            • Votes:
              2 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development