Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/JPA/CustomSequencing

< EclipseLink‎ | Examples‎ | JPA
Revision as of 13:28, 15 July 2008 by Andrei.ilitchev.oracle.com (Talk | contribs) (UUIDSequence)

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 ((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