FileDocCategorySizeDatePackage
TypeFactory.javaAPI DocHibernate 3.2.523298Tue May 08 22:52:56 BST 2007org.hibernate.type

TypeFactory

public final class TypeFactory extends Object
Used internally to obtain instances of Type. Applications should use static methods and constants on org.hibernate.Hibernate.
see
org.hibernate.Hibernate
author
Gavin King

Fields Summary
private static final Map
BASIC_TYPES
Constructors Summary
private TypeFactory()

		HashMap basics = new HashMap();
		basics.put( boolean.class.getName(), Hibernate.BOOLEAN );
		basics.put( long.class.getName(), Hibernate.LONG );
		basics.put( short.class.getName(), Hibernate.SHORT );
		basics.put( int.class.getName(), Hibernate.INTEGER );
		basics.put( byte.class.getName(), Hibernate.BYTE );
		basics.put( float.class.getName(), Hibernate.FLOAT );
		basics.put( double.class.getName(), Hibernate.DOUBLE );
		basics.put( char.class.getName(), Hibernate.CHARACTER );
		basics.put( Hibernate.CHARACTER.getName(), Hibernate.CHARACTER );
		basics.put( Hibernate.INTEGER.getName(), Hibernate.INTEGER );
		basics.put( Hibernate.STRING.getName(), Hibernate.STRING );
		basics.put( Hibernate.DATE.getName(), Hibernate.DATE );
		basics.put( Hibernate.TIME.getName(), Hibernate.TIME );
		basics.put( Hibernate.TIMESTAMP.getName(), Hibernate.TIMESTAMP );
		basics.put( "dbtimestamp", new DbTimestampType() );
		basics.put( Hibernate.LOCALE.getName(), Hibernate.LOCALE );
		basics.put( Hibernate.CALENDAR.getName(), Hibernate.CALENDAR );
		basics.put( Hibernate.CALENDAR_DATE.getName(), Hibernate.CALENDAR_DATE );
		basics.put( Hibernate.CURRENCY.getName(), Hibernate.CURRENCY );
		basics.put( Hibernate.TIMEZONE.getName(), Hibernate.TIMEZONE );
		basics.put( Hibernate.CLASS.getName(), Hibernate.CLASS );
		basics.put( Hibernate.TRUE_FALSE.getName(), Hibernate.TRUE_FALSE );
		basics.put( Hibernate.YES_NO.getName(), Hibernate.YES_NO );
		basics.put( Hibernate.BINARY.getName(), Hibernate.BINARY );
		basics.put( Hibernate.TEXT.getName(), Hibernate.TEXT );
		basics.put( Hibernate.BLOB.getName(), Hibernate.BLOB );
		basics.put( Hibernate.CLOB.getName(), Hibernate.CLOB );
		basics.put( Hibernate.BIG_DECIMAL.getName(), Hibernate.BIG_DECIMAL );
		basics.put( Hibernate.BIG_INTEGER.getName(), Hibernate.BIG_INTEGER );
		basics.put( Hibernate.SERIALIZABLE.getName(), Hibernate.SERIALIZABLE );
		basics.put( Hibernate.OBJECT.getName(), Hibernate.OBJECT );
		basics.put( Boolean.class.getName(), Hibernate.BOOLEAN );
		basics.put( Long.class.getName(), Hibernate.LONG );
		basics.put( Short.class.getName(), Hibernate.SHORT );
		basics.put( Integer.class.getName(), Hibernate.INTEGER );
		basics.put( Byte.class.getName(), Hibernate.BYTE );
		basics.put( Float.class.getName(), Hibernate.FLOAT );
		basics.put( Double.class.getName(), Hibernate.DOUBLE );
		basics.put( Character.class.getName(), Hibernate.CHARACTER );
		basics.put( String.class.getName(), Hibernate.STRING );
		basics.put( java.util.Date.class.getName(), Hibernate.TIMESTAMP );
		basics.put( Time.class.getName(), Hibernate.TIME );
		basics.put( Timestamp.class.getName(), Hibernate.TIMESTAMP );
		basics.put( java.sql.Date.class.getName(), Hibernate.DATE );
		basics.put( BigDecimal.class.getName(), Hibernate.BIG_DECIMAL );
		basics.put( BigInteger.class.getName(), Hibernate.BIG_INTEGER );
		basics.put( Locale.class.getName(), Hibernate.LOCALE );
		basics.put( Calendar.class.getName(), Hibernate.CALENDAR );
		basics.put( GregorianCalendar.class.getName(), Hibernate.CALENDAR );
		if ( CurrencyType.CURRENCY_CLASS != null ) {
			basics.put( CurrencyType.CURRENCY_CLASS.getName(), Hibernate.CURRENCY );
		}
		basics.put( TimeZone.class.getName(), Hibernate.TIMEZONE );
		basics.put( Object.class.getName(), Hibernate.OBJECT );
		basics.put( Class.class.getName(), Hibernate.CLASS );
		basics.put( byte[].class.getName(), Hibernate.BINARY );
		basics.put( "byte[]", Hibernate.BINARY );
		basics.put( Byte[].class.getName(), Hibernate.WRAPPER_BINARY );
		basics.put( "Byte[]", Hibernate.WRAPPER_BINARY );
		basics.put( char[].class.getName(), Hibernate.CHAR_ARRAY );
		basics.put( "char[]", Hibernate.CHAR_ARRAY );
		basics.put( Character[].class.getName(), Hibernate.CHARACTER_ARRAY );
		basics.put( "Character[]", Hibernate.CHARACTER_ARRAY );
		basics.put( Blob.class.getName(), Hibernate.BLOB );
		basics.put( Clob.class.getName(), Hibernate.CLOB );
		basics.put( Serializable.class.getName(), Hibernate.SERIALIZABLE );

		Type type = new AdaptedImmutableType(Hibernate.DATE);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.TIME);
		basics.put( type.getName(), type );		
		type = new AdaptedImmutableType(Hibernate.TIMESTAMP);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType( new DbTimestampType() );
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.CALENDAR);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.CALENDAR_DATE);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.SERIALIZABLE);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.BINARY);
		basics.put( type.getName(), type );
		
		BASIC_TYPES = Collections.unmodifiableMap( basics );
	
		throw new UnsupportedOperationException();
	
