6.10. Primary Key Generation

6.10.1. Using table-based key generation
6.10.2. Using Database Sequences for key generation

Kodo provides various mechanisms to control how primary keys are generated when using datastore identity. The control of how keys are obtained if done via the com.solarmetric.kodo.impl.jdbc.SequenceFactory implementation specified by the com.solarmetric.kodo.impl.jdbc.SequenceFactoryClass property, and parameters of the SequenceFactory are controlled via the com.solarmetric.kodo.impl.jdbc.SequenceFactoryProperties property.

Using one of the vaious SequenceFactory implementations allows coarse-grained control over identity assignment while maintaining the simplicity of using datastore identity. Furthermore, it is possible to use the SequenceFactory interface to make default assignments to the fields of application identity instances. This enables the utilization of the fine-grained controls over identity that is only possible when using application identity, and still retaining the ability to interact with the database for reliable unique sequence generation. Sequence numbers for application identity classes can be obtained by invoking JDBCConfiguration.html.getSequenceFactory.

6.10.1. Using table-based key generation

By default, Kodo uses a single table to hold the current numeric sequence value for assigning primary keys using the com.solarmetric.kodo.impl.jdbc.schema.DBSequenceFactory SequenceFactory implementation. The name of the table is, by default, JDO_SEQUENCE; this can be changed using the TableName parameter of the DBSequenceFactory. When a sequence number is first needed for assignment to an object identity instance the a newly persistent object, the DBSequenceFactory will obtain the current value of the sequence table and increment it by the value specified by the Increment parameter of the the DBSequenceFactory. Kodo will then use this pool of sequence numbers to assign values to new persistent instances.

Example 6.8. Properties for configuring DBSequenceFactory

com.solarmetric.kodo.impl.jdbc.SequenceFactoryClass=\
    com.solarmetric.kodo.impl.jdbc.schema.DBSequenceFactory
# The table "MY_SEQUENCE_TABLE" will be used for holding the
# sequence values. A pool of 1,000 sequences will be "checked out" at a
# time, which is suitable for large bulk inserts.
com.solarmetric.kodo.impl.jdbc.SequenceFactoryProperties=\
    TableName=MY_SEQUENCE_TABLE \
    Increment=1000

6.10.2. Using Database Sequences for key generation

Most databases support a "native sequence" function, which is a built-in mechanism for obtaining incrementing numbers from a "sequence". For example, in Oracle, a database sequence can be created with a statement like "create sequence mysequence". Sequence values can then be simultaneously obtained and incremented with the statement "select mysequence.nextval from dual". Kodo provides support for this common mechanism of sequence assignment with the com.solarmetric.kodo.impl.jdbc.schema.ClassSequenceFactory SequenceFactory implementation. Sequence names are defined on a per-class basis in the metadata for the persistent class using the sequence class-level metadata extension.

Example 6.9. Properties for configuring ClassSequenceFactory

com.solarmetric.kodo.impl.jdbc.SequenceFactoryClass=\
    com.solarmetric.kodo.impl.jdbc.schema.ClassSequenceFactory
# Set the name of the table from which we will be obtaining sequences
com.solarmetric.kodo.impl.jdbc.SequenceFactoryProperties=TableName=DUAL

Example 6.10. Metadata for configuring DBSequenceFactory

<?xml version="1.0"?>
<jdo>
  <package name="com.mycompany">
      <class name="MyPersistenceClass">
        <extension vendor-name="kodo" key="sequence" value="my_sequence_name"/>
      </class>
  </package>
</jdo>