KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SequenceGenerator.java,v 1.14 2005/07/04 02:40:29 oneovthafew Exp $
2
package org.hibernate.id;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.hibernate.HibernateException;
13 import org.hibernate.MappingException;
14 import org.hibernate.exception.JDBCExceptionHelper;
15 import org.hibernate.dialect.Dialect;
16 import org.hibernate.engine.SessionImplementor;
17 import org.hibernate.mapping.Table;
18 import org.hibernate.type.Type;
19 import org.hibernate.util.PropertiesHelper;
20
21 /**
22  * <b>sequence</b><br>
23  * <br>
24  * Generates <tt>long</tt> values using an oracle-style sequence. A higher
25  * performance algorithm is <tt>SequenceHiLoGenerator</tt>.<br>
26  * <br>
27  * Mapping parameters supported: sequence, parameters.
28  *
29  * @see SequenceHiLoGenerator
30  * @see TableHiLoGenerator
31  * @author Gavin King
32  */

33
34 public class SequenceGenerator implements PersistentIdentifierGenerator, Configurable {
35
36     /**
37      * The sequence parameter
38      */

39     public static final String JavaDoc SEQUENCE = "sequence";
40
41     /**
42      * The parameters parameter, appended to the create sequence DDL.
43      * For example (Oracle): <tt>INCREMENT BY 1 START WITH 1 MAXVALUE 100 NOCACHE</tt>.
44      */

45     public static final String JavaDoc PARAMETERS = "parameters";
46
47     private String JavaDoc sequenceName;
48     private String JavaDoc parameters;
49     private Type identifierType;
50     private String JavaDoc sql;
51
52     private static final Log log = LogFactory.getLog(SequenceGenerator.class);
53
54     public void configure(Type type, Properties JavaDoc params, Dialect dialect) throws MappingException {
55         this.sequenceName = PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence");
56         this.parameters = params.getProperty(PARAMETERS);
57         String JavaDoc schemaName = params.getProperty(SCHEMA);
58         String JavaDoc catalogName = params.getProperty(CATALOG);
59
60         if (sequenceName.indexOf(dialect.getSchemaSeparator() ) < 0) {
61             sequenceName = Table.qualify( catalogName, schemaName, sequenceName, dialect.getSchemaSeparator() );
62         }
63
64         this.identifierType = type;
65         sql = dialect.getSequenceNextValString(sequenceName);
66     }
67
68     public Serializable JavaDoc generate(SessionImplementor session, Object JavaDoc obj)
69     throws HibernateException {
70         
71         try {
72
73             PreparedStatement JavaDoc st = session.getBatcher().prepareSelectStatement(sql);
74             try {
75                 ResultSet JavaDoc rs = st.executeQuery();
76                 final Serializable JavaDoc result;
77                 try {
78                     rs.next();
79                     result = IdentifierGeneratorFactory.get(
80                         rs, identifierType
81                     );
82                 }
83                 finally {
84                     rs.close();
85                 }
86                 if ( log.isDebugEnabled() )
87                     log.debug("Sequence identifier generated: " + result);
88                 return result;
89             }
90             finally {
91                 session.getBatcher().closeStatement(st);
92             }
93             
94         }
95         catch (SQLException JavaDoc sqle) {
96             throw JDBCExceptionHelper.convert(
97                     session.getFactory().getSQLExceptionConverter(),
98                     sqle,
99                     "could not get next sequence value",
100                     sql
101                 );
102         }
103
104     }
105
106     public String JavaDoc[] sqlCreateStrings(Dialect dialect) throws HibernateException {
107         String JavaDoc[] ddl = dialect.getCreateSequenceStrings(sequenceName);
108         if ( parameters!=null ) ddl[ddl.length-1] += ' ' + parameters;
109         return ddl;
110     }
111
112     public String JavaDoc[] sqlDropStrings(Dialect dialect) throws HibernateException {
113         return dialect.getDropSequenceStrings(sequenceName);
114     }
115
116     public Object JavaDoc generatorKey() {
117         return sequenceName;
118     }
119
120 }
121
Popular Tags