# Class SequencePkGenerator

java.lang.Object
org.apache.cayenne.dba.JdbcPkGenerator
org.apache.cayenne.dba.SequencePkGenerator

All Implemented Interfaces:
  `PkGenerator`

Direct Known Subclasses:
  `DB2PkGenerator, DerbyPkGenerator, H2PkGenerator, IngresPkGenerator, OraclePkGenerator, PostgresPkGenerator`

public abstract class SequencePkGenerator extends JdbcPkGenerator
A common superclass for sequence-based primary key generators. Uses database sequences to generate primary key values, which is generally faster than the lookup table approach implemented by `JdbcPkGenerator`. Concrete subclasses provide database-specific SQL for selecting the next sequence value and for discovering existing sequences.

When using the Cayenne key caching mechanism, make sure that sequences in the database have "INCREMENT BY" greater or equal to the generator "pkCacheSize" property value. If this is not the case, you will need to adjust the PkGenerator value accordingly. For example when a sequence is incremented by 1 each time, use the following code:

    dataNode.getPkGenerator().setPkCacheSize(1);

Since:
  5.0

- ## Field Summary

  ### Fields inherited from class JdbcPkGenerator

  `adapter, DEFAULT_PK_CACHE_SIZE, pkCache, pkCacheSize, pkStartValue`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `SequencePkGenerator(JdbcAdapter adapter)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `createAutoPk(DataNode node, List<DbEntity> dbEntities)`
  Generates necessary database objects to provide automatic primary key support.
  `List<String>`
  `createAutoPkStatements(List<DbEntity> dbEntities)`
  Creates a list of CREATE SEQUENCE statements for the list of DbEntities.
  `protected String`
  `createSequenceString(DbEntity ent)`
  `void`
  `dropAutoPk(DataNode node, List<DbEntity> dbEntities)`
  Drops PK sequences for all specified DbEntities.
  `List<String>`
  `dropAutoPkStatements(List<DbEntity> dbEntities)`
  Creates a list of DROP SEQUENCE statements for the list of DbEntities.
  `protected String`
  `dropSequenceString(DbEntity ent)`
  Returns a SQL string needed to drop any database objects associated with automatic primary key generation process for a specific DbEntity.
  `protected List<String>`
  `getExistingSequences(DataNode node)`
  Fetches a list of existing sequences that might match Cayenne generated ones.
  `protected String`
  `getSequencePrefix()`
  `protected long`
  `longPkFromDatabase(DataNode node, DbEntity entity)`
  Generates a primary key by calling the database sequence corresponding to the `dbEntity`.
  `protected int`
  `pkCacheSize(DbEntity entity)`
  `protected abstract String`
  `selectAllSequencesQuery()`
  Returns a database-specific query that lists the names of all existing sequences.
  `protected abstract String`
  `selectNextValQuery(String pkGeneratingSequenceName)`
  Returns a database-specific query to fetch the next value of the named sequence.
  `protected String`
  `sequenceName(DbEntity entity)`

  ### Methods inherited from class JdbcPkGenerator

  `autoPkTableExists, dropAutoPkString, generatePk, getAdapter, getPkCacheSize, pkCreateString, pkDeleteString, pkSelectString, pkTableCreateString, pkUpdateString, reset, runUpdate, setPkCacheSize`

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface PkGenerator

  `generatePk`

- ## Constructor Details

  - ### SequencePkGenerator

    protected SequencePkGenerator(JdbcAdapter adapter)

- ## Method Details

  - ### createAutoPk

    public void createAutoPk(DataNode node, List<DbEntity> dbEntities)
    Description copied from interface: `PkGenerator`
    Generates necessary database objects to provide automatic primary key support.
    Specified by:
      `createAutoPk` in interface `PkGenerator`
    Overrides:
      `createAutoPk` in class `JdbcPkGenerator`
    Parameters:
      `node` - node that provides access to a DataSource.
      `dbEntities` - a list of entities that require primary key auto-generation support

  - ### createAutoPkStatements

    public List<String> createAutoPkStatements(List<DbEntity> dbEntities)
    Creates a list of CREATE SEQUENCE statements for the list of DbEntities.
    Specified by:
      `createAutoPkStatements` in interface `PkGenerator`
    Overrides:
      `createAutoPkStatements` in class `JdbcPkGenerator`

  - ### dropAutoPk

    public void dropAutoPk(DataNode node, List<DbEntity> dbEntities)
    Drops PK sequences for all specified DbEntities.
    Specified by:
      `dropAutoPk` in interface `PkGenerator`
    Overrides:
      `dropAutoPk` in class `JdbcPkGenerator`
    Parameters:
      `node` - node that provides access to a DataSource.
      `dbEntities` - a list of entities whose primary key auto-generation support should be dropped.

  - ### dropAutoPkStatements

    public List<String> dropAutoPkStatements(List<DbEntity> dbEntities)
    Creates a list of DROP SEQUENCE statements for the list of DbEntities.
    Specified by:
      `dropAutoPkStatements` in interface `PkGenerator`
    Overrides:
      `dropAutoPkStatements` in class `JdbcPkGenerator`

  - ### createSequenceString

    protected String createSequenceString(DbEntity ent)

  - ### dropSequenceString

    protected String dropSequenceString(DbEntity ent)
    Returns a SQL string needed to drop any database objects associated with automatic primary key generation process for a specific DbEntity.

  - ### selectNextValQuery

    protected abstract String selectNextValQuery(String pkGeneratingSequenceName)
    Returns a database-specific query to fetch the next value of the named sequence.

  - ### selectAllSequencesQuery

    protected abstract String selectAllSequencesQuery()
    Returns a database-specific query that lists the names of all existing sequences.

  - ### longPkFromDatabase

    protected long longPkFromDatabase(DataNode node, DbEntity entity)
    Generates a primary key by calling the database sequence corresponding to the `dbEntity`.
    Overrides:
      `longPkFromDatabase` in class `JdbcPkGenerator`
    Since:
      3.0

  - ### pkCacheSize

    protected int pkCacheSize(DbEntity entity)

  - ### sequenceName

    protected String sequenceName(DbEntity entity)

  - ### getSequencePrefix

    protected String getSequencePrefix()

  - ### getExistingSequences

    protected List<String> getExistingSequences(DataNode node)
    Fetches a list of existing sequences that might match Cayenne generated ones.