glassfish
  1. glassfish
  2. GLASSFISH-1021

OneToMany Cascade Persist Failure due to Non-entity

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 9.1pe
    • Fix Version/s: 9.1pe_dev
    • Component/s: entity-persistence
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: Sun

    • Issuezilla Id:
      1,021

      Description

      The entity Client has a uni-directional OneToMany
      relationship with the entity BaseInfo.
      The BaseInfo has non-entity subclass, Info and ClientInfo.

      @Entity
      public class Client implements java.io.Serializable {
      private List<BaseInfo> infolist;

      @OneToMany(cascade=CascadeType.PERSIST)
      public List<BaseInfo> getInfolist()

      { return infolist; }

      ......

      @Entity
      public class BaseInfo implements java.io.Serializable {
      ......

      public class TestClient {
      Client c = new Client();
      c.setInfolist(infoList);
      em.persist(c);
      ......

      When Client was persisted, the error is shown as
      ......
      INSERT INTO CLIENT (CLIENTID, CLIENTSSN) VALUES (?, ?)
      bind => [c1, 111-11-1111]
      ......
      INSERT INTO CLIENT_BASEINFO (infolist_ID, Client_CLIENTID)
      VALUES (?, ?)
      bind => [i1, c1]
      [TopLink Warning]: 2006.08.2511:09:23.141--UnitOfWork
      (30961619)-Thread(Thread[main,5,main])-Exception [TOPLINK-4002]
      (Oracle TopLink Essentials - 2006.8 (Build 060822)):
      oracle.toplink.essentials.exceptions.DatabaseException
      Internal Exception: org.apache.derby.client.am.SqlException:
      INSERT on table 'CLIENT_BASEINFO' caused a violation of
      foreign key constraint 'CLNTBSEINFONFLSTID' for key (i1).
      The statement has been rolled back.Error Code: -1
      Call:INSERT INTO CLIENT_BASEINFO (infolist_ID, Client_CLIENTID)
      VALUES (?, ?)
      bind => [i1, c1]
      Query:DataModifyQuery()
      .......

      CLIENT_BASEINFO can't be inserted before BASEINFO is inserted.
      The issue seems relating to the insert order (inverse
      alphabetical order with entity name?) and OneToMany relationship.
      It has no issue if BaseInfo is persisted alone
      without relationship.
      It has also no issue either if entity name is changed
      from BaseInfo to DaseInfo or if classes Info and ClientInfo
      are changed from non-entity to entity.

      The tests are done on AS9.1PE Build 14.

        Activity

        Hide
        mf125085 added a comment -

        I'm attaching a possible fix for issue 1021. The fix basically
        registers and removes new instances for non-entity classes in the
        Hashtable of the closest entity super class. During flush, the
        instances are written to the database from there.

        I think that we have to do this twist only for new instance, since
        TopLink will never return non-entity instances for any entity manager
        operation or queries.

        What do you think?

        Show
        mf125085 added a comment - I'm attaching a possible fix for issue 1021. The fix basically registers and removes new instances for non-entity classes in the Hashtable of the closest entity super class. During flush, the instances are written to the database from there. I think that we have to do this twist only for new instance, since TopLink will never return non-entity instances for any entity manager operation or queries. What do you think?
        Hide
        sherryshen added a comment -

        The error is due to non-entity, but not insert order since
        my previous comment with dasebase is incorrect with further tests.
        Hence, I changed summary before and change it again now.

        I try to describe my tests and observation below.
        Entity class A has a subclass B which is a non-entity.
        Entity class C has @OneToMany(cascade=CascadeType.PERSIST) with A.
        Test 1. A direct persist of B goes through without error, in which
        the provider seems ignoring non-entity subclass B and persisting A.
        Test 2. A direct persist of C results a cascade persist of B,
        which fails with a violation of foreign key constraint.

        What should I expect for direct and cascade persist for a non-entity?

        Show
        sherryshen added a comment - The error is due to non-entity, but not insert order since my previous comment with dasebase is incorrect with further tests. Hence, I changed summary before and change it again now. I try to describe my tests and observation below. Entity class A has a subclass B which is a non-entity. Entity class C has @OneToMany(cascade=CascadeType.PERSIST) with A. Test 1. A direct persist of B goes through without error, in which the provider seems ignoring non-entity subclass B and persisting A. Test 2. A direct persist of C results a cascade persist of B, which fails with a violation of foreign key constraint. What should I expect for direct and cascade persist for a non-entity?
        Hide
        mf125085 added a comment -

        Direct persisting a non-entity instance should fail. Please file a new issue if
        direct persisting non-entity instances is successful.

        A fix for cascade persisting has been checked in, please see
        http://fisheye5.cenqua.com/changelog/glassfish/?cs=MAIN:mf125085:20061016231436
        Also, see thread
        https://glassfish.dev.java.net/servlets/ReadMsg?listName=persistence&msgNo=1844
        for more discussion about cascading the persist operation to non-entities and thread
        https://glassfish.dev.java.net/servlets/ReadMsg?listName=persistence&msgNo=2026
        for the further code review process.

        Show
        mf125085 added a comment - Direct persisting a non-entity instance should fail. Please file a new issue if direct persisting non-entity instances is successful. A fix for cascade persisting has been checked in, please see http://fisheye5.cenqua.com/changelog/glassfish/?cs=MAIN:mf125085:20061016231436 Also, see thread https://glassfish.dev.java.net/servlets/ReadMsg?listName=persistence&msgNo=1844 for more discussion about cascading the persist operation to non-entities and thread https://glassfish.dev.java.net/servlets/ReadMsg?listName=persistence&msgNo=2026 for the further code review process.
        Hide
        mf125085 added a comment -

        target milestone.

        Show
        mf125085 added a comment - target milestone.
        Hide
        sherryshen added a comment -

        Verified the fix in 9.1pe_b22 for cascade persist involving
        non-entity and filed a new issue 1347 since direct persisting
        non-entity instances is successful as before.

        Show
        sherryshen added a comment - Verified the fix in 9.1pe_b22 for cascade persist involving non-entity and filed a new issue 1347 since direct persisting non-entity instances is successful as before.

          People

          • Assignee:
            mf125085
            Reporter:
            sherryshen
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: