KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > id > SequenceHiLoGenerator


1 //$Id: SequenceHiLoGenerator.java,v 1.7 2005/02/12 07:19:22 steveebersole Exp $
2
package org.hibernate.id;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Properties JavaDoc;
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 /**
17  * <b>seqhilo</b><br>
18  * <br>
19  * An <tt>IdentifierGenerator</tt> that combines a hi/lo algorithm with an underlying
20  * oracle-style sequence that generates hi values. The user may specify a
21  * maximum lo value to determine how often new hi values are fetched.<br>
22  * <br>
23  * If sequences are not available, <tt>TableHiLoGenerator</tt> might be an
24  * alternative.<br>
25  * <br>
26  * Mapping parameters supported: sequence, max_lo, parameters.
27  *
28  * @see TableHiLoGenerator
29  * @author Gavin King
30  */

31 public class SequenceHiLoGenerator extends SequenceGenerator {
32
33     public static final String JavaDoc 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 JavaDoc returnClass;
41
42     public void configure(Type type, Properties JavaDoc params, Dialect d) throws MappingException {
43         super.configure(type, params, d);
44         maxLo = PropertiesHelper.getInt(MAX_LO, params, 9);
45         lo = maxLo + 1; // so we "clock over" on the first invocation
46
returnClass = type.getReturnedClass();
47     }
48
49     public synchronized Serializable JavaDoc generate(SessionImplementor session, Object JavaDoc obj)
50     throws HibernateException {
51
52         if ( lo>maxLo ) {
53             long hival = ( (Number JavaDoc) 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