Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Problem defining custom Sequence.

Hi folks,

i followed the steps definied here: http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing

With the difference that i SessionCustomizer is registered using the persistence.xml editor of eclipse galileo.But the customSequence mechanism still doesn't work.

Here are the both class involved

@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@OptimisticLocking(type=OptimisticLockingType.ALL_COLUMNS)
public abstract class DomainObject implements Serializable {

/**
*/
private static final long serialVersionUID = -3066404192344014658L;
@Id
@GeneratedValue(generator = "ATSKEY_CUSTOM_GENERATOR")
@SequenceGenerator(name = "ATSKEY_CUSTOM_GENERATOR")
private Integer id;
        ...
        ...
}


@Entity
@Table(name = "USER_TABLE")
@AttributeOverride(name = "id", column = @Column(name = "USER_ID"))
@NamedQuery(name = "FIND_BY_USER_NAME", query = "SELECT o FROM User o WHERE o.userName = :userName")
public class User extends DomainObject {
       -----
}

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

Back to the top