title: Oracle ADF - Create Child Entity on Parent Entity Creation
body: <p>I am building an ADF Fusion web application (12c) using a MySQL database.</p>
  
  <p>To get auto-incremented PK values upon insert, I am using the method of assigning an "AutoIncrementProperty" property set (property "AI", value "true") to auto-increment PK fields.  All entities are extended from a class that overrides doDML().  That all works just fine, but FYI, this is the code I use for that:</p>
  
  <pre><code>protected void doDML(int i, TransactionEvent transactionEvent) {
      super.doDML(i, transactionEvent);
  
      if (i == DML_INSERT) {
          populateAutoincrementAtt();
      }
  }
  
  /*
  * Determines if the Entity PK is marked as an autoincrement col
  * and executes a MySQL function to retrieve the last insert id
  */
  private void populateAutoincrementAtt() {
      EntityDefImpl entdef = this.getEntityDef();
      AttributeDef pk = null;
      //look for primary key with Autoincrement property set
      for (AttributeDef att : entdef.getAttributeDefs()) {
          if (att.isPrimaryKey() &amp;&amp; (att.getProperty("AI") != null )) {
              pk = att;
              break;
          }
      }
      if (pk != null) {
          try (PreparedStatement stmt = 
               this.getDBTransaction()
                   .createPreparedStatement("SELECT last_insert_id()", 1)) {
              stmt.execute();
              try (ResultSet rs = stmt.getResultSet()) {
                  if (rs.next()) {
                      setAttribute(pk.getName(), rs.getInt(1));
                  }
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>OK, that's not the problem.  That works as it should.</p>
  
  <p>I have two entities with corresponding MySQL tables.  Let's call them "Person" and "Folder".  The Folder entity/table is recursive insomuch as it looks like this:</p>
  
  <p><strong>Folder</strong></p>
  
  <ul>
  <li><em>id</em> (auto-increment PK integer) </li>
  <li><em>parentId</em>  (foreign key to a parent Folder object) </li>
  <li><em>folderName</em> </li>
  <li>...</li>
  </ul>
  
  <p>And, the Person entity/table looks something like this:</p>
  
  <p><strong>Person</strong></p>
  
  <ul>
  <li><em>id</em> (auto-increment PK integer)</li>
  <li><em>userName</em></li>
  <li><em>rootFolderId</em> (foreign key to Folder table)</li>
  <li>...</li>
  </ul>
  
  <p>So, a Person has a root Folder assigned to him/her.  (That Folder may then have a hierarchy of sub-folders.)</p>
  
  <p>The problem I am having is that, when I create a Person entity, I want to create a new Folder entity, get its PK value, then assign that Integer to the new Person's rootFolderId attribute.</p>
  
  <p>Should be really simple, no?</p>
  
  <p>In the PersonImpl class (extends EntityImpl), I have the following method:</p>
  
  <pre><code>private Integer createRootFolder() {
      Integer newIdAssigned;
  
      String entityName = "com.my.model.entity.Folder";
      EntityDefImpl folderDef = EntityDefImpl.findDefObject(entityName);
      EntityImpl newFolder = (EntityImpl) folderDef.createInstance2(getDBTransaction(), null);
      newFolder.setAttribute("folderName", "ROOT");
      try {
          getDBTransaction().commit();
          newIdAssigned = (Integer) newCollection.getAttribute("Id");
      } catch (JboException ex) {
          getDBTransaction().rollback();
          newIdAssigned = null;
      }
  
      return newIdAssigned;
  }
  </code></pre>
  
  <p>So, that method works just fine.  It does, indeed, insert a Folder object and returns its PK value.  The problem comes with when/where/how I call this method.</p>
  
  <p>I can call it from the Create method of PersonImpl, like this:</p>
  
  <pre><code>protected void create(AttributeList attributeList) {
      Integer rootFolderId = null;
  
      rootFolderId = createRootFolder();
  
      super.create(attributeList);
      this.setRootFolderId(rootFolderID);
  }
  </code></pre>
  
  <p>But, of course, this creates the root Folder object before the owning Person object is committed to the database.  So, if the Person is never committed, we have an orphaned Folder object that we just created for no reason.</p>
  
  <p>I have tried calling it from doDML() in the PersonImpl class like this:</p>
  
  <pre><code>protected void doDML(int operation, TransactionEvent e) {
      Integer rootFolderId;
  
      super.doDML(operation, e);
  
      if (operation == DML_INSERT) {
          rootFolderId = createRootFolder();
          if (rootFolderId != null) {
              this.setRootCollectionId(rootCollID);
              getDBTransaction().commit();
          }
      }
  }
  </code></pre>
  
  <p>But ... of course, when createRootFolder() calls commit(), this causes doDML() to fire again, and we get a recursive problem.  I've played with kludges like setting a flag to prevent recursive doDML() calls, but they all have problems.  And, I think there must be something much simpler I'm overlooking.</p>
  
  <p>It would be ideal if I could do this declaratively somehow in the entities themselves, without code.  But, I should think the coded solution would be pretty simple.  I'm just not seeing it.</p>
  
  <p>Any words?</p>