FileDocCategorySizeDatePackage
IdentifierGeneratorFactory.javaAPI DocHibernate 3.2.54434Mon Mar 27 10:47:06 BST 2006org.hibernate.id

IdentifierGeneratorFactory

public final class IdentifierGeneratorFactory extends Object
Factory and helper methods for IdentifierGenerator framework.
author
Gavin King

Fields Summary
private static final Log
log
private static final HashMap
GENERATORS
public static final Serializable
SHORT_CIRCUIT_INDICATOR
public static final Serializable
POST_INSERT_INDICATOR
Constructors Summary
private IdentifierGeneratorFactory()

Methods Summary
public static IdentifierGeneratorcreate(java.lang.String strategy, org.hibernate.type.Type type, java.util.Properties params, org.hibernate.dialect.Dialect dialect)


	 
		GENERATORS.put("uuid", UUIDHexGenerator.class);
		GENERATORS.put("hilo", TableHiLoGenerator.class);
		GENERATORS.put("assigned", Assigned.class);
		GENERATORS.put("identity", IdentityGenerator.class);
		GENERATORS.put("select", SelectGenerator.class);
		GENERATORS.put("sequence", SequenceGenerator.class);
		GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);
		GENERATORS.put("increment", IncrementGenerator.class);
		GENERATORS.put("foreign", ForeignGenerator.class);
		GENERATORS.put("guid", GUIDGenerator.class);
		GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated
		GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class);
	
		try {
			Class clazz = getIdentifierGeneratorClass( strategy, dialect );
			IdentifierGenerator idgen = (IdentifierGenerator) clazz.newInstance();
			if (idgen instanceof Configurable) ( (Configurable) idgen).configure(type, params, dialect);
			return idgen;
		}
		catch (Exception e) {
			throw new MappingException("could not instantiate id generator", e);
		}
	
public static java.lang.NumbercreateNumber(long value, java.lang.Class clazz)

		if ( clazz==Long.class ) {
			return new Long(value);
		}
		else if ( clazz==Integer.class ) {
			return new Integer( (int) value );
		}
		else if ( clazz==Short.class ) {
			return new Short( (short) value );
		}
		else {
			throw new IdentifierGenerationException("this id generator generates long, integer, short");
		}
	
public static java.io.Serializableget(java.sql.ResultSet rs, org.hibernate.type.Type type)

	
		Class clazz = type.getReturnedClass();
		if ( clazz==Long.class ) {
			return new Long( rs.getLong(1) );
		}
		else if ( clazz==Integer.class ) {
			return new Integer( rs.getInt(1) );
		}
		else if ( clazz==Short.class ) {
			return new Short( rs.getShort(1) );
		}
		else if ( clazz==String.class ) {
			return rs.getString(1);
		}
		else {
			throw new IdentifierGenerationException("this id generator generates long, integer, short or string");
		}
		
	
public static java.io.SerializablegetGeneratedIdentity(java.sql.ResultSet rs, org.hibernate.type.Type type)
Get the generated identifier when using identity columns


	        	 
	      
	    
		if ( !rs.next() ) {
			throw new HibernateException( "The database returned no natively generated identity value" );
		}
		final Serializable id = IdentifierGeneratorFactory.get( rs, type );

		if ( log.isDebugEnabled() ) log.debug( "Natively generated identity: " + id );
		return id;
	
public static java.lang.ClassgetIdentifierGeneratorClass(java.lang.String strategy, org.hibernate.dialect.Dialect dialect)

		Class clazz = (Class) GENERATORS.get(strategy);
		if ( "native".equals(strategy) ) clazz = dialect.getNativeIdentifierGeneratorClass();
		try {
			if (clazz==null) clazz = ReflectHelper.classForName(strategy);
		}
		catch (ClassNotFoundException e) {
			throw new MappingException("could not interpret id generator strategy: " + strategy);
		}
		return clazz;