Methods Summary
public static CollectionTypearray(java.lang.String role, java.lang.String propertyRef, boolean embedded, java.lang.Class elementClass)

		return new ArrayType( role, propertyRef, elementClass, embedded );
	
public static java.lang.Object[]assemble(java.io.Serializable[] row, Type[] types, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)
Apply the {@link Type#assemble} operation across a series of values.

param
row The values
param
types The value types
param
session The orginating session
param
owner The entity "owning" the values
return
The assembled state

		Object[] assembled = new Object[row.length];
		for ( int i = 0; i < types.length; i++ ) {
			if ( row[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY || row[i] == BackrefPropertyAccessor.UNKNOWN ) {
				assembled[i] = row[i];
			}
			else {
				assembled[i] = types[i].assemble( row[i], session, owner );
			}
		}
		return assembled;
	
public static CollectionTypebag(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new BagType( role, propertyRef, embedded );
	
public static Typebasic(java.lang.String name)
Given the name of a Hibernate basic type, return an instance of org.hibernate.type.Type.

		return (Type) BASIC_TYPES.get( name );
	
public static voidbeforeAssemble(java.io.Serializable[] row, Type[] types, org.hibernate.engine.SessionImplementor session)
Apply the {@link Type#beforeAssemble} operation across a series of values.

param
row The values
param
types The value types
param
session The orginating session

		for ( int i = 0; i < types.length; i++ ) {
			if ( row[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY
				&& row[i] != BackrefPropertyAccessor.UNKNOWN ) {
				types[i].beforeAssemble( row[i], session );
			}
		}
	
public static CollectionTypecustomCollection(java.lang.String typeName, java.util.Properties typeParameters, java.lang.String role, java.lang.String propertyRef, boolean embedded)

		Class typeClass;
		try {
			typeClass = ReflectHelper.classForName( typeName );
		}
		catch ( ClassNotFoundException cnfe ) {
			throw new MappingException( "user collection type class not found: " + typeName, cnfe );
		}
		CustomCollectionType result = new CustomCollectionType( typeClass, role, propertyRef, embedded );
		if ( typeParameters != null ) {
			TypeFactory.injectParameters( result.getUserType(), typeParameters );
		}
		return result;
	
public static CollectionTypecustomCollection(java.lang.String typeName, java.lang.String role, java.lang.String propertyRef, boolean embedded)
The legacy contract.

deprecated
Use {@link #customCollection(String, java.util.Properties, String, String, boolean)} instead

		return customCollection( typeName, null, role, propertyRef, embedded );
	
public static voiddeepCopy(java.lang.Object[] values, Type[] types, boolean[] copy, java.lang.Object[] target, org.hibernate.engine.SessionImplementor session)
Deep copy a series of values from one array to another...

param
values The values to copy (the source)
param
types The value types
param
copy an array indicating which values to include in the copy
param
target The array into which to copy the values
param
session The orginating session

		for ( int i = 0; i < types.length; i++ ) {
			if ( copy[i] ) {
				if ( values[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
					|| values[i] == BackrefPropertyAccessor.UNKNOWN ) {
					target[i] = values[i];
				}
				else {
					target[i] = types[i].deepCopy( values[i], session.getEntityMode(), session
						.getFactory() );
				}
			}
		}
	
public static java.io.Serializable[]disassemble(java.lang.Object[] row, Type[] types, boolean[] nonCacheable, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)
Apply the {@link Type#disassemble} operation across a series of values.

param
row The values
param
types The value types
param
nonCacheable An array indicating which values to include in the disassemled state
param
session The orginating session
param
owner The entity "owning" the values
return
The disassembled state

		Serializable[] disassembled = new Serializable[row.length];
		for ( int i = 0; i < row.length; i++ ) {
			if ( nonCacheable!=null && nonCacheable[i] ) {
				disassembled[i] = LazyPropertyInitializer.UNFETCHED_PROPERTY;
			}
			else if ( row[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY || row[i] == BackrefPropertyAccessor.UNKNOWN ) {
				disassembled[i] = (Serializable) row[i];
			}
			else {
				disassembled[i] = types[i].disassemble( row[i], session, owner );
			}
		}
		return disassembled;
	
public static int[]findDirty(org.hibernate.tuple.StandardProperty[] properties, java.lang.Object[] currentState, java.lang.Object[] previousState, boolean[][] includeColumns, boolean anyUninitializedProperties, org.hibernate.engine.SessionImplementor session)
Determine if any of the given field values are dirty, returning an array containing indices of the dirty fields.

If it is determined that no fields are dirty, null is returned.

param
properties The property definitions
param
currentState The current state of the entity
param
previousState The baseline state of the entity
param
includeColumns Columns to be included in the dirty checking, per property
param
anyUninitializedProperties Does the entity currently hold any uninitialized property values?
param
session The session from which the dirty check request originated.
return
Array containing indices of the dirty properties, or null if no properties considered dirty.

		int[] results = null;
		int count = 0;
		int span = properties.length;

		for ( int i = 0; i < span; i++ ) {
			final boolean dirty = currentState[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY
					&& properties[i].isDirtyCheckable( anyUninitializedProperties )
					&& properties[i].getType().isDirty( previousState[i], currentState[i], includeColumns[i], session );
			if ( dirty ) {
				if ( results == null ) {
					results = new int[span];
				}
				results[count++] = i;
			}
		}

		if ( count == 0 ) {
			return null;
		}
		else {
			int[] trimmed = new int[count];
			System.arraycopy( results, 0, trimmed, 0, count );
			return trimmed;
		}
	
public static int[]findModified(org.hibernate.tuple.StandardProperty[] properties, java.lang.Object[] currentState, java.lang.Object[] previousState, boolean[][] includeColumns, boolean anyUninitializedProperties, org.hibernate.engine.SessionImplementor session)
Determine if any of the given field values are modified, returning an array containing indices of the modified fields.

If it is determined that no fields are dirty, null is returned.

param
properties The property definitions
param
currentState The current state of the entity
param
previousState The baseline state of the entity
param
includeColumns Columns to be included in the mod checking, per property
param
anyUninitializedProperties Does the entity currently hold any uninitialized property values?
param
session The session from which the dirty check request originated.
return
Array containing indices of the modified properties, or null if no properties considered modified.

		int[] results = null;
		int count = 0;
		int span = properties.length;

		for ( int i = 0; i < span; i++ ) {
			final boolean modified = currentState[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY
					&& properties[i].isDirtyCheckable(anyUninitializedProperties)
					&& properties[i].getType().isModified( previousState[i], currentState[i], includeColumns[i], session );

			if ( modified ) {
				if ( results == null ) {
					results = new int[span];
				}
				results[count++] = i;
			}
		}

		if ( count == 0 ) {
			return null;
		}
		else {
			int[] trimmed = new int[count];
			System.arraycopy( results, 0, trimmed, 0, count );
			return trimmed;
		}
	
public static TypeheuristicType(java.lang.String typeName)
Uses heuristics to deduce a Hibernate type given a string naming the type or Java class. Return an instance of org.hibernate.type.Type.

		return heuristicType( typeName, null );
	
public static TypeheuristicType(java.lang.String typeName, java.util.Properties parameters)
Uses heuristics to deduce a Hibernate type given a string naming the type or Java class. Return an instance of org.hibernate.type.Type.

		Type type = TypeFactory.basic( typeName );
		if ( type == null ) {
			Class typeClass;
			try {
				typeClass = ReflectHelper.classForName( typeName );
			}
			catch (ClassNotFoundException cnfe) {
				typeClass = null;
			}
			if ( typeClass != null ) {
				if ( Type.class.isAssignableFrom( typeClass ) ) {
					try {
						type = (Type) typeClass.newInstance();
					}
					catch (Exception e) {
						throw new MappingException( 
								"Could not instantiate Type: " + typeClass.getName(),
								e 
							);
					}
					injectParameters(type, parameters);
				}
				else if ( CompositeUserType.class.isAssignableFrom( typeClass ) ) {
					type = new CompositeCustomType( typeClass, parameters );
				}
				else if ( UserType.class.isAssignableFrom( typeClass ) ) {
					type = new CustomType( typeClass, parameters );
				}
				else if ( Lifecycle.class.isAssignableFrom( typeClass ) ) {
					type = Hibernate.entity( typeClass );
				}
				else if ( Serializable.class.isAssignableFrom( typeClass ) ) {
					type = Hibernate.serializable( typeClass );
				}
			}
		}
		return type;

	
public static CollectionTypeidbag(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new IdentifierBagType( role, propertyRef, embedded );
	
public static voidinjectParameters(java.lang.Object type, java.util.Properties parameters)

		if (type instanceof ParameterizedType) {
			( (ParameterizedType) type ).setParameterValues(parameters);
		}
		else if ( parameters!=null && !parameters.isEmpty() ) {
			throw new MappingException(
					"type is not parameterized: " +
					type.getClass().getName()
				);
		}
	
public static CollectionTypelist(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new ListType( role, propertyRef, embedded );
	
public static EntityTypemanyToOne(java.lang.String persistentClass)
A many-to-one association type for the given class

		return new ManyToOneType( persistentClass );
	
public static EntityTypemanyToOne(java.lang.String persistentClass, boolean lazy)
A many-to-one association type for the given class

		return new ManyToOneType( persistentClass, lazy );
	
public static EntityTypemanyToOne(java.lang.String persistentClass, java.lang.String uniqueKeyPropertyName, boolean lazy, boolean unwrapProxy, boolean isEmbeddedInXML, boolean ignoreNotFound)
A many-to-one association type for the given class

		return new ManyToOneType( 
				persistentClass, 
				uniqueKeyPropertyName, 
				lazy,
				unwrapProxy, 
				isEmbeddedInXML,
				ignoreNotFound
			);
	
public static CollectionTypemap(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new MapType( role, propertyRef, embedded );
	
public static EntityTypeoneToOne(java.lang.String persistentClass, ForeignKeyDirection foreignKeyType, java.lang.String uniqueKeyPropertyName, boolean lazy, boolean unwrapProxy, boolean isEmbeddedInXML, java.lang.String entityName, java.lang.String propertyName)
A one-to-one association type for the given class

		return new OneToOneType(
				persistentClass,
				foreignKeyType,
				uniqueKeyPropertyName,
				lazy,
				unwrapProxy,
				isEmbeddedInXML,
				entityName,
				propertyName 
			);
	
public static CollectionTypeorderedMap(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new OrderedMapType( role, propertyRef, embedded );
	
public static CollectionTypeorderedSet(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new OrderedSetType( role, propertyRef, embedded );
	
public static java.lang.Object[]replace(java.lang.Object[] original, java.lang.Object[] target, Type[] types, org.hibernate.engine.SessionImplementor session, java.lang.Object owner, java.util.Map copyCache)
Apply the {@link Type#replace} operation across a series of values.

param
original The source of the state
param
target The target into which to replace the source values.
param
types The value types
param
session The orginating session
param
owner The entity "owning" the values
param
copyCache A map representing a cache of already replaced state
return
The replaced state

		Object[] copied = new Object[original.length];
		for ( int i = 0; i < types.length; i++ ) {
			if ( original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
				|| original[i] == BackrefPropertyAccessor.UNKNOWN ) {
				copied[i] = target[i];
			}
			else {
				copied[i] = types[i].replace( original[i], target[i], session, owner, copyCache );
			}
		}
		return copied;
	
public static java.lang.Object[]replace(java.lang.Object[] original, java.lang.Object[] target, Type[] types, org.hibernate.engine.SessionImplementor session, java.lang.Object owner, java.util.Map copyCache, ForeignKeyDirection foreignKeyDirection)
Apply the {@link Type#replace} operation across a series of values.

param
original The source of the state
param
target The target into which to replace the source values.
param
types The value types
param
session The orginating session
param
owner The entity "owning" the values
param
copyCache A map representing a cache of already replaced state
param
foreignKeyDirection FK directionality to be applied to the replacement
return
The replaced state

		Object[] copied = new Object[original.length];
		for ( int i = 0; i < types.length; i++ ) {
			if ( original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
				|| original[i] == BackrefPropertyAccessor.UNKNOWN ) {
				copied[i] = target[i];
			}
			else {
				copied[i] = types[i].replace( original[i], target[i], session, owner, copyCache, foreignKeyDirection );
			}
		}
		return copied;
	
public static java.lang.Object[]replaceAssociations(java.lang.Object[] original, java.lang.Object[] target, Type[] types, org.hibernate.engine.SessionImplementor session, java.lang.Object owner, java.util.Map copyCache, ForeignKeyDirection foreignKeyDirection)
Apply the {@link Type#replace} operation across a series of values, as long as the corresponding {@link Type} is an association.

If the corresponding type is a component type, then apply {@link #replaceAssociations} accross the component subtypes but do not replace the component value itself.

param
original The source of the state
param
target The target into which to replace the source values.
param
types The value types
param
session The orginating session
param
owner The entity "owning" the values
param
copyCache A map representing a cache of already replaced state
param
foreignKeyDirection FK directionality to be applied to the replacement
return
The replaced state

		Object[] copied = new Object[original.length];
		for ( int i = 0; i < types.length; i++ ) {
			if ( original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY
					|| original[i] == BackrefPropertyAccessor.UNKNOWN ) {
				copied[i] = target[i];
			}
			else if ( types[i].isComponentType() ) {
				// need to extract the component values and check for subtype replacements...
				AbstractComponentType componentType = ( AbstractComponentType ) types[i];
				Type[] subtypes = componentType.getSubtypes();
				Object[] origComponentValues = original[i] == null ? new Object[subtypes.length] : componentType.getPropertyValues( original[i], session );
				Object[] targetComponentValues = componentType.getPropertyValues( target[i], session );
				replaceAssociations( origComponentValues, targetComponentValues, subtypes, session, null, copyCache, foreignKeyDirection );
				copied[i] = target[i];
			}
			else if ( !types[i].isAssociationType() ) {
				copied[i] = target[i];
			}
			else {
				copied[i] = types[i].replace( original[i], target[i], session, owner, copyCache, foreignKeyDirection );
			}
		}
		return copied;
	
public static CollectionTypeset(java.lang.String role, java.lang.String propertyRef, boolean embedded)

		return new SetType( role, propertyRef, embedded );
	
public static CollectionTypesortedMap(java.lang.String role, java.lang.String propertyRef, boolean embedded, java.util.Comparator comparator)

		return new SortedMapType( role, propertyRef, comparator, embedded );
	
public static CollectionTypesortedSet(java.lang.String role, java.lang.String propertyRef, boolean embedded, java.util.Comparator comparator)

		return new SortedSetType( role, propertyRef, comparator, embedded );