Open Source Repository

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



org/hibernate/id/SequenceIdentityGenerator.java
package org.hibernate.id;

import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.id.insert.AbstractReturningDelegate;
import org.hibernate.id.insert.IdentifierGeneratingInsert;
import org.hibernate.dialect.Dialect;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.sql.Insert;
import org.hibernate.util.NamedGeneratedKeysHelper;
import org.hibernate.type.Type;
import org.hibernate.engine.SessionImplementor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

/**
 * A generator which combines sequence generation with immediate retrieval
 * through JDBC3 {@link java.sql.Connection#prepareStatement(String, String[]) getGeneratedKeys}.
 * In this respect it works much like ANSI-SQL IDENTITY generation.
 <p/>
 * This generator only known to work with newer Oracle drivers compiled for
 * JDK 1.4 (JDBC3).
 <p/>
 * Note: Due to a bug in Oracle drivers, sql comments on these insert statements
 * are completely disabled.
 *
 @author Steve Ebersole
 */
public class SequenceIdentityGenerator extends SequenceGenerator
    implements PostInsertIdentifierGenerator {

  private static final Log log = LogFactory.getLogSequenceIdentityGenerator.class );

  public Serializable generate(SessionImplementor s, Object obj) {
    return IdentifierGeneratorFactory.POST_INSERT_INDICATOR;
  }

  public InsertGeneratedIdentifierDelegate getInsertGeneratedIdentifierDelegate(
      PostInsertIdentityPersister persister,
          Dialect dialect,
          boolean isGetGeneratedKeysEnabledthrows HibernateException {
    return new Delegatepersister, dialect, getSequenceName() );
  }

  public void configure(Type type, Properties params, Dialect dialectthrows MappingException {
    super.configuretype, params, dialect );
  }

  public static class Delegate extends AbstractReturningDelegate {
    private final Dialect dialect;
    private final String sequenceNextValFragment;
    private final String[] keyColumns;

    public Delegate(PostInsertIdentityPersister persister, Dialect dialect, String sequenceName) {
      superpersister );
      this.dialect = dialect;
      this.sequenceNextValFragment = dialect.getSelectSequenceNextValStringsequenceName );
      this.keyColumns = getPersister().getRootTableKeyColumnNames();
      if keyColumns.length > ) {
        throw new HibernateException"sequence-identity generator cannot be used with with multi-column keys" );
      }
    }

    public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() {
      NoCommentsInsert insert = new NoCommentsInsertdialect );
      insert.addColumngetPersister().getRootTableKeyColumnNames()[0], sequenceNextValFragment );
      return insert;
    }

    protected PreparedStatement prepare(String insertSQL, SessionImplementor sessionthrows SQLException {
      return session.getBatcher().prepareStatementinsertSQL, keyColumns );
    }

    protected Serializable executeAndExtract(PreparedStatement insertthrows SQLException {
      insert.executeUpdate();
      return IdentifierGeneratorFactory.getGeneratedIdentity(
          NamedGeneratedKeysHelper.getGeneratedKeyinsert ),
              getPersister().getIdentifierType()
      );
    }
  }

  public static class NoCommentsInsert extends IdentifierGeneratingInsert {
    public NoCommentsInsert(Dialect dialect) {
      superdialect );
    }

    public Insert setComment(String comment) {
      // don't allow comments on these insert statements as comments totally
      // blow up the Oracle getGeneratedKeys "support" :(
      log.info"disallowing insert statement comment for select-identity due to Oracle driver bug" );
      return this;
    }
  }
}