Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Examples/JPA/CustomSequencing"
(→Using the Sequence) |
m |
||
Line 1: | Line 1: | ||
[[Category:EclipseLink/Example/JPA|CustomSequencing]] | [[Category:EclipseLink/Example/JPA|CustomSequencing]] | ||
+ | |||
+ | =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. | This example illustrates how to configure custom sequencing using a UUID generator. The configuration shown is specific to EclipseLink JPA. |
Revision as of 11:42, 8 February 2011
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.
The solution involves:
- Implementing a custom Sequence
- Registering the sequence using a SessionCustomizer
- 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>