public void check() throws Exception {
   if (dba == null)
     throw new IllegalStateException("InstanceReferenceChecker.check(): No database specified.");
   GKSchema schema = (GKSchema) dba.getSchema();
   Set instanceAtts = new HashSet();
   GKSchemaAttribute att = null;
   for (Iterator it = schema.getOriginalAttributes().iterator(); it.hasNext(); ) {
     att = (GKSchemaAttribute) it.next();
     if (att.isInstanceTypeAttribute()) {
       instanceAtts.add(att);
     }
   }
   System.out.println("Instance Atts: " + instanceAtts.size());
   Connection conn = dba.getConnection();
   Statement stat = conn.createStatement();
   ResultSet resultSet = null;
   GKSchemaClass cls = null;
   Map attMap = new HashMap();
   Map tableNameMap = new HashMap();
   String query = null;
   for (Iterator it = instanceAtts.iterator(); it.hasNext(); ) {
     att = (GKSchemaAttribute) it.next();
     cls = (GKSchemaClass) att.getOrigin();
     System.out.println("Checking " + att.getName() + " in " + cls.getName() + "...");
     String tableName = null;
     if (!att.isMultiple()) {
       tableName = cls.getName();
     } else {
       tableName = cls.getName() + "_2_" + att.getName();
     }
     query =
         "SELECT distinct " + att.getName() + ", " + att.getName() + "_class FROM " + tableName;
     resultSet = stat.executeQuery(query);
     while (resultSet.next()) {
       long dbID = resultSet.getLong(1);
       String clsName = resultSet.getString(2);
       if (dbID == 0 && clsName == null) {
         // errorMessage.append("0, null occur for " + att.getName() + " in " + tableName);
         // errorMessage.append("\n");
         continue;
       }
       Long dbIDLong = new Long(dbID);
       String tmpCls = (String) attMap.get(dbIDLong);
       if (tmpCls != null && !tmpCls.equals(clsName)) {
         errorMessage.append(dbID + ", " + tmpCls + " in ");
         errorMessage.append(tableNameMap.get(dbIDLong) + " is different from ");
         errorMessage.append(tableName);
         errorMessage.append("\n");
       } else {
         attMap.put(dbIDLong, clsName);
         tableNameMap.put(dbIDLong, tableName);
       }
     }
     resultSet.close();
   }
   stat.close();
   System.out.println("The size of the map: " + attMap.size());
   // Check DB_ID in the database
   query = "SELECT _class FROM DatabaseObject WHERE DB_ID = ?";
   PreparedStatement prepStat = conn.prepareStatement(query);
   int c = 0;
   // Set notInSet = new HashSet();
   for (Iterator it = attMap.keySet().iterator(); it.hasNext(); ) {
     Long dbID = (Long) it.next();
     prepStat.setLong(1, dbID.longValue());
     resultSet = prepStat.executeQuery();
     c = 0;
     while (resultSet.next()) {
       c++;
       String clsName = resultSet.getString(1);
       String tmpClsName = (String) attMap.get(dbID);
       if (!tmpClsName.equals(clsName)) {
         errorMessage.append(dbID + ", " + tmpClsName);
         errorMessage.append(" in " + tableNameMap.get(dbID));
         errorMessage.append(" is different in DatabaseObject (");
         errorMessage.append(clsName);
         errorMessage.append(")\n");
       }
     }
     if (c == 0) {
       errorMessage.append(dbID + ", " + attMap.get(dbID));
       errorMessage.append(" in " + tableNameMap.get(dbID));
       errorMessage.append(" is not in the DatabaseObject");
       errorMessage.append("\n");
     } else if (c > 1) {
       errorMessage.append(dbID + " occurs in more than once in DatabaseObject");
       errorMessage.append("\n");
     }
     resultSet.close();
   }
   prepStat.close();
   // Print out an empty line
   System.out.println();
   if (errorMessage.length() == 0) {
     System.out.println("Nothing Wrong!");
   } else {
     FileWriter fileWriter = new FileWriter("error.txt");
     BufferedWriter writer = new BufferedWriter(fileWriter);
     writer.write(errorMessage.toString());
     writer.close();
     fileWriter.close();
     System.out.println("Done. See errors in error.txt.");
   }
 }