OpenJPA
  1. OpenJPA
  2. OPENJPA-1966

Entity graphs containing bi-directional references may not merge correctly if an entity does not contain a state manager

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.0.4, 1.2.2, 2.0.1, 2.1.0
    • Fix Version/s: None
    • Component/s: kernel
    • Labels:
      None

      Description

      An entity graph (with cascade) containing bi-directional relationships or circular references may fail to merge properly if one or more of the entities in the graph do not have a state manager - either an active or detached state manager. The result is that one or more of the merged entities may end up with a reference to a detached object. This may not be immediately evident and could cause problems in subsequent operations. It can even affect the ordering of SQL statements since foreign keys may not be properly associated with the detached entities.

      For example: A <- OneToOne -> B
      em.clear()
      Detached graph: B <--> A
      A: Is detached, has DetachedStateManager
      B: Is detached, No state manager, only detached state field. (ie. @DetachedState Object state)
      Merge: B
      After merge: B' --> A' --> B (detached)

      If B contains a state manager the result of the merge is correct: B' <--> A'

      The source of the problem is that the "no state manager" attachment path creates a new PC + state manager and assigns this state manager to the detached instance so that it can be used to attach all its fields. During a cascade, this detached entity (with borrowed state manager) looks persistent, so it can get attached into the resulting merge graph.

      1. OPENJPA-1966.patch
        12 kB
        Jeremy Bauer
      2. OPENJPA-1966.patch
        30 kB
        Jeremy Bauer

        Activity

        Hide
        Vermeulen added a comment - - edited

        We are experiencing exactly this problem with all our cascading relations of the form A <- OneToMany -> B. We prefer using detached entities without a state manager because we have a client-server system where the client may insert/update entities with a remote method call to the server.
        What we get on merging A is A' -> B'_i, with B'_i -> A.
        An easy workaround for us is to manually set the reference from each B'_i to A' after detaching.
        However it would be very nice if this bug is fixed because this is easy to forget when working with a new entity.

        Show
        Vermeulen added a comment - - edited We are experiencing exactly this problem with all our cascading relations of the form A <- OneToMany -> B. We prefer using detached entities without a state manager because we have a client-server system where the client may insert/update entities with a remote method call to the server. What we get on merging A is A' -> B'_i, with B'_i -> A. An easy workaround for us is to manually set the reference from each B'_i to A' after detaching. However it would be very nice if this bug is fixed because this is easy to forget when working with a new entity.
        Hide
        Jeremy Bauer added a comment -

        Updated patch with additional test variations.

        Show
        Jeremy Bauer added a comment - Updated patch with additional test variations.
        Hide
        Jeremy Bauer added a comment -

        Attaching a patch and simple jUnit. The patch queries the attach cache before the using the broker to determine what to use for a pc reference during attachment. More jUnits will be provided.

        Show
        Jeremy Bauer added a comment - Attaching a patch and simple jUnit. The patch queries the attach cache before the using the broker to determine what to use for a pc reference during attachment. More jUnits will be provided.

          People

          • Assignee:
            Jeremy Bauer
            Reporter:
            Jeremy Bauer
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development