KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.id;
2
3 import java.io.Serializable JavaDoc;
4 import java.sql.Connection 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.StringHelper;
20
21 /**
22  * <b>increment</b><br>
23  * <br>
24  * An <tt>IdentifierGenerator</tt> that returns a <tt>long</tt>, constructed by
25  * counting from the maximum primary key value at startup. Not safe for use in a
26  * cluster!<br>
27  * <br>
28  * Mapping parameters supported, but not usually needed: tables, column.
29  * (The tables parameter specified a comma-separated list of table names.)
30  *
31  * @author Gavin King
32  */

33 public class IncrementGenerator implements IdentifierGenerator, Configurable {
34
35     private static final Log log = LogFactory.getLog(IncrementGenerator.class);
36
37     private long next;
38     private String JavaDoc sql;
39     private Class JavaDoc returnClass;
40
41     public synchronized Serializable JavaDoc generate(SessionImplementor session, Object JavaDoc object)
42     throws HibernateException {
43
44         if (sql!=null) {
45             getNext( session );
46         }
47         return IdentifierGeneratorFactory.createNumber(next++, returnClass);
48     }
49
50     public void configure(Type type, Properties JavaDoc params, Dialect dialect)
51     throws MappingException {
52
53         String JavaDoc tableList = params.getProperty("tables");
54         if (tableList==null) tableList = params.getProperty(PersistentIdentifierGenerator.TABLES);
55         String JavaDoc[] tables = StringHelper.split(", ", tableList);
56         String JavaDoc column = params.getProperty("column");
57         if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
58         String JavaDoc schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
59         String JavaDoc catalog = params.getProperty(PersistentIdentifierGenerator.CATALOG);
60         returnClass = type.getReturnedClass();
61         
62
63         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
64         for ( int i=0; i<tables.length; i++ ) {
65             if (tables.length>1) {
66                 buf.append("select ").append(column).append(" from ");
67             }
68             buf.append( Table.qualify( catalog, schema, tables[i], dialect.getSchemaSeparator() ) );
69             if ( i<tables.length-1) buf.append(" union ");
70         }
71         if (tables.length>1) {
72             buf.insert(0, "( ").append(" ) ids_");
73             column = "ids_." + column;
74         }
75         
76         sql = "select max(" + column + ") from " + buf.toString();
77     }
78
79     private void getNext( SessionImplementor session ) {
80
81         Connection JavaDoc conn = session.connection();
82         log.debug("fetching initial value: " + sql);
83         
84         try {
85             PersistentIdentifierGenerator.SQL.debug(sql);
86             PreparedStatement JavaDoc st = conn.prepareStatement(sql);
87             ResultSet JavaDoc rs = null;
88             try {
89                 rs = st.executeQuery();
90                 if ( rs.next() ) {
91                     next = rs.getLong(1) + 1;
92                     if ( rs.wasNull() ) next = 1;
93                 }
94                 else {
95                     next = 1;
96                 }
97                 sql=null;
98                 log.debug("first free id: " + next);
99             }
100             finally {
101                 if (rs!=null) rs.close();
102                 st.close();
103             }
104             
105         }
106         catch (SQLException JavaDoc sqle) {
107             throw JDBCExceptionHelper.convert(
108                     session.getFactory().getSQLExceptionConverter(),
109                     sqle,
110                     "could not fetch initial value",
111                     sql
112                 );
113         }
114     }
115
116 }
117
Popular Tags