1 package org.hibernate.id; 2 3 import java.io.Serializable ; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.util.Properties ; 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 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 sql; 39 private Class returnClass; 40 41 public synchronized Serializable generate(SessionImplementor session, Object 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 params, Dialect dialect) 51 throws MappingException { 52 53 String tableList = params.getProperty("tables"); 54 if (tableList==null) tableList = params.getProperty(PersistentIdentifierGenerator.TABLES); 55 String [] tables = StringHelper.split(", ", tableList); 56 String column = params.getProperty("column"); 57 if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK); 58 String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA); 59 String catalog = params.getProperty(PersistentIdentifierGenerator.CATALOG); 60 returnClass = type.getReturnedClass(); 61 62 63 StringBuffer buf = new StringBuffer (); 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 conn = session.connection(); 82 log.debug("fetching initial value: " + sql); 83 84 try { 85 PersistentIdentifierGenerator.SQL.debug(sql); 86 PreparedStatement st = conn.prepareStatement(sql); 87 ResultSet 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 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 |