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.dialect.Dialect; 11 import org.hibernate.engine.SessionImplementor; 12 import org.hibernate.type.Type; 13 import org.hibernate.util.PropertiesHelper; 14 15 31 32 public class TableHiLoGenerator extends TableGenerator { 33 34 37 public static final String MAX_LO = "max_lo"; 38 39 private long hi; 40 private int lo; 41 private int maxLo; 42 private Class returnClass; 43 44 private static final Log log = LogFactory.getLog(TableHiLoGenerator.class); 45 46 public void configure(Type type, Properties params, Dialect d) { 47 super.configure(type, params, d); 48 maxLo = PropertiesHelper.getInt(MAX_LO, params, Short.MAX_VALUE); 49 lo = maxLo + 1; returnClass = type.getReturnedClass(); 51 } 52 53 public synchronized Serializable generate(SessionImplementor session, Object obj) 54 throws HibernateException { 55 if (maxLo < 2) { 56 int val = ( (Integer ) super.generate(session, obj) ).intValue(); 58 return IdentifierGeneratorFactory.createNumber( val, returnClass ); 59 } 60 if (lo>maxLo) { 61 int hival = ( (Integer ) super.generate(session, obj) ).intValue(); 62 lo = (hival == 0) ? 1 : 0; 63 hi = hival * (maxLo+1); 64 log.debug("new hi value: " + hival); 65 } 66 67 return IdentifierGeneratorFactory.createNumber( hi + lo++, returnClass ); 68 69 } 70 71 } 72 | Popular Tags |