Monday, June 13, 2016

Oracle® Weblogic 12.2.1: How to implement a custom UUID in for Toplink EJB 3.0

To implement a custom sequence strategy you can subclass org.eclipse.persistence.sequencing.Sequenceor StandardSequence 

StandardSequence assumes a numeric value being used so Sequence will be used for this example to return a random UUID value.

Implementation of UUIDSequence
                                                             
package com.mindtelligent.worklist.model;


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
public void onConnect() {
}

@Override
public void onDisconnect() {
}

@Override
public boolean shouldAcquireValueAfterInsert() {
return false;
}

 

@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


Add a property to persistence.xml

 <property name="eclipselink.session.customizer" value="com.mindtelligent.worklist.model.UUIDSequence"/> 

 

                                                             

Change the EJB 3.0 Class 



@Id
@GeneratedValue(generator="system-uuid")    
@Column(name="ADDRESS_ID", nullable = false, length = 50)


Where ADDRESS_ID is the Primary Key of the table

No comments:

Post a Comment

SOA Cloud 12c GIT Repository

When a project is created, you can choose to initialize the project with one project Git repository for the project. A Maven repository is a...