Skip to main content
Jump to: navigation, search

EclipseLink/Examples/JPA/CustomSequencing

< EclipseLink‎ | Examples‎ | JPA
Revision as of 13:14, 15 July 2008 by Douglas.clarke.oracle.com (Talk | contribs) (New page: This example illustrates how to configure custom sequencing using a UUID generator. The configuration shown is specific to EclipseLink JPA. The solution involves: # Implementing a custom ...)

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

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

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.

UUIDSequence

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(String name) {
		super(name);
	}
 
	@Override
	public Object getGeneratedValue(Accessor accessor,
			AbstractSession writeSession, String seqName) {
		return UUID.randomUUID().toString().toUpperCase();
	}
 
	@Override
	public Vector getGeneratedVector(Accessor accessor,
			AbstractSession writeSession, String seqName, int size) {
		return null;
	}
 
	@Override
	protected void onConnect() {
	}
 
	@Override
	protected void onDisconnect() {
	}
 
	@Override
	public boolean shouldAcquireValueAfterInsert() {
		return false;
	}
 
	@Override
	public boolean shouldOverrideExistingValue(String seqName,
			Object existingValue) {
		return existingValue == null || ((String) existingValue).isEmpty();
	}
 
	@Override
	public boolean shouldUseTransaction() {
		return false;
	}
 
	@Override
	public boolean shouldUsePreallocation() {
		return false;
	}
 
	public void customize(Session session) throws Exception {
		UUIDSequence sequence = new UUIDSequence("system-uuid");
 
		session.getLogin().addSequence(sequence);
	}
 
}

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:

		properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER,
				"eclipselink.example.UUIDSequence");

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:

	@Id
	@GeneratedValue(generator="system-uuid"/>
	@Column(name="PROJ_ID")
	private int id;

or you can configure it in your ORM XML:

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

Back to the top