1 package org.hibernate.id; 3 4 import java.io.Serializable ; 5 import java.util.Properties ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.hibernate.HibernateException; 10 import org.hibernate.MappingException; 11 import org.hibernate.dialect.Dialect; 12 import org.hibernate.engine.SessionImplementor; 13 import org.hibernate.type.Type; 14 import org.hibernate.util.PropertiesHelper; 15 16 31 public class SequenceHiLoGenerator extends SequenceGenerator { 32 33 public static final String MAX_LO = "max_lo"; 34 35 private static final Log log = LogFactory.getLog(SequenceHiLoGenerator.class); 36 37 private int maxLo; 38 private int lo; 39 private long hi; 40 private Class returnClass; 41 42 public void configure(Type type, Properties params, Dialect d) throws MappingException { 43 super.configure(type, params, d); 44 maxLo = PropertiesHelper.getInt(MAX_LO, params, 9); 45 lo = maxLo + 1; returnClass = type.getReturnedClass(); 47 } 48 49 public synchronized Serializable generate(SessionImplementor session, Object obj) 50 throws HibernateException { 51 52 if ( lo>maxLo ) { 53 long hival = ( (Number ) super.generate(session, obj) ).longValue(); 54 lo = (hival == 0) ? 1 : 0; 55 hi = hival * ( maxLo+1 ); 56 if ( log.isDebugEnabled() ) 57 log.debug("new hi value: " + hival); 58 } 59 60 return IdentifierGeneratorFactory.createNumber( hi + lo++, returnClass ); 61 } 62 63 } 64 | Popular Tags |