Open Source Repository

Home /hibernate/hibernate-3.2.6.ga | Repository Home



org/hibernate/id/insert/AbstractReturningDelegate.java
package org.hibernate.id.insert;

import org.hibernate.id.PostInsertIdentityPersister;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.pretty.MessageHelper;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Abstract InsertGeneratedIdentifierDelegate implementation where the
 * underlying strategy causes the enerated identitifer to be returned as an
 * effect of performing the insert statement.  Thus, there is no need for an
 * additional sql statement to determine the generated identitifer.
 *
 @author Steve Ebersole
 */
public abstract class AbstractReturningDelegate implements InsertGeneratedIdentifierDelegate {
  private final PostInsertIdentityPersister persister;

  public AbstractReturningDelegate(PostInsertIdentityPersister persister) {
    this.persister = persister;
  }

  public final Serializable performInsert(String insertSQL, SessionImplementor session, Binder binder) {
    try {
      // prepare and execute the insert
      PreparedStatement insert = prepareinsertSQL, session );
      try {
        binder.bindValuesinsert );
        return executeAndExtractinsert );
      }
      finally {
        releaseStatementinsert, session );
      }
    }
    catch SQLException sqle ) {
      throw JDBCExceptionHelper.convert(
          session.getFactory().getSQLExceptionConverter(),
              sqle,
              "could not insert: " + MessageHelper.infoStringpersister ),
              insertSQL
        );
    }
  }

  protected PostInsertIdentityPersister getPersister() {
    return persister;
  }

  protected abstract PreparedStatement prepare(String insertSQL, SessionImplementor sessionthrows SQLException;

  protected abstract Serializable executeAndExtract(PreparedStatement insertthrows SQLException;

  protected void releaseStatement(PreparedStatement insert, SessionImplementor sessionthrows SQLException {
    session.getBatcher().closeStatementinsert );
  }
}