Difference between revisions of "EclipseLink/Examples/JPA/CustomSequencing"

From Eclipsepedia

Jump to: navigation, search
m
(How to Configure Custom Sequencing)
Line 4: Line 4:
  
 
This example illustrates how to configure custom sequencing using a UUID generator. The configuration shown is specific to EclipseLink JPA.
 
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 <code>UuiGenerator</code> as of 2.4, see [http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_uuidgenerator.htm#CFAFIIFC @UuidGenerator].
  
 
The solution involves:
 
The solution involves:

Revision as of 08:52, 4 April 2013


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

Contents


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() {
		super();
	}
 
	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>