9.7. Generators

9.7.1. Runtime Access

The JPA Overview's Chapter 12, Mapping Metadata details using generators to automatically populate identity fields in JPA.

The JDO Overview demonstrates how to declare sequences in Chapter 15, Mapping Metadata. It describes JDO's javax.jdo.Sequence interface and how to obtain named sequences in JDO in Chapter 16, Sequence.

Kodo represents all generators internally with the kodo.kernel.Seq interface. This interface supplies all the context you need to create your own custom generators, including the current persistence environment, the JDBC DataSource, and other essentials. The kodo.jdbc.kernel.AbstractJDBCSeq helps you create custom JDBC-based sequences. Kodo also supplies the following built-in Seqs:

You can use JPA SequenceGenerators to describe any built-in Seqs or your own Seq implementation. Set the sequenceName attribute to a plugin string describing your choice. See Section 12.5, “Generators” in the JPA Overview for details on defining SequenceGenerators.

You can create named JDO sequences using any of the built-in Seqs or your own Seq implementation. Just set the sequence element's factory-class attribute to a plugin string describing your choice. See Section 15.3, “Sequences” in the JDO Overview for details on defining named sequences in mapping metadata.

See Section 2.4, “Plugin Configuration” for plugin string formatting.

Example 9.16. Named Seq Sequence

JPA:

@Entity
@Table(name="AUTO")
public class Author
{
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AuthorSeq")
    @SequenceGenerator(name="AuthorSeq" sequence="table(Table=AUTO_SEQ, Increment=100)")
    @Column(name="AID")
    private long id;
 
    ...
}

Note that if you want to use a plugin string without any arguments, you must still suffix the plugin type with () to differentiate it from a sequence name in the SequenceGenerator.sequence attribute:

@SequenceGenerator(name="AuthorSeq", sequence="table()")

JDO:

<?xml version="1.0"?>
<orm>
    <package name="org.mag.pub">
        <sequence name="AuthorSeq" factory-class="table(Table=AUTH_SEQ, Increment=100)"/>
        <class name="Author" table="AUTH">
            <datastore-identity sequence="AuthorSeq" column="AID"/>
            ...
        </class>
    </package>
</orm>

Kodo maintains a system sequence to generate datastore identity values for classes that do not declare a specific datastore identity strategy. You can configure the system sequence through the kodo.Sequence configuration property. This property accepts a plugin string describing a Seq instance.

Example 9.17. System Sequence Configuration

JPA XML format:

<property name="kodo.Sequence" value="table(Table=KODOSEQ, Increment=100)"/>

JDO properties format:

kodo.Sequence: table(Table=KODOSEQ, Increment=100)

In JPA, set your GeneratedValue annotation's strategy attribute to AUTO to use the configured system sequence. Or, because AUTO is the default strategy, use the annotation without attributes:

@GeneratedValue
private long id;

In JDO, set the sequence attribute of your datastore-identity or field element to system to use the system sequence:

<field name="id" primary-key="true" sequence="system"/>

9.7.1. Runtime Access

Kodo JPA allows you to access named generators at runtime through the OpenJPAEntityManager.getNamedGenerator method:

public Generator getNamedGenerator (String name);

The returned org.apache.openjpa.persistence.Generator is a facade over an internal Kodo Seq.

The OpenJPAEntityManager includes additional APIs to retrieve the identity generator of any class, or the generator of any field. With these APIs, you do not have to know the generator name. Additionally, they allow you to access the implicit generator used by default for datastore identity classes. See the Javadoc for the OpenJPAEntityManager.getIdentityGenerator and OpenJPAEntityManager.getFieldGenerator methods for API details.

The JDO Overview demonstrates how to obtain a sequence by name in Chapter 16, Sequence. The KodoJDOHelper includes additional APIs to retrieve the identity sequence of any class, or the value sequence of any field. With these APIs, you do not have to know the sequence name. Additionally, they allow you to access the implicit sequence used for a datastore identity class with the default native strategy, or the backing sequence of a field with a value strategy of uuid-hex or uuid-string.

See the Javadoc for the KodoJDOHelper.getIdentitySequence and KodoJDOHelper.getFieldSequence methods for API details. Note that all methods return instances of javax.jdo.Sequence to coincide with JDO's standard APIs; the Seq interface is only used internally.

 

Skip navigation bar   Back to Top