OraclePkGenerator (cayenne-doc: Cayenne Documentation 5.0-M2 API)
Description: declaration: package: org.apache.cayenne.dba.oracle, class: OraclePkGenerator

# Class OraclePkGenerator

java.lang.Object
org.apache.cayenne.dba.JdbcPkGenerator
org.apache.cayenne.dba.oracle.OraclePkGenerator

All Implemented Interfaces:
  `PkGenerator`

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

public class OraclePkGenerator extends JdbcPkGenerator
Sequence-based primary key generator implementation for Oracle. Uses Oracle sequences to generate primary key values. This approach is at least 50% faster when tested with Oracle compared to the lookup table approach.

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

    dataNode.getAdapter().getPkGenerator().setPkCacheSize(1);

- ## Field Summary

  ### Fields inherited from class JdbcPkGenerator

  `adapter, DEFAULT_PK_CACHE_SIZE, pkCache, pkCacheSize, pkStartValue`
  Modifier and Type
  Field
  Description
  `protected JdbcAdapter`
  `adapter`
  `static final int`
  `DEFAULT_PK_CACHE_SIZE`
  `protected ConcurrentMap<String, Queue<Long>>`
  `pkCache`
  `protected int`
  `pkCacheSize`
  `protected long`
  `pkStartValue`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  ``
  `OraclePkGenerator()`
  Used by DI
  `protected`
  `OraclePkGenerator(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 primary key by calling Oracle sequence corresponding to the `dbEntity`.
  `protected int`
  `pkCacheSize(DbEntity entity)`
  `protected String`
  `selectAllSequencesQuery()`
  `protected String`
  `selectNextValQuery(String pkGeneratingSequenceName)`
  `protected String`
  `sequenceName(DbEntity entity)`
  Returns expected primary key sequence name for a DbEntity.

  ### Methods inherited from class JdbcPkGenerator

  `autoPkTableExists, dropAutoPkString, generatePk, getAdapter, getPkCacheSize, pkCreateString, pkDeleteString, pkSelectString, pkTableCreateString, pkUpdateString, reset, runUpdate, setAdapter, setPkCacheSize`
  Modifier and Type
  Method
  Description
  `protected boolean`
  `autoPkTableExists(DataNode node)`
  Checks if AUTO_PK_TABLE already exists in the database.
  `protected String`
  `dropAutoPkString()`
  `Object`
  `generatePk(DataNode node, DbAttribute pk)`
  Generates a unique and non-repeating primary key for specified dbEntity.
  `JdbcAdapter`
  `getAdapter()`
  Get an adapter associated with current PkGenerator
  `int`
  `getPkCacheSize()`
  Returns a size of the entity primary key cache.
  `protected String`
  `pkCreateString(String entName)`
  `protected String`
  `pkDeleteString(List<DbEntity> dbEntities)`
  `protected String`
  `pkSelectString(String entName)`
  `protected String`
  `pkTableCreateString()`
  `protected String`
  `pkUpdateString(String entName)`
  `void`
  `reset()`
  Resets any cached primary keys forcing generator to go to the database next time id generation is requested.
  `int`
  `runUpdate(DataNode node, String sql)`
  Runs JDBC update over a Connection obtained from DataNode.
  `void`
  `setAdapter(DbAdapter adapter)`
  Install the adapter associated with current PkGenerator
  `void`
  `setPkCacheSize(int pkCacheSize)`
  Sets the size of the entity primary key cache.

  ### Methods inherited from class Object

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

- ## Constructor Details

  - ### OraclePkGenerator

    public OraclePkGenerator()
    Used by DI
    Since:
      4.1

  - ### OraclePkGenerator

    protected OraclePkGenerator(JdbcAdapter adapter)

- ## Method Details

  - ### createAutoPk

    public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception
    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
    Throws:
      `Exception`

  - ### 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) throws Exception
    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.
    Throws:
      `Exception`

  - ### 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 String selectNextValQuery(String pkGeneratingSequenceName)

  - ### selectAllSequencesQuery

    protected String selectAllSequencesQuery()

  - ### longPkFromDatabase

    protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception
    Generates primary key by calling Oracle sequence corresponding to the `dbEntity`. Executed SQL looks like this:

          SELECT pk_table_name.nextval FROM DUAL

    Overrides:
      `longPkFromDatabase` in class `JdbcPkGenerator`
    Throws:
      `Exception`
    Since:
      3.0

  - ### pkCacheSize

    protected int pkCacheSize(DbEntity entity)

  - ### sequenceName

    protected String sequenceName(DbEntity entity)
    Returns expected primary key sequence name for a DbEntity.

  - ### getSequencePrefix

    protected String getSequencePrefix()

  - ### getExistingSequences

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