Index: src/edu/ucsb/nceas/metacat/DBSAXNode.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBSAXNode.java	(revision 757)
+++ src/edu/ucsb/nceas/metacat/DBSAXNode.java	(revision 758)
@@ -57,6 +57,7 @@
     this.conn = conn;
     this.parentNode = null;
     writeChildNodeToDB("DOCUMENT", null, null, docid);
+    updateRootNodeID(getNodeID());
   }
 
   /** 
@@ -91,47 +92,54 @@
       if (nodetype == "DOCUMENT") {
         pstmt = conn.prepareStatement(
             "INSERT INTO xml_nodes " +
-            "(nodeid, nodetype, nodename, docid, rootnodeid) " +
-            "VALUES (?, ?, ?, ?, ?)");
+            "(nodetype, nodename, docid) " +
+            "VALUES (?, ?, ?)");
         MetaCatUtil.debugMessage("INSERTING DOCNAME: " + nodename);
       } else {
         pstmt = conn.prepareStatement(
             "INSERT INTO xml_nodes " +
-            "(nodeid, nodetype, nodename, docid, " +
+            "(nodetype, nodename, docid, " +
             "rootnodeid, parentnodeid, nodedata, nodeindex) " +
-            "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
+            "VALUES (?, ?, ?, ?, ?, ?, ?)");
       }
 
       // Bind the values to the query
       // NOT USED; USED DBAdapter.getUniqueID() instead
       //long nid = generateNodeID();
-      long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
-      pstmt.setLong(1, nid);
-      pstmt.setString(2, nodetype);
-      pstmt.setString(3, nodename);
-      pstmt.setString(4, docid);
+      // NOT USED
+      //let the db generate it: using identity key or through 
+      //the db trigger xml_nodes_before_insert using sequence
+      //long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
+      //pstmt.setLong(1, nid); 
+      pstmt.setString(1, nodetype);
+      pstmt.setString(2, nodename);
+      pstmt.setString(3, docid);
       if (nodetype == "DOCUMENT") {
-        pstmt.setLong(5, nid);
+        // moved it to separate method updateRootNodeID
+        //pstmt.setLong(4, nid);
       } else {
         if (nodetype == "ELEMENT") {
-          pstmt.setLong(5, getRootNodeID());
-          pstmt.setLong(6, getParentID());
-          pstmt.setString(7, data);
-          pstmt.setInt(8, getNodeIndex());
+          pstmt.setLong(4, getRootNodeID());
+          pstmt.setLong(5, getParentID());
+          pstmt.setString(6, data);
+          pstmt.setInt(7, getNodeIndex());
         } else {
-          pstmt.setLong(5, getRootNodeID());
-          pstmt.setLong(6, getNodeID());
+          pstmt.setLong(4, getRootNodeID());
+          pstmt.setLong(5, getNodeID());
           if ( nodetype == "TEXT" && getTagName().equals("meta_file_id") ) {
-            pstmt.setString(7, getDocID());
+            pstmt.setString(6, getDocID());
           } else {
-            pstmt.setString(7, data);
+            pstmt.setString(6, data);
           }  
-          pstmt.setInt(8, incChildNum());
+          pstmt.setInt(7, incChildNum());
         }
       }
       // Do the insertion
       pstmt.execute();
       pstmt.close();
+
+      // get the generated unique id afterward
+      long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
       
       if (nodetype.equals("DOCUMENT")) {
         // Record the root node id that was generated from the database
@@ -159,6 +167,29 @@
   }
 
   /** 
+   * update rootnodeid=nodeid for 'DOCUMENT' type of nodes only
+   */
+  public void updateRootNodeID(long nodeid) throws SAXException {
+      try {
+        PreparedStatement pstmt;
+        pstmt = conn.prepareStatement(
+              "UPDATE xml_nodes set rootnodeid = ? " +
+              "WHERE nodeid = ?");
+
+        // Bind the values to the query
+        pstmt.setLong(1, nodeid);
+        pstmt.setLong(2, nodeid);
+        // Do the update
+        pstmt.execute();
+        pstmt.close();
+      } catch (SQLException e) {
+        System.out.println("Error in DBSaxNode.updateRootNodeID: " + 
+                           e.getMessage());
+        throw new SAXException(e.getMessage());
+      }
+  }
+
+  /** 
    * creates SQL code to put nodename for the document node 
    * into DB connection 
    */
Index: src/edu/ucsb/nceas/metacat/AccessionNumber.java
===================================================================
--- src/edu/ucsb/nceas/metacat/AccessionNumber.java	(revision 757)
+++ src/edu/ucsb/nceas/metacat/AccessionNumber.java	(revision 758)
@@ -108,9 +108,7 @@
         // get a new docid
         if ( docid == null ) {
           String sitecode = getSitecode();
-          // NOT USED; USED DBAdapter.getUniqueID() instead
-          //String uniqueid = getUniqueID();
-          String uniqueid = "" + dbAdapter.getUniqueID(conn,"xml_documents");
+          String uniqueid = getUniqueID(sitecode);
 
           return sitecode + sep + uniqueid;
 
@@ -167,21 +165,22 @@
   }
 
   // get Unique ID from DB sequence
-  private String getUniqueID () throws SQLException
+  private String getUniqueID (String sitecode) throws SQLException
   {
     String uniqueid;
+    String sysdate = dbAdapter.getDateTimeFunction();
     
     try {
       PreparedStatement pstmt;
       pstmt = conn.prepareStatement
-              ("SELECT accnum_uniqueid_seq.nextval FROM dual");
+              ("INSERT INTO accession_number (site_code, date_created) " +
+               "VALUES (?, " + sysdate + ")");
+      pstmt.setString(1, sitecode);
       pstmt.execute();
-
-      ResultSet rs = pstmt.getResultSet();
-      boolean hasRow = rs.next();
-      uniqueid = rs.getString(1);
       pstmt.close();
       
+      uniqueid = "" + dbAdapter.getUniqueID(conn, "accession_number");
+
     } catch (SQLException e) {
       throw new 
       SQLException("Error on AccessionNumber.getUniqueID(): "+e.getMessage());
