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

📄 abstractdomain.java

📁 把java对象映射成数据库表中的一条记录
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        // Tell JDBCHelper to ignore commit and close messages until
        // endTransaction() is called.
        this.beginTransaction(aJDBCHelper);
        okToEndTransaction = true;
        }

    // Perform any special logic specified in the subclass.
    this.preDelete(aPO, aJDBCHelper);

    StringBuffer sqlBuffer = new StringBuffer();
    sqlBuffer.append(" DELETE FROM ");
    sqlBuffer.append(this.getTableName());
    sqlBuffer.append(" WHERE ");
    sqlBuffer.append(
        i_primaryKeyColumnSpec.buildWhereClause(aPO,
                                                ColumnSpec.EQUALS,
                                                null, // table alias
                                                this.getDatabasePolicy()));
    int rowsDeleted =
            this.executeSQLUpdate(sqlBuffer.toString(), aJDBCHelper);

    // Perform any special logic specified in the subclass.
    this.postDelete(aPO, aJDBCHelper);

    if (okToEndTransaction)
        {
        // Tell JDBCHelper that it's OK to close the connection now.
        this.endTransaction(aJDBCHelper);
        }

    return rowsDeleted;
    } // delete(aPO, aJDBCHelper)


  public void createTable()
    {
    this.createTable(this.getJDBCHelper());
    }


  /**
   * Build and execute SQL to create a table with default column types for
   * this domain.  This was written to create test tables for running the
   * test suite.  If this class represents a subtype table, that is the
   * table that will be created.
   *
   * @param aJDBCHelper a value of type 'JDBCHelper'
   */
  public void createTable(JDBCHelper aJDBCHelper)
    {
    if (aJDBCHelper == null)
        {
        throw new IllegalArgumentException(
            "A null value argument for aJDBCHelper was passed to "
            + "AbstractDomain#createTable(aJDBCHelper)");
        }
    CreateTableSQLBuilder sqlBuilder = this.getCreateTableSQLBuilder();

    boolean okToEndTransaction = false;
    if (!aJDBCHelper.isInsideTransaction())
        {
        // Tell JDBCHelper to ignore commit and close messages until
        // endTransaction() is called.
        this.beginTransaction(aJDBCHelper);
        okToEndTransaction = true;
        }

    if (i_subtypeColumnSpecs.size() > 0)
        { // Create the subtype table instead
        List colSpecs = new ArrayList();
        colSpecs.add(i_primaryKeyColumnSpec);
        colSpecs.addAll(i_subtypeColumnSpecs);
        String sql = sqlBuilder.buildSQL(i_subtypeTable.getTableName(),
                                         colSpecs);
        int rows = this.executeSQLUpdate(sql, aJDBCHelper);
        LOG.debug(
            i_subtypeTable.getTableName() + " table created. rows: " + rows);
        }
    else
        { // create the table
        String sql = sqlBuilder.buildSQL(i_tableName,
                                         i_columnSpecs);
        int rows = this.executeSQLUpdate(sql, aJDBCHelper);
        LOG.debug(i_tableName + " table created. rows: " + rows);
        }

    try
        {
        // Create the Sequence object for this table, if needed.
        if (i_primaryKeyColumnSpec.isSequencedPrimaryKey())
            {
            this.getDatabasePolicy().createSequence(this, aJDBCHelper);
            }
        }
    catch (SQLException e)
        {
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw new DatabaseException(
            e, "SQLException occurred while creating sequence");
        }
    catch (DatabaseException e)
        {
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw e;
        }
    catch (Exception e)
        {
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw new DatabaseException(e);
        }

    if (okToEndTransaction)
        {
        // Tell JDBCHelper that it's OK to commit the transaction now.
        this.endTransaction(aJDBCHelper);
        }
    } // createTable(aJDBCHelper)


  /**
   * Execute SQL to drop the table for this domain object - use the default
   * JDBCHelper.
   *
   * @param ignoreErrors a value of type 'boolean'
   */
  public void dropTable(boolean ignoreErrors)
    {
    this.dropTable(ignoreErrors, this.getJDBCHelper());
    }


  /**
   * Execute SQL to drop the table for this domain object.  If this class
   * represents a subtype table, then the subtype table will be dropped
   * instead.  This code is unfortunately ugly because of the reimplementation
   * of error handling when ignoreErrors is true.
   *
   * @param ignoreErrors a value of type 'boolean' - It is often that
   *        we don't want to know if the table wasn't there.
   * @param aJDBCHelper a value of type 'JDBCHelper'
   */
  public void dropTable(boolean ignoreErrors, JDBCHelper aJDBCHelper)
    {
    String sql = null;
    if (i_subtypeColumnSpecs.size() > 0)
        {
        sql = "DROP TABLE " + i_subtypeTable.getTableName();
        }
    else
        {
        sql = "DROP TABLE " + i_tableName;
        }

    // We don't want to begin (or end) a "transaction" if we are already in
    // one.  The given JDBCHelper might already be inside a transaction if
    // this method is being called from a preSave() or postSave() method of
    // another domain.
    boolean okToEndTransaction = false;
    if (!aJDBCHelper.isInsideTransaction())
        {
        // Tell JDBCHelper to ignore commit and close messages until
        // endTransaction() is called.
        this.beginTransaction(aJDBCHelper);
        okToEndTransaction = true;
        }

    if (!ignoreErrors)
        {
        // Use the AbstractDomain code to drop the tables.
        // This does all the proper error handling, rolling back, etc.
        this.executeSQLUpdate(sql, aJDBCHelper);
        if (okToEndTransaction)
            {
            // commit tx and close JDBCHelper
            this.endTransaction(aJDBCHelper);
            }
        return;
        }

    try
        {
        // Do our own error handling.
        aJDBCHelper.executeUpdate(sql);
        if (okToEndTransaction)
            {
            // commit tx and close JDBCHelper
            this.endTransaction(aJDBCHelper);
            }
        }
    catch (SQLException e)
        {
        LOG.warn(
           "Ignoring SQLException in AbstractDomain.dropTable(...)", e);
        if (okToEndTransaction)
            {
            // commit tx and close JDBCHelper.
            // This may seem strange, but if there was no outer transaction
            // context, then we have to close the transaction that was
            // started above.
            this.endTransaction(aJDBCHelper);
            }
        }
    catch (Exception e)
        {
        if (okToEndTransaction)
            {
            // commit tx and close JDBCHelper.
            // This may seem strange, but if there was no outer transaction
            // context, then we have to close the transaction that was
            // started above.
            try
                {
                this.endTransaction(aJDBCHelper);
                }
            catch (DatabaseException de)
                {
                // do nothing since the original exception should take
                // precedence.
                }
            }
        throw new DatabaseException(
            e,"Exception occurred while dropping table: " + i_tableName);
        }
    } // dropTable(ignoreErrors, aJDBCHelper)


  /* ===============  Protected and Private Helper Methods  =============== */


  /**
   * Execute the SQL and pass each row to the given RowHandler.  This is a
   * pass-through method that adds the default JDBCHelper to the argument
   * list.
   *
   * @param sql a value of type 'String'
   * @param aRowHandler a value of type 'RowHandler'
   */
  protected void executeSQLQuery(String sql,
                                 RowHandler aRowHandler)
    {
    this.executeSQLQuery(sql, aRowHandler, this.getJDBCHelper());
    }


  /**
   * Execute the SQL and pass each row to the given RowHandler.
   *
   * @param sql a value of type 'String'
   * @param aRowHandler a value of type 'RowHandler' - This is an inner
   *        class that gets called for each row in the result set.
   * @param aJDBCHelper a value of type 'JDBCHelper'
   */
  protected void executeSQLQuery(String sql,
                                 RowHandler aRowHandler,
                                 JDBCHelper aJDBCHelper)
    {
    if (aRowHandler == null)
        {
        throw new IllegalArgumentException(
            "A null value argument for aRowHandler was pass to "
            + "AbstractDomain#executeSQLQuery(sql,handler,jdbcHelper)");
        }
    try
        {
        aJDBCHelper.executeQuery(sql);
        Object value = null;
        while (aJDBCHelper.next())
            {
            aRowHandler.handleRow(aJDBCHelper);
            } // while
        aJDBCHelper.close();
        }
    catch (RuntimeException e)
        {
        // rethrow RuntimeException after closing connection.
        // If an exception occurs on close, the original exception
        // takes priority.
        LOG.error("RuntimeException in AbstractDomai#executeSQLQuery.", e);
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw e;
        }
    catch (Exception e)
        {
        // Wrap exception in a DatabaseException.
        // If an exception occurs on close, the original exception
        // takes priority.
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw new DatabaseException(e, sql);
        }
    } // executeSQLQuery(sqlString, aRowHandler, aJDBCHelper)


  /**
   * 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 'int'
   */
  protected int executeSQLUpdate(String sql)
    {
    return this.executeSQLUpdate(sql, this.getJDBCHelper());
    }


  /**
   * Execute SQL that updates, deletes, or inserts.
   *
   * @return a value of type 'int' - number of rows affected.
   */
  protected int executeSQLUpdate(String sql,
                                 JDBCHelper aJDBCHelper)
    {
    int rowCount = 0;

    try
        {
        rowCount = aJDBCHelper.executeUpdate(sql);
        aJDBCHelper.close();
        }
    catch (RuntimeException e)
        {
        // rethrow RuntimeExceptions
        this.quietlyRollbackAndClose(aJDBCHelper);
        LOG.error("RuntimeException in AbstractDomai#executeSQLUpdate.", e);
        throw e;
        }
    catch (Exception e)
        {
        // wrap all other exceptions as a DatabaseException
        this.quietlyRollbackAndClose(aJDBCHelper);
        throw new DatabaseException(e, sql);
        }
    return rowCount;
    } // executeSQLUpdate(sqlString, aJDBCHelper)


  /**
   * Use JDBC to call a stored procedure to get a the next available
   * sequence id.
   *
   * @return a value of type 'Long'
   */
  protected Long nextSequence(JDBCHelper aJDBCHelper)
    {
    return this.findLong(
        this.getDatabasePolicy().sequenceSQL(this.getTableName()),
        aJDBCHelper);
    } // nextSequence()


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


  /**
   * This method calls findLong and casts the result down to an Integer.  It
   * is up to the developer to be sure the result will fit into an Integer.
   *
   * @param sql a value of type 'String'
   * @return a value of type 'Integer'
   */
  protected Integer findInteger(String sql,
                                JDBCHelper aJDBCHelper)
    {
    Long aLong = this.findLong(sql, aJDBCHelper);
    if (aLong == null)
        {
        return null;
        }
    return new Integer(aLong.intValue());
    }


  /**
   * This is a pass-through method that adds the default JDBCHelper to the
   * parameter list.
   *
   * @param sql a value of type 'String'
   * @return a value of type 'Long'
   */
  protected Long findLong(String sql)
    {
    return th

⌨️ 快捷键说明

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