Jump to: navigation, search


< EclipseLink‎ | Examples‎ | JPA
Revision as of 08:52, 4 April 2013 by James.sutherland.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to Configure Custom Sequencing

This example illustrates how to configure custom sequencing using a UUID generator. The configuration shown is specific to EclipseLink JPA.

Note: EclipseLink now supports a UuiGenerator as of 2.4, see @UuidGenerator.

The solution involves:

  1. Implementing a custom Sequence
  2. Registering the sequence using a SessionCustomizer
  3. Using the named sequence in your entity classes

Implement Custom Sequence

To implement a custom sequence strategy you can subclass Sequence or StandardSequence. StandardSequence assumes a numeric value being used so Sequence will be used for this example to return a random UUID value.


package eclipselink.example;
import java.util.UUID;
import java.util.Vector;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.sequencing.Sequence;
import org.eclipse.persistence.sessions.Session;
public class UUIDSequence extends Sequence implements SessionCustomizer {
	public UUIDSequence() {
	public UUIDSequence(String name) {
	public Object getGeneratedValue(Accessor accessor,
			AbstractSession writeSession, String seqName) {
		return UUID.randomUUID().toString().toUpperCase();
	public Vector getGeneratedVector(Accessor accessor,
			AbstractSession writeSession, String seqName, int size) {
		return null;
	protected void onConnect() {
	protected void onDisconnect() {
	public boolean shouldAcquireValueAfterInsert() {
		return false;
	public boolean shouldOverrideExistingValue(String seqName,
			Object existingValue) {
		return ((String) existingValue).isEmpty();
	public boolean shouldUseTransaction() {
		return false;
	public boolean shouldUsePreallocation() {
		return false;
	public void customize(Session session) throws Exception {
		UUIDSequence sequence = new UUIDSequence("system-uuid");

Registering Sequence

To register your custom Sequence implementation you can use a SessionCustomizer. The above implementation of UUIDSequence implements the customizer interface. This customizer can then be specified as a persistence unit property in API:


or in XML:

		<property name="eclipselink.session.customizer" value="eclipselink.example.UUIDSequence"/>

Using the Sequence

Now you can use this named sequence in your domain model with annotations:

private int id;

or you can configure it in your ORM XML:

<id name="id">
	<column name="PROJ_ID" />
	<generated-value generator="system-uuid"/>