Package org.hibernate.jdbc

Examples of org.hibernate.jdbc.Expectation


  private void writeIndex(PersistentCollection collection, Iterator entries, Serializable id, SessionImplementor session) {
    // If one-to-many and inverse, still need to create the index.  See HHH-5732.
    if ( isInverse && hasIndex && !indexContainsFormula ) {
      try {
        if ( entries.hasNext() ) {
          Expectation expectation = Expectations.appropriateExpectation( getUpdateCheckStyle() );
          int i = 0;
          int count = 0;
          while ( entries.hasNext() ) {

            final Object entry = entries.next();
            if ( entry != null && collection.entryExists( entry, i ) ) {
              int offset = 1;
              PreparedStatement st = null;
              boolean callable = isUpdateCallable();
              boolean useBatch = expectation.canBeBatched();
              String sql = getSQLUpdateRowString();

              if ( useBatch ) {
                if ( recreateBatchKey == null ) {
                  recreateBatchKey = new BasicBatchKey(
                      getRole() + "#RECREATE",
                      expectation
                      );
                }
                st = session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getBatch( recreateBatchKey )
                    .getBatchStatement( sql, callable );
              }
              else {
                st = session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getStatementPreparer()
                    .prepareStatement( sql, callable );
              }

              try {
                offset += expectation.prepare( st );
                if ( hasIdentifier ) {
                  offset = writeIdentifier( st, collection.getIdentifier( entry, i ), offset, session );
                }
                offset = writeIndex( st, collection.getIndex( entry, i, this ), offset, session );
                offset = writeElement( st, collection.getElement( entry ), offset, session );

                if ( useBatch ) {
                  session.getTransactionCoordinator()
                      .getJdbcCoordinator()
                      .getBatch( recreateBatchKey )
                      .addToBatch();
                }
                else {
                  expectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
                }
                count++;
              }
              catch ( SQLException sqle ) {
                if ( useBatch ) {
View Full Code Here


    // constraints and so that we can take better advantage of batching
   
    try {
      int count = 0;
      if ( isRowDeleteEnabled() ) {
        final Expectation deleteExpectation = Expectations.appropriateExpectation( getDeleteCheckStyle() );
        final boolean useBatch = deleteExpectation.canBeBatched();
        if ( useBatch && deleteRowBatchKey == null ) {
          deleteRowBatchKey = new BasicBatchKey(
              getRole() + "#DELETEROW",
              deleteExpectation
          );
        }
        final String sql = getSQLDeleteRowString();

        PreparedStatement st = null;
        // update removed rows fks to null
        try {
          int i = 0;
          Iterator entries = collection.entries( this );
          int offset = 1;
          while ( entries.hasNext() ) {
            Object entry = entries.next();
            if ( collection.needsUpdating( entry, i, elementType ) ) {  // will still be issued when it used to be null
              if ( useBatch ) {
                st = session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getBatch( deleteRowBatchKey )
                    .getBatchStatement( sql, isDeleteCallable() );
              }
              else {
                st = session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getStatementPreparer()
                    .prepareStatement( sql, isDeleteCallable() );
              }
              int loc = writeKey( st, id, offset, session );
              writeElementToWhere( st, collection.getSnapshotElement(entry, i), loc, session );
              if ( useBatch ) {
                session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getBatch( deleteRowBatchKey )
                    .addToBatch();
              }
              else {
                deleteExpectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
              }
              count++;
            }
            i++;
          }
        }
        catch ( SQLException e ) {
          if ( useBatch ) {
            session.getTransactionCoordinator().getJdbcCoordinator().abortBatch();
          }
          throw e;
        }
        finally {
          if ( !useBatch ) {
            session.getTransactionCoordinator().getJdbcCoordinator().release( st );
          }
        }
      }
     
      if ( isRowInsertEnabled() ) {
        final Expectation insertExpectation = Expectations.appropriateExpectation( getInsertCheckStyle() );
        boolean useBatch = insertExpectation.canBeBatched();
        boolean callable = isInsertCallable();
        if ( useBatch && insertRowBatchKey == null ) {
          insertRowBatchKey = new BasicBatchKey(
              getRole() + "#INSERTROW",
              insertExpectation
          );
        }
        final String sql = getSQLInsertRowString();

        PreparedStatement st = null;
        // now update all changed or added rows fks
        try {
          int i = 0;
          Iterator entries = collection.entries( this );
          while ( entries.hasNext() ) {
            Object entry = entries.next();
            int offset = 1;
            if ( collection.needsUpdating( entry, i, elementType ) ) {
              if ( useBatch ) {
                st = session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getBatch( insertRowBatchKey )
                    .getBatchStatement( sql, callable );
              }
              else {
                st = session.getTransactionCoordinator()
                    .getJdbcCoordinator()
                    .getStatementPreparer()
                    .prepareStatement( sql, callable );
              }

              offset += insertExpectation.prepare( st );

              int loc = writeKey( st, id, offset, session );
              if ( hasIndex && !indexContainsFormula ) {
                loc = writeIndexToWhere( st, collection.getIndex( entry, i, this ), loc, session );
              }

              writeElementToWhere( st, collection.getElement( entry ), loc, session );

              if ( useBatch ) {
                session.getTransactionCoordinator().getJdbcCoordinator().getBatch( insertRowBatchKey ).addToBatch();
              }
              else {
                insertExpectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
              }
              count++;
            }
            i++;
          }
View Full Code Here

      if ( j == 0 && isVersioned() )
        LOG.tracev( "Version: {0}", Versioning.getVersion( fields, this ) );
    }

    // TODO : shouldn't inserts be Expectations.NONE?
    final Expectation expectation = Expectations.appropriateExpectation( insertResultCheckStyles[j] );
    // we can't batch joined inserts, *especially* not if it is an identity insert;
    // nor can we batch statements where the expectation is based on an output param
    final boolean useBatch = j == 0 && expectation.canBeBatched();
    if ( useBatch && inserBatchKey == null ) {
      inserBatchKey = new BasicBatchKey(
          getEntityName() + "#INSERT",
          expectation
      );
    }
    final boolean callable = isInsertCallable( j );

    try {
      // Render the SQL query
      final PreparedStatement insert;
      if ( useBatch ) {
        insert = session.getTransactionCoordinator()
            .getJdbcCoordinator()
            .getBatch( inserBatchKey )
            .getBatchStatement( sql, callable );
      }
      else {
        insert = session.getTransactionCoordinator()
            .getJdbcCoordinator()
            .getStatementPreparer()
            .prepareStatement( sql, callable );
      }

      try {
        int index = 1;
        index += expectation.prepare( insert );

        // Write the values of fields onto the prepared statement - we MUST use the state at the time the
        // insert was issued (cos of foreign key constraints). Not necessarily the object's current state

        dehydrate( id, fields, null, notNull, propertyColumnInsertable, j, insert, session, index, false );

        if ( useBatch ) {
          session.getTransactionCoordinator().getJdbcCoordinator().getBatch( inserBatchKey ).addToBatch();
        }
        else {
          expectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( insert ), insert, -1 );
        }

      }
      catch ( SQLException e ) {
        if ( useBatch ) {
View Full Code Here

      final Object oldVersion,
      final Object object,
      final String sql,
      final SessionImplementor session) throws HibernateException {

    final Expectation expectation = Expectations.appropriateExpectation( updateResultCheckStyles[j] );
    final boolean useBatch = j == 0 && expectation.canBeBatched() && isBatchable(); //note: updates to joined tables can't be batched...
    if ( useBatch && updateBatchKey == null ) {
      updateBatchKey = new BasicBatchKey(
          getEntityName() + "#UPDATE",
          expectation
      );
    }
    final boolean callable = isUpdateCallable( j );
    final boolean useVersion = j == 0 && isVersioned();

    if ( LOG.isTraceEnabled() ) {
      LOG.tracev( "Updating entity: {0}", MessageHelper.infoString( this, id, getFactory() ) );
      if ( useVersion )
        LOG.tracev( "Existing version: {0} -> New version:{1}", oldVersion, fields[getVersionProperty()] );
    }

    try {
      int index = 1; // starting index
      final PreparedStatement update;
      if ( useBatch ) {
        update = session.getTransactionCoordinator()
            .getJdbcCoordinator()
            .getBatch( updateBatchKey )
            .getBatchStatement( sql, callable );
      }
      else {
        update = session.getTransactionCoordinator()
            .getJdbcCoordinator()
            .getStatementPreparer()
            .prepareStatement( sql, callable );
      }

      try {
        index+= expectation.prepare( update );

        //Now write the values of fields onto the prepared statement
        index = dehydrate( id, fields, rowId, includeProperty, propertyColumnUpdateable, j, update, session, index, true );

        // Write any appropriate versioning conditional parameters
View Full Code Here

      return;
    }

    final boolean useVersion = j == 0 && isVersioned();
    final boolean callable = isDeleteCallable( j );
    final Expectation expectation = Expectations.appropriateExpectation( deleteResultCheckStyles[j] );
    final boolean useBatch = j == 0 && isBatchable() && expectation.canBeBatched();
    if ( useBatch && deleteBatchKey == null ) {
      deleteBatchKey = new BasicBatchKey(
          getEntityName() + "#DELETE",
          expectation
      );
    }

    final boolean traceEnabled = LOG.isTraceEnabled();
    if ( traceEnabled ) {
      LOG.tracev( "Deleting entity: {0}", MessageHelper.infoString( this, id, getFactory() ) );
      if ( useVersion )
        LOG.tracev( "Version: {0}", version );
    }

    if ( isTableCascadeDeleteEnabled( j ) ) {
      if ( traceEnabled ) {
        LOG.tracev( "Delete handled by foreign key constraint: {0}", getTableName( j ) );
      }
      return; //EARLY EXIT!
    }

    try {
      //Render the SQL query
      PreparedStatement delete;
      int index = 1;
      if ( useBatch ) {
        delete = session.getTransactionCoordinator()
            .getJdbcCoordinator()
            .getBatch( deleteBatchKey )
            .getBatchStatement( sql, callable );
      }
      else {
        delete = session.getTransactionCoordinator()
            .getJdbcCoordinator()
            .getStatementPreparer()
            .prepareStatement( sql, callable );
      }

      try {

        index += expectation.prepare( delete );

        // Do the key. The key is immutable so we can use the _current_ object state - not necessarily
        // the state at the time the delete was issued
        getIdentifierType().nullSafeSet( delete, id, index, session );
        index += getIdentifierColumnSpan();
View Full Code Here

      if ( j == 0 && isVersioned() ) {
        log.trace( "Version: " + Versioning.getVersion( fields, this ) );
      }
    }

    Expectation expectation = Expectations.appropriateExpectation( insertResultCheckStyles[j] );
    boolean callable = isInsertCallable( j );
    // we can't batch joined inserts, *especially* not if it is an identity insert;
    // nor can we batch statements where the expectation is based on an output param
    final boolean useBatch = j == 0 && expectation.canBeBatched();
    try {

      // Render the SQL query
      final PreparedStatement insert;
      if ( useBatch ) {
        if ( callable ) {
          insert = session.getBatcher().prepareBatchCallableStatement( sql );
        }
        else {
          insert = session.getBatcher().prepareBatchStatement( sql );
        }
      }
      else {
        if ( callable ) {
          insert = session.getBatcher().prepareCallableStatement( sql );
        }
        else {
          insert = session.getBatcher().prepareStatement( sql );
        }
      }

      try {
        int index = 1;
        index += expectation.prepare( insert );

        // Write the values of fields onto the prepared statement - we MUST use the state at the time the
        // insert was issued (cos of foreign key constraints). Not necessarily the object's current state

        dehydrate( id, fields, null, notNull, propertyColumnInsertable, j, insert, session, index );

        if ( useBatch ) {
          // TODO : shouldnt inserts be Expectations.NONE?
          session.getBatcher().addToBatch( expectation );
        }
        else {
          expectation.verifyOutcome( insert.executeUpdate(), insert, -1 );
        }

      }
      catch ( SQLException sqle ) {
        if ( useBatch ) {
View Full Code Here

          final Object object,
          final String sql,
          final SessionImplementor session) throws HibernateException {

    final boolean useVersion = j == 0 && isVersioned();
    final Expectation expectation = Expectations.appropriateExpectation( updateResultCheckStyles[j] );
    final boolean callable = isUpdateCallable( j );
    final boolean useBatch = j == 0 && expectation.canBeBatched() && isBatchable(); //note: updates to joined tables can't be batched...

    if ( log.isTraceEnabled() ) {
      log.trace( "Updating entity: " + MessageHelper.infoString( this, id, getFactory() ) );
      if ( useVersion ) {
        log.trace( "Existing version: " + oldVersion + " -> New version: " + fields[getVersionProperty()] );
      }
    }

    try {

      int index = 1; // starting index
      final PreparedStatement update;
      if ( useBatch ) {
        if ( callable ) {
          update = session.getBatcher().prepareBatchCallableStatement( sql );
        }
        else {
          update = session.getBatcher().prepareBatchStatement( sql );
        }
      }
      else {
        if ( callable ) {
          update = session.getBatcher().prepareCallableStatement( sql );
        }
        else {
          update = session.getBatcher().prepareStatement( sql );
        }
      }

      try {

        index+= expectation.prepare( update );

        //Now write the values of fields onto the prepared statement
        index = dehydrate( id, fields, rowId, includeProperty, propertyColumnUpdateable, j, update, session, index );

        // Write any appropriate versioning conditional parameters
View Full Code Here

      return;
    }

    final boolean useVersion = j == 0 && isVersioned();
    final boolean callable = isDeleteCallable( j );
    final Expectation expectation = Expectations.appropriateExpectation( deleteResultCheckStyles[j] );
    final boolean useBatch = j == 0 && isBatchable() && expectation.canBeBatched();

    if ( log.isTraceEnabled() ) {
      log.trace( "Deleting entity: " + MessageHelper.infoString( this, id, getFactory() ) );
      if ( useVersion ) {
        log.trace( "Version: " + version );
      }
    }

    if ( isTableCascadeDeleteEnabled( j ) ) {
      if ( log.isTraceEnabled() ) {
        log.trace( "delete handled by foreign key constraint: " + getTableName( j ) );
      }
      return; //EARLY EXIT!
    }

    try {

      //Render the SQL query
      PreparedStatement delete;
      int index = 1;
      if ( useBatch ) {
        if ( callable ) {
          delete = session.getBatcher().prepareBatchCallableStatement( sql );
        }
        else {
          delete = session.getBatcher().prepareBatchStatement( sql );
        }
      }
      else {
        if ( callable ) {
          delete = session.getBatcher().prepareCallableStatement( sql );
        }
        else {
          delete = session.getBatcher().prepareStatement( sql );
        }
      }

      try {

        index += expectation.prepare( delete );

        // Do the key. The key is immutable so we can use the _current_ object state - not necessarily
        // the state at the time the delete was issued
        getIdentifierType().nullSafeSet( delete, id, index, session );
        index += getIdentifierColumnSpan();
View Full Code Here

   
    if ( ArrayHelper.isAllFalse(elementColumnIsSettable) ) return 0;

    try {
      PreparedStatement st = null;
      Expectation expectation = Expectations.appropriateExpectation( getUpdateCheckStyle() );
      boolean callable = isUpdateCallable();
      boolean useBatch = expectation.canBeBatched();
      Iterator entries = collection.entries( this );
      String sql = getSQLUpdateRowString();
      int i = 0;
      int count = 0;
      while ( entries.hasNext() ) {
        Object entry = entries.next();
        if ( collection.needsUpdating( entry, i, elementType ) ) {
          int offset = 1;

          if ( useBatch ) {
            if ( updateBatchKey == null ) {
              updateBatchKey = new BasicBatchKey(
                  getRole() + "#UPDATE",
                  expectation
              );
            }
            st = session.getTransactionCoordinator()
                .getJdbcCoordinator()
                .getBatch( updateBatchKey )
                .getBatchStatement( sql, callable );
          }
          else {
            st = session.getTransactionCoordinator()
                .getJdbcCoordinator()
                .getStatementPreparer()
                .prepareStatement( sql, callable );
          }

          try {
            offset+= expectation.prepare( st );
            int loc = writeElement( st, collection.getElement( entry ), offset, session );
            if ( hasIdentifier ) {
              writeIdentifier( st, collection.getIdentifier( entry, i ), loc, session );
            }
            else {
              loc = writeKey( st, id, loc, session );
              if ( hasIndex && !indexContainsFormula ) {
                writeIndexToWhere( st, collection.getIndex( entry, i, this ), loc, session );
              }
              else {
                writeElementToWhere( st, collection.getSnapshotElement( entry, i ), loc, session );
              }
            }

            if ( useBatch ) {
              session.getTransactionCoordinator()
                  .getJdbcCoordinator()
                  .getBatch( updateBatchKey )
                  .addToBatch();
            }
            else {
              expectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
            }
          }
          catch ( SQLException sqle ) {
            if ( useBatch ) {
              session.getTransactionCoordinator().getJdbcCoordinator().abortBatch();
View Full Code Here

      // Remove all the old entries

      try {
        int offset = 1;
        PreparedStatement st = null;
        Expectation expectation = Expectations.appropriateExpectation( getDeleteAllCheckStyle() );
        boolean callable = isDeleteAllCallable();
        boolean useBatch = expectation.canBeBatched();
        String sql = getSQLDeleteString();
        if ( useBatch ) {
          if ( removeBatchKey == null ) {
            removeBatchKey = new BasicBatchKey(
                getRole() + "#REMOVE",
                expectation
                );
          }
          st = session.getTransactionCoordinator()
              .getJdbcCoordinator()
              .getBatch( removeBatchKey )
              .getBatchStatement( sql, callable );
        }
        else {
          st = session.getTransactionCoordinator()
              .getJdbcCoordinator()
              .getStatementPreparer()
              .prepareStatement( sql, callable );
        }

        try {
          offset += expectation.prepare( st );

          writeKey( st, id, offset, session );
          if ( useBatch ) {
            session.getTransactionCoordinator()
                .getJdbcCoordinator()
                .getBatch( removeBatchKey )
                .addToBatch();
          }
          else {
            expectation.verifyOutcome( session.getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 );
          }
        }
        catch ( SQLException sqle ) {
          if ( useBatch ) {
            session.getTransactionCoordinator().getJdbcCoordinator().abortBatch();
View Full Code Here

TOP

Related Classes of org.hibernate.jdbc.Expectation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.