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
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidcreateAutoPk(DataNode node, List<DbEntity> dbEntities) Generates necessary database objects to provide automatic primary key support.createAutoPkStatements(List<DbEntity> dbEntities) Creates a list of CREATE SEQUENCE statements for the list of DbEntities.protected StringvoiddropAutoPk(DataNode node, List<DbEntity> dbEntities) Drops PK sequences for all specified DbEntities.dropAutoPkStatements(List<DbEntity> dbEntities) Creates a list of DROP SEQUENCE statements for the list of DbEntities.protected StringReturns a SQL string needed to drop any database objects associated with automatic primary key generation process for a specific DbEntity.getExistingSequences(DataNode node) Fetches a list of existing sequences that might match Cayenne generated ones.protected Stringprotected longlongPkFromDatabase(DataNode node, DbEntity entity) Generates a primary key by calling the database sequence corresponding to thedbEntity.protected intpkCacheSize(DbEntity entity) protected abstract StringReturns a database-specific query that lists the names of all existing sequences.protected abstract StringselectNextValQuery(String pkGeneratingSequenceName) Returns a database-specific query to fetch the next value of the named sequence.protected StringsequenceName(DbEntity entity) Methods inherited from class JdbcPkGenerator
autoPkTableExists, dropAutoPkString, generatePk, getAdapter, getPkCacheSize, pkCreateString, pkDeleteString, pkSelectString, pkTableCreateString, pkUpdateString, reset, runUpdate, setPkCacheSizeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface PkGenerator
generatePk
-
Constructor Details
-
SequencePkGenerator
-
-
Method Details
-
createAutoPk
Description copied from interface:PkGeneratorGenerates necessary database objects to provide automatic primary key support.- Specified by:
createAutoPkin interfacePkGenerator- Overrides:
createAutoPkin classJdbcPkGenerator- Parameters:
node- node that provides access to a DataSource.dbEntities- a list of entities that require primary key auto-generation support
-
createAutoPkStatements
Creates a list of CREATE SEQUENCE statements for the list of DbEntities.- Specified by:
createAutoPkStatementsin interfacePkGenerator- Overrides:
createAutoPkStatementsin classJdbcPkGenerator
-
dropAutoPk
Drops PK sequences for all specified DbEntities.- Specified by:
dropAutoPkin interfacePkGenerator- Overrides:
dropAutoPkin classJdbcPkGenerator- Parameters:
node- node that provides access to a DataSource.dbEntities- a list of entities whose primary key auto-generation support should be dropped.
-
dropAutoPkStatements
Creates a list of DROP SEQUENCE statements for the list of DbEntities.- Specified by:
dropAutoPkStatementsin interfacePkGenerator- Overrides:
dropAutoPkStatementsin classJdbcPkGenerator
-
createSequenceString
-
dropSequenceString
-
selectNextValQuery
-
selectAllSequencesQuery
Returns a database-specific query that lists the names of all existing sequences. -
longPkFromDatabase
Generates a primary key by calling the database sequence corresponding to thedbEntity.- Overrides:
longPkFromDatabasein classJdbcPkGenerator- Since:
- 3.0
-
pkCacheSize
-
sequenceName
-
getSequencePrefix
-
getExistingSequences
-