@NamedQuery(name = "FIND_BY_USER_NAME", query = "SELECT o FROM User o
WHERE o.userName = :userName")
Here is the customized Sequence.
/**
*
*/
package
com.ids_scheer.atskey.persistence.jpa.eclipselink.customizer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
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;
/**
* @author ROLA
*
*/
public class ATSKeyCustomSequence extends Sequence implements
SessionCustomizer {
/**
*
*/
private static
final long serialVersionUID = 6063793974472021257L;
protected
static final String SQL_MAX_ID = "SELECT MAX(%s) FROM %s";
public
ATSKeyCustomSequence() {
super();
System.out.println("instatiating sequence");
}
public
ATSKeyCustomSequence(String name) {
super(name);
}
/*
*
(non-Javadoc)
*
* @see
*
org.eclipse.persistence.sequencing.Sequence#getGeneratedValue(org.eclipse
*
.persistence.internal.databaseaccess.Accessor,
*
org.eclipse.persistence.internal.sessions.AbstractSession,
*
java.lang.String)
*/
@Override
public Object
getGeneratedValue(Accessor accessor, AbstractSession session,
String name)
{
String
tableName = null, idColumnName = null;
// identify
the name of table and the corresponding idColumnName
// depending
on the passed sequence
if
("USER_SEQ".equals(name)) {
tableName =
"USER_TABLE";
idColumnName =
"USER_ID";
}
// get the
maxId only if both the tableName and the idColumnName are
//
setted
if (tableName
!= null && idColumnName != null) {
try {
int maxId =
getMaxId(idColumnName, tableName, accessor
.getConnection());
maxId++;
return
maxId;
} catch
(SQLException e) {
e.printStackTrace();
}
}
return
null;
}
/*
*
(non-Javadoc)
*
* @see
*
org.eclipse.persistence.sequencing.Sequence#getGeneratedVector(org.eclipse
*
.persistence.internal.databaseaccess.Accessor,
*
org.eclipse.persistence.internal.sessions.AbstractSession,
*
java.lang.String, int)
*/
@SuppressWarnings("unchecked")
@Override
public Vector
getGeneratedVector(Accessor arg0, AbstractSession arg1,
String arg2,
int arg3) {
// TODO
Auto-generated method stub
return
null;
}
/*
*
(non-Javadoc)
*
* @see
org.eclipse.persistence.sequencing.Sequence#onConnect()
*/
@Override
protected void
onConnect() {
}
/*
*
(non-Javadoc)
*
* @see
org.eclipse.persistence.sequencing.Sequence#onDisconnect()
*/
@Override
protected void
onDisconnect() {
}
/*
*
(non-Javadoc)
*
* @see
*
org.eclipse.persistence.sequencing.Sequence#shouldAcquireValueAfterInsert
* ()
*/
@Override
public boolean
shouldAcquireValueAfterInsert() {
return
false;
}
/*
*
(non-Javadoc)
*
* @see
org.eclipse.persistence.sequencing.Sequence#shouldUseTransaction()
*/
@Override
public boolean
shouldUseTransaction() {
return
false;
}
@Override
public boolean
shouldUsePreallocation() {
return
false;
}
@Override
public void
customize(Session session) throws Exception {
System.out.println("customizing session: " +
session.getClass().getName());
ATSKeyCustomSequence sequence = new
ATSKeyCustomSequence(
"ATSKEY_CUSTOM_GENERATOR");
session.getLogin().addSequence(sequence);
}
/**
*
* @param
idColumn
* @param
table
* @return the
maxId in the given table
* @throws
SQLException
*/
protected int
getMaxId(String idColumn, String table, Connection connection)
throws
SQLException {
// build the
query
String
sqlQuery = String.format(SQL_MAX_ID, idColumn, table);
// get the
current max id
ResultSet
resultSet = connection.createStatement().executeQuery(
(sqlQuery));
// read out
the result
if
(resultSet.next()) {
return
Integer.parseInt(resultSet.getString(1));
}
return
-1;
}
}
Any help will be appreciated
Rodrigue