FileDocCategorySizeDatePackage
IncrementGenerator.javaAPI DocHibernate 3.2.53318Sun Nov 20 22:59:30 GMT 2005org.hibernate.id

IncrementGenerator

public class IncrementGenerator extends Object implements Configurable, IdentifierGenerator
increment

An IdentifierGenerator that returns a long, constructed by counting from the maximum primary key value at startup. Not safe for use in a cluster!

Mapping parameters supported, but not usually needed: tables, column. (The tables parameter specified a comma-separated list of table names.)
author
Gavin King

Fields Summary
private static final Log
log
private long
next
private String
sql
private Class
returnClass
Constructors Summary
Methods Summary
public voidconfigure(org.hibernate.type.Type type, java.util.Properties params, org.hibernate.dialect.Dialect dialect)


		String tableList = params.getProperty("tables");
		if (tableList==null) tableList = params.getProperty(PersistentIdentifierGenerator.TABLES);
		String[] tables = StringHelper.split(", ", tableList);
		String column = params.getProperty("column");
		if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
		String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
		String catalog = params.getProperty(PersistentIdentifierGenerator.CATALOG);
		returnClass = type.getReturnedClass();
		

		StringBuffer buf = new StringBuffer();
		for ( int i=0; i<tables.length; i++ ) {
			if (tables.length>1) {
				buf.append("select ").append(column).append(" from ");
			}
			buf.append( Table.qualify( catalog, schema, tables[i] ) );
			if ( i<tables.length-1) buf.append(" union ");
		}
		if (tables.length>1) {
			buf.insert(0, "( ").append(" ) ids_");
			column = "ids_." + column;
		}
		
		sql = "select max(" + column + ") from " + buf.toString();
	
public synchronized java.io.Serializablegenerate(org.hibernate.engine.SessionImplementor session, java.lang.Object object)


	       
	  

		if (sql!=null) {
			getNext( session );
		}
		return IdentifierGeneratorFactory.createNumber(next++, returnClass);
	
private voidgetNext(org.hibernate.engine.SessionImplementor session)


		log.debug("fetching initial value: " + sql);
		
		try {
			PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
			try {
				ResultSet rs = st.executeQuery();
				try {
					if ( rs.next() ) {
						next = rs.getLong(1) + 1;
						if ( rs.wasNull() ) next = 1;
					}
					else {
						next = 1;
					}
					sql=null;
					log.debug("first free id: " + next);
				}
				finally {
					rs.close();
				}
			}
			finally {
				session.getBatcher().closeStatement(st);
			}
			
		}
		catch (SQLException sqle) {
			throw JDBCExceptionHelper.convert(
					session.getFactory().getSQLExceptionConverter(),
					sqle,
					"could not fetch initial value for increment generator",
					sql
				);
		}