6.  Generators

6.1. Runtime Access

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

OpenJPA represents all generators internally with the org.apache.openjpa.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 org.apache.openjpa.jdbc.kernel.AbstractJDBCSeq helps you create custom JDBC-based sequences. OpenJPA 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.

If specifying your own class name, you must include parentheses at the end of the class name, even if you have no plugin properties to configure. (E.g., sequenceName="com.example.SeqImpl()".

See Section 5, “ Generators ” in the JPA Overview for details on defining SequenceGenerators.

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

Example 9.8.  Named Seq Sequence

@Entity
@Table(name="AUTO")
public class Author {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AuthorSeq")
    @SequenceGenerator(name="AuthorSeq", sequenceName="table(Table=AUTO_SEQ)", allocationSize=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.sequenceName attribute:

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

OpenJPA 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 openjpa.Sequence configuration property. This property accepts a plugin string describing a Seq instance.

Example 9.9.  System Sequence Configuration

<property name="openjpa.Sequence" value="table(Table=OPENJPASEQ, 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;

6.1.  Runtime Access

OpenJPA 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 OpenJPA 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.