4 Replies Latest reply: Mar 24, 2013 9:00 AM by Jobinesh-Oracle RSS

    Entity with key cannot be owned by another row

    943099
      Hi All,

      I have a master detail relationship between 2 VOs using Entity level association. When I try to create details rows using createAndInitRow(), I am getting below error. The Association has composition association, cascade delete, cascade update key attributes selected

      Entity with key:oracle.jbo.Key[ XXXXXXX ] cannot be owned by another row:oracle.jbo.Key[ XXXXXXX ]

      Code -
      RowSetIterator rsi = (RowSetIterator)((ParentVORowImpl)parentCurrentRow).getChildVO();

      NameValuePairs nvp = null;
      Row row = null;

      for (ExpressionObject obj : objectList) {
      nvp = new NameValuePairs();

      nvp.setAttribute("AAA", "AAA");
      nvp.setAttribute(<Attributes>, <Attribute Values>);
      row = rsi.createAndInitRow(nvp);
      rsi.insertRow(row);
      }
      }

      The moment createAndInitRow is called for first item I get the error and other items does not get associated with parent properly.

      Any clue what wrong am I doing??
        • 1. Re: Entity with key cannot be owned by another row
          Timo Hahn
          One thing you should check is that you set a different value for the key attribute for every child row you create.
          For me it look like you always set the same attribute for every row.
          Next you can tell us which cardinality the relationship has.

          Timo
          • 2. Re: Entity with key cannot be owned by another row
            Jobinesh-Oracle
            Copying from [http://www.packtpub.com/oracle-application-development-framework-real-world-developers-guide/book|Oracle ADF Real World Developers Guide]

            Creating child rows in composition association

            Creating a new child row is a bit tricky when the master-child entity association
            type is composition. When you mark an association between master and child entity
            objects as composition, the framework takes the necessary steps to ensure that the
            child entity object row does not exist without the master entity row. If you try to
            create a new child row by calling createRow() on the view object, the framework
            will throw the oracle.jbo.InvalidOwnerException: JBO-25030: Failed to
            find or invalidate owning entity exception.

            There are two possible ways to create child entity rows in this case:
            • Find the master row by using findByKey() or calling executeQuery() with
            a proper condition and then get the RowIterator for the child collection
            by accessing the view link accessor attribute. Now you can use the child
            RowIterator to call createRow() for creating the child row. The following
            code snippet illustrates the creation of the child entity row for the
            Country-Location composition association:
            //In application module implementation class
            /**
            * This custom method defined in application module
            * creates child location rows for a master
            * country entity through accessor iterator
            */
            public void createChildRowsThruIter() {
            //Find the parent view object
            ViewObject countries = findViewObject("Countries");
            //IN is the Key value for a specific Country row
            Key key = new Key(new Object[] { "IN" });
            //Find the country for 'IN', maxNumOfRows=1
            Row rows[] = countries.findByKey(key, 1);
            //Access the RowIterator for Location
            RowIterator locIter = (RowIterator)rows[0].
            getAttribute("LocationVO");
            //Create and init location row
            Row row = locIter.createRow();
            row.setAttribute("LocationId", 2200);
            row.setAttribute("City", "Bangalore");
            locIter.insertRow(row);
            }
            • Alternatively, you can directly create a new row in the child view object,
            using createAndInitRow() by passing an appropriately constructed instance
            of oracle.jbo.NameValuePairs, which includes the foreign key attribute
            that provides the context for a child row. Here is an example:
            //In application module implementation class
            /**
            * This custom method defined in application module
            * creates child location rows for a master
            * country entity using createAndInitRow()
            */
            public void createChildRowsThruNameValuePairs() {
            ViewObject locations = findViewObject("Locations");
            //Initialize Attributes and Values
            String[] attributes = new String[] { "CountryId" };
            Object[] values = new Object[] { "IN" };
            //Pass the foreign key value for Country while
            //initializing new location row
            Row locRow = locations.createAndInitRow(new
            NameValuePairs(attributes, values));
            //Set other attributes
            locRow.setAttribute("LocationId", 2200);
            locRow.setAttribute("City", "Bangalore");
            locations.insertRow(locRow);
            }
            • 3. Re: Entity with key cannot be owned by another row
              943099
              Cardinality is 1 to *

              Child has composite key [Key1, Key2, Key3],
              My master VO has an attribute (not part of key) which stores Key1 attribute of child VO.

              I have create relation as MasterVo.Key1 = ChildVO.key1.
              All child rows have different keys.

              Before I start creating child I put the Key1 (random number) into parent VO. Then using View Accessor, I start creating child.
              Is there something wrong the way association is created?

              From the log, I see that parent associated to child is not proper.
              Entity with key:oracle.jbo.Key[-82977451 null null null ] owned by row:oracle.jbo.Key[-82977451 null null null ]
              • 4. Re: Entity with key cannot be owned by another row
                Jobinesh-Oracle
                See the second example in my previous post. In case you are passing key while creating child, you dont need to go via accessor, get hold child VO instance directly from AM and create it as shown in approach 2