Dear friend, I just want to share with you what I've done with Oracle
Custom Sequences. The problem was that I wanted to have a different
sequence for each table with no cache so as to overcome the problem
of "sequence gaps" (see
http://www.techonthenet.com/oracle/sequences.php).
So I read the article that you mention and I did the following:
1) I've created a dialect which extends the Oracle9Dialect and
2) I've created a Generator which extends the SequenceGenerator.
Basically I did what the last part of the article said.
The code is :
Code:
public class MyDialect extends Oracle9Dialect {
public Class getNativeIdentifierGeneratorClass() {
return NonCachingSequenceGenerator.class;
}
}
public class NonCachingSequenceGenerator extends SequenceGenerator {
/**
* The longest allowable sequence name length: 30 for oracle.
*/
protected int SEQUENCE_NAME_LENGTH = 30;
/**
* Additional parameters for the DDL of the sequence.
*/
protected static final String DDL_PARAMETERS = "nocache";
private static final Log log = LogFactory.getLog(NonCachingSequenceGenerator.class);
/**
* If the parameters do not contain a {@link SequenceGenerator#SEQUENCE} name, we
* assign one based on the table name.
*/
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
params.setProperty(PARAMETERS, DDL_PARAMETERS);
if(params.getProperty(SEQUENCE) == null || params.getProperty(SEQUENCE).length() == 0) {
String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
if(tableName != null) {
String tmpSeqName = "S_" + tableName;
String seqName;
if(tmpSeqName.length() <= SEQUENCE_NAME_LENGTH){
seqName = tmpSeqName;
}
else{
seqName = tmpSeqName.substring(0, SEQUENCE_NAME_LENGTH);
log.warn(tmpSeqName + " truncated to " + seqName
+ ". This may collide with another sequence already in the database.");
}
params.setProperty(SEQUENCE, seqName);
}
}
super.configure(type, params, dialect);
}
}
So what i get is a sequence for each table with maximum 30 char length
with no cache in order to avoid gaps. In adition all i had to do is in the
configuration file to declare which dialect to use and still preserve the "native" attribute in the hbm.
Any thoughts or suggestions are welcomed.