Skip to main content

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

Try deriving from QuerySequence instead, something like:
 
public class ATSKeyCustomSequence extends QuerySequence 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, String tablename, String idColumnName) {
  super(name, 1, false, false);
  shouldSkipUpdate = true;
  this.tablename = tablename;
  this.idColumnName = idColumnName;
}
 
@Override
public void customize(Session session) throws Exception {
System.out.println("customizing session: " + session.getClass().getName());
// add a sequence for each sequence generator you want to override
ATSKeyCustomSequence sequence = new ATSKeyCustomSequence(
"ATSKEY_CUSTOM_GENERATOR", "USER_TABLE", "USER_ID");
session.getLogin().addSequence(sequence);
}
 
protected ValueReadQuery buildSelectQuery() {
  ValueReadQuery query = new ValueReadQuery();
  query.setSQLString(String.format(SQL_MAX_ID, idColumnName, tableName));
  return query;
}
 
protected Object select(Accessor accessor, AbstractSession writeSession, String seqName, Integer size) {
  Integer value = (Integer)super.(accessor, writeSession, seqName, size);
  return value + 1;
}
 
}
 
Note that the class implements the same functionality as the original class - and I am not sure it's a very functional one.
For instance, what would happen if you create more than one object in transaction before writing them out into the db - it seems that all of them will get the same sequence number.
 
Hope that helps,
 
Andrei
----- Original Message -----
Sent: Thursday, August 27, 2009 8:11 AM
Subject: [eclipselink-users] Problem defining custom Sequence.

Hi folks,


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


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top