You have to write a Generator to satisfy this condition.
I did a similar one and maybe it would help
Code:
package com.yoyo.hibernate.idgen;
import com.yoyo.CounterPersist;
import com.yoyo.persistentfactory.CounterFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.dialect.Dialect;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.id.Configurable;
import net.sf.hibernate.id.PersistentIdentifierGenerator;
import net.sf.hibernate.type.Type;
import net.sf.hibernate.util.PropertiesHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Serializable;
import java.util.Properties;
/**
* Generates Sequence numbers for individual entities.
* A Separate table holds keys that correspond to entities and values that need to
* be incremented and returned for the next id. Based on the "Sequence Blocks" Pattern
* From EJBDesign Patterns.
*/
public class SequenceGenerator implements PersistentIdentifierGenerator, Configurable {
private static String KEY = "key";
private static String TABLE = "table";
/** Key - Name of the entity in the Counter Table **/
private String key;
/** Table - Name of the table that maintains key information **/
private String table;
private static final Log log = LogFactory.getLog(SequenceGenerator.class);
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
this.key = PropertiesHelper.getString(KEY, params, "");
this.table = PropertiesHelper.getString(TABLE, params, "T_COUNTER");
log.info("SequenceGenerator configured with key: " + this.key + " and table: " + this.table);
}
public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
CounterPersist counter = CounterFactory.getCounterPersistence("Hibernate");
int nextValue = counter.getCounter(this.key).getNextValue();
log.debug("Sequence ID generated: " + nextValue);
return new Integer(nextValue);
}
public String sqlDropString(Dialect dialect) throws HibernateException {
return "";
}
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
return new String[0];
}
public Object generatorKey() {
return "com.yoyo.hibernate.idgen.SequenceGenerator";
}
}
Use your own logic to get the sequence number in the generate method above.
Your Sequence generator in the code will be something like(using XDoclet)
/**
* @hibernate.id column="addressid"
* unsaved-value="null"
* generator-class="com.yoyo.hibernate.idgen.SequenceGenerator"
* @hibernate.generator-param name="table" value="T_COUNTER"
* @hibernate.generator-param name="key" value="Address"
* @return
*/
-Raj