⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractdomain.java

📁 把java对象映射成数据库表中的一条记录
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   *   // aPO.setName(aJDBCHelper.getString("Name"));
   *   // return value;
   *
   * @param aJDBCHelper a value of type 'JDBCHelper'
   * @return a value of type 'Object'
   * @exception Exception if an error occurs
   */
  protected PersistentObject convertToPersistentObject (JDBCHelper aJDBCHelper)
          throws Exception
    {
    PersistentObject aPO = this.newPersistentObject();
    Iterator iterator = null;
    Iterator it = null;
    ColumnSpec aColumnSpec = null;
    JoinTable aJoinTable = null;
    JoinColumn aJoinColumn = null;

    iterator = i_columnSpecs.iterator();
    while (iterator.hasNext())
        {
        aColumnSpec = (ColumnSpec)iterator.next();
        aColumnSpec.copyColumnValueToPersistentObject(aJDBCHelper, aPO);
        } // while

    // Assign joined columns to the object too.
    iterator = i_joinTables.iterator();
    while (iterator.hasNext())
        {   // join columns
        aJoinTable = (JoinTable) iterator.next();
        aJoinTable.copyColumnValuesToPersistentObject(aJDBCHelper, aPO);
        }

    if (i_subtypeTable != null)
        {
        i_subtypeTable.copyColumnValuesToPersistentObject(aJDBCHelper, aPO);
        }
    return aPO;
    } // convertToPersistentObject(aJDBCHelper)



  /* ========  These methods will usually not be overridden  ======== */
  /* ========  unless additional behavior is needed          ======== */


  /**
   * This is a pass-through method that adds the default JDBCHelper to the
   * argument list.  This method should not be overridden.  Override
   * find(aPO,aJDBCHelper) instead.
   *
   * @param pkOrPersistentObject a value of type 'Object'
   * @return a value of type 'PersistentObject'
   */
  public final PersistentObject find(Object pkOrPersistentObject)
    {
    return this.find(pkOrPersistentObject, this.getJDBCHelper());
    }


  /**
   * Find and return one PersistentObject using either the raw primary key
   * or the primary key attribute(s) from a PersistentObject object.  If a
   * value is not found then null is returned.
   *
   * <p>This method can be overridden.
   *
   * @param pk a value of type 'Object'.  This can be a PersistentObject
   * with it's primary key field(s) filled in or it can be a single primary
   * key value (i.e. a String or an Integer).
   * @param aJDBCHelper a value of type 'JDBCHelper'
   * @return a value of type 'PersistentObject'
   */
  public PersistentObject find(Object pkOrPersistentObject,
                               JDBCHelper aJDBCHelper)
    {
    return this.finalFind(pkOrPersistentObject, aJDBCHelper);
    }


  /**
   * This is a protected method that cannot be overridden.  Using this
   * method will always do a JDBC call.
   *
   * @param pk a value of type 'Object'.  This can be a PersistentObject
   * with it's primary key attribute(s) filled in or a singular primary key
   * value (like String or Integer).
   * @param aJDBCHelper a value of type 'JDBCHelper'
   * @return a value of type 'PersistentObject'
   */
  protected final PersistentObject finalFind(Object pkOrPersistentObject,
                                             JDBCHelper aJDBCHelper)
    {
    if (LOG.isDebugEnabled())  // this avoids StringBuffer creation
        {
        LOG.debug("AbstractDomain.finalFind(" + pkOrPersistentObject
                         + ", " + aJDBCHelper + ")");
        }
    PersistentObject returnValue = null;
    if (pkOrPersistentObject == null)
        {
        throw new IllegalArgumentException(
            "Null value argument for aPO was passed in to "
            + "AbstractDomain#find(aPO,aJDBCHelper).");
        }

    if (aJDBCHelper == null)
        {
        throw new IllegalArgumentException(
            "Null value argument for aJDBCHelper was passed in to "
            + "AbstractDomain#find(aPO,aJDBCHelper).");
        }

    String whereClause =
            i_primaryKeyColumnSpec.buildWhereClause(pkOrPersistentObject,
                                                    ColumnSpec.EQUALS,
                                                    i_tableAlias,
                                                    this.getDatabasePolicy());
    List aList = this.findWhere(whereClause, aJDBCHelper);

    if (aList.size() > 0)
        {
        returnValue = (PersistentObject) aList.get(0);
        }

    if (returnValue == null)
        {
        return null;
        }

    return returnValue;
    } // finalFind(pkOrPersistentObject, aJDBCHelper)


  /**
   * This is a pass-through method that adds the default JDBCHelper to the
   * argument list.
   *
   * @return a value of type 'List'
   */
  public List findAll()
    {
    return this.findAll(this.getJDBCHelper());
    }


  /**
   * Find all PersistentObject values found in my table.
   *
   * @param aJDBCHelper a value of type 'JDBCHelper'
   * @return a value of type 'List'
   */
  public List findAll(JDBCHelper aJDBCHelper)
    {
    if (LOG.isDebugEnabled())  // this avoids StringBuffer creation
        {
        LOG.debug("AbstractDomain.findAll(" + aJDBCHelper + ")");
        }
    return this.find(this.getSelectSQLBuilder(), aJDBCHelper);
    } // findAll(aJDBCHelper)



  /**
   * This method adds a where clause to the basic findAll SQL.  'WHERE'
   * should not be included in the argument string.
   */
  public final List findWhere(String whereString)
    {
    return this.findWhere(whereString, this.getJDBCHelper());
    }



  /**
   * This method adds a where clause to the basic findAll SQL.  'WHERE'
   * should not be included in the argument string.
   */
  public final List findWhere(String whereString,
                              JDBCHelper aJDBCHelper)
    {
    SelectSQLBuilder builder = this.getSelectSQLBuilder();
    builder.setWhere(whereString);
    return this.find(builder, aJDBCHelper);
    }


  /**
   * This method adds an ORDER BY clause to the basic findAll SQL.  'ORDER
   * BY' should not be included in the argument string.
   */
  public final List findOrderBy(String orderByString)
    {
    return this.findOrderBy(orderByString, this.getJDBCHelper());
    }



  /**
   * This method adds an ORDER BY clause to the basic findAll SQL.  'ORDER
   * BY' should not be included in the argument string.
   */
  public final List findOrderBy(String orderByString,
                                JDBCHelper aJDBCHelper)
    {
    SelectSQLBuilder builder = this.getSelectSQLBuilder();
    builder.setOrderBy(orderByString);
    return this.find(builder, aJDBCHelper);
    }


  /**
   * This method adds both a WHERE and an ORDER BY clause to the basic
   * findAll SQL.  Neither 'ORDER BY' nor 'WHERE' should not be included in
   * the argument strings.
   */
  public final List findWhereOrderBy(String whereString,
                                     String orderByString)
    {
    return this.findWhereOrderBy(whereString,
                                 orderByString,
                                 this.getJDBCHelper());
    }


  /**
   * This method adds both a WHERE and an ORDER BY clause to the basic
   * findAll SQL.  Neither 'ORDER BY' nor 'WHERE' should not be included in
   * the argument strings.
   */
  public final List findWhereOrderBy(String whereString,
                                     String orderByString,
                                     JDBCHelper aJDBCHelper)
    {
    SelectSQLBuilder builder = this.getSelectSQLBuilder();
    builder.setOrderBy(orderByString);
    builder.setWhere(whereString);
    return this.find(builder, aJDBCHelper);
    }


  public List find(SelectSQLBuilder builder)
    {
    return this.find(builder, this.getJDBCHelper());
    }


  public List find(SelectSQLBuilder builder,
                   JDBCHelper aJDBCHelper)
    {
    return this.findCustom (
        builder.buildSQL(),
        aJDBCHelper);
    }

  public SelectSQLBuilder getSelectSQLBuilder()
    {
    return new SelectSQLBuilder(this);
    }

  public InsertSQLBuilder getInsertSQLBuilder()
    {
    return new InsertSQLBuilder(this);
    }

  public UpdateSQLBuilder getUpdateSQLBuilder()
    {
    return new UpdateSQLBuilder(this);
    }

  public CreateTableSQLBuilder getCreateTableSQLBuilder()
    {
    return new CreateTableSQLBuilder(this);
    }


  /**
   * This is a pass-through method that adds the default JDBCHelper to the
   * argument list.
   *
   * @param sql a value of type 'String'
   * @return a value of type 'List'
   */
  public List findCustom(String sql)
    {
    return this.findCustom(sql, this.getJDBCHelper());
    }


  /**
   * This method should *rarely*, if ever, be used by subclasses.  The
   * findWhere methods are to be preferred over this method.
   *
   * Subclasses can use this method when (for whatever reason) they need to
   * write totally custom SQL but still want the result set converted to
   * persistent objects.  This method returns a List of persistent objects
   * using the given SQL string.  An inner class is used that tells the
   * executeSQLQuery() method how to handle the result set rows.
   *
   * @param sql a value of type 'String'
   * @param aJDBCHelper a value of type 'JDBCHelper'
   * @return a value of type 'List'
   */
  public List findCustom(String sql,
                         JDBCHelper aJDBCHelper)
    {
    final List result = new ArrayList();
    RowHandler handler;
    if (i_endingIndex < i_startingIndex)
        {
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw new ConfigurationException(
            "Starting index must not be greater than the ending index");
        }
    handler = new RowHandler()
          {
          PersistentObject aPO = null;
          int startingIndex = i_startingIndex;
          int endingIndex = i_endingIndex;
          int rowNumber = 0;
          public void handleRow(JDBCHelper aJDBCHelper2)
                  throws Exception
            {
            rowNumber++;
            if (startingIndex < 0 ||
                endingIndex < 0 ||
                (rowNumber >= startingIndex &&
                 rowNumber <= endingIndex))
                {
                aPO = convertToPersistentObject(aJDBCHelper2);
                if (i_usePostFind)
                    {
                    postFind(aPO, aJDBCHelper2);
                    }
                aPO.forceCurrentPersistentState();
                result.add(aPO);
                }
            } // handleRow(aJDBCHelper)
          };
    this.executeSQLQuery(sql, handler, aJDBCHelper);
    return result;
    } // findCustom(sqlString, aJDBCHelper)



  /**
   * This is a pass-through method that adds the default JDBCHelper to the
   * argument list.  Do not override this method since it is not called by
   * the framework.  Override validate(aPO, aJDBCHelper) instead.
   *
   * @see AbstractDomain#validate(PersistentObject, JDBCHelper)
   *
   * @param aPO a value of type 'PersistentObject'
   * @exception MissingAttributeException if an error occurs
   * @exception DuplicateRowException if an error occurs
   */
  public final void validate(PersistentObject aPO)
          throws
          MissingAttributeException,
          DuplicateRowException
    {
    this.validate(aPO, this.getJDBCHelper());
    }


  /**
   * This method validates a PersistentObject using the ColumnSpec objects.
   * When a "problem" is found, the JDBCHelper is rolled back and an
   * exception is thrown.  Explicit validation is done here (rather than
   * depend on database constraints at time of insert or update) to give a
   * consistent error message across databases.
   *
   * @param aPO a value of type 'PersistentObject'
   * @exception MissingAttributeException if a required attribute is missing.
   * @exception DuplicateRowException if this row is already in the database.
   */
  public void validate(PersistentObject aPO,
                       JDBCHelper aJDBCHelper)
          throws
          MissingAttributeException,
          DuplicateRowException
    {
    if (LOG.isDebugEnabled())  // this avoids StringBuffer creation
        {
        LOG.debug("AbstractDomain.validate(" + aPO
                         + ", " + aJDBCHelper + ")");
        }
    ColumnSpec aColumnSpec = null; // This defines an attribute

    if (aPO == null)
        {
        throw new IllegalArgumentException(
            "Null argument value for aPO was passed to "
            +"AbstractDomain#validate(...)");
        }
    if (aJDBCHelper == null)
        {
        throw new IllegalArgumentException(

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -