FileDocCategorySizeDatePackage
ComponentType.javaAPI DocHibernate 3.2.518506Thu Jul 13 19:09:20 BST 2006org.hibernate.type

ComponentType

public class ComponentType extends AbstractType implements AbstractComponentType
Handles "component" mappings
author
Gavin King

Fields Summary
private final String[]
propertyNames
private final Type[]
propertyTypes
private final boolean[]
propertyNullability
protected final int
propertySpan
private final org.hibernate.engine.CascadeStyle[]
cascade
private final org.hibernate.FetchMode[]
joinedFetch
private final boolean
isKey
protected final org.hibernate.tuple.EntityModeToTuplizerMapping
tuplizerMapping
Constructors Summary
public ComponentType(org.hibernate.tuple.component.ComponentMetamodel metamodel)

		// for now, just "re-flatten" the metamodel since this is temporary stuff anyway (HHH-1907)
		this.isKey = metamodel.isKey();
		this.propertySpan = metamodel.getPropertySpan();
		this.propertyNames = new String[ propertySpan ];
		this.propertyTypes = new Type[ propertySpan ];
		this.propertyNullability = new boolean[ propertySpan ];
		this.cascade = new CascadeStyle[ propertySpan ];
		this.joinedFetch = new FetchMode[ propertySpan ];

		for ( int i = 0; i < propertySpan; i++ ) {
			StandardProperty prop = metamodel.getProperty( i );
			this.propertyNames[i] = prop.getName();
			this.propertyTypes[i] = prop.getType();
			this.propertyNullability[i] = prop.isNullable();
			this.cascade[i] = prop.getCascadeStyle();
			this.joinedFetch[i] = prop.getFetchMode();
		}

		this.tuplizerMapping = metamodel.getTuplizerMapping();
	
Methods Summary
public java.lang.Objectassemble(java.io.Serializable object, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)


		if ( object == null ) {
			return null;
		}
		else {
			Object[] values = ( Object[] ) object;
			Object[] assembled = new Object[values.length];
			for ( int i = 0; i < propertyTypes.length; i++ ) {
				assembled[i] = propertyTypes[i].assemble( ( Serializable ) values[i], session, owner );
			}
			Object result = instantiate( owner, session );
			setPropertyValues( result, assembled, session.getEntityMode() );
			return result;
		}
	
public intcompare(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode)

		if ( x == y ) {
			return 0;
		}
		Object[] xvalues = getPropertyValues( x, entityMode );
		Object[] yvalues = getPropertyValues( y, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			int propertyCompare = propertyTypes[i].compare( xvalues[i], yvalues[i], entityMode );
			if ( propertyCompare != 0 ) {
				return propertyCompare;
			}
		}
		return 0;
	
public java.lang.ObjectdeepCopy(java.lang.Object component, org.hibernate.EntityMode entityMode, org.hibernate.engine.SessionFactoryImplementor factory)

		if ( component == null ) {
			return null;
		}

		Object[] values = getPropertyValues( component, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			values[i] = propertyTypes[i].deepCopy( values[i], entityMode, factory );
		}

		Object result = instantiate( entityMode );
		setPropertyValues( result, values, entityMode );

		//not absolutely necessary, but helps for some
		//equals()/hashCode() implementations
		ComponentTuplizer ct = ( ComponentTuplizer ) tuplizerMapping.getTuplizer( entityMode );
		if ( ct.hasParentProperty() ) {
			ct.setParent( result, ct.getParent( component ), factory );
		}

		return result;
	
public java.io.Serializabledisassemble(java.lang.Object value, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)


		if ( value == null ) {
			return null;
		}
		else {
			Object[] values = getPropertyValues( value, session.getEntityMode() );
			for ( int i = 0; i < propertyTypes.length; i++ ) {
				values[i] = propertyTypes[i].disassemble( values[i], session, owner );
			}
			return values;
		}
	
public java.lang.ObjectfromXMLNode(org.dom4j.Node xml, org.hibernate.engine.Mapping factory)

		return xml;
	
public org.hibernate.engine.CascadeStylegetCascadeStyle(int i)

		return cascade[i];
	
public intgetColumnSpan(org.hibernate.engine.Mapping mapping)

		int span = 0;
		for ( int i = 0; i < propertySpan; i++ ) {
			span += propertyTypes[i].getColumnSpan( mapping );
		}
		return span;
	
public org.hibernate.FetchModegetFetchMode(int i)

		return joinedFetch[i];
	
public intgetHashCode(java.lang.Object x, org.hibernate.EntityMode entityMode)

		int result = 17;
		Object[] values = getPropertyValues( x, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			Object y = values[i];
			result *= 37;
			if ( y != null ) {
				result += propertyTypes[i].getHashCode( y, entityMode );
			}
		}
		return result;
	
public intgetHashCode(java.lang.Object x, org.hibernate.EntityMode entityMode, org.hibernate.engine.SessionFactoryImplementor factory)

		int result = 17;
		Object[] values = getPropertyValues( x, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			Object y = values[i];
			result *= 37;
			if ( y != null ) {
				result += propertyTypes[i].getHashCode( y, entityMode, factory );
			}
		}
		return result;
	
public java.lang.StringgetName()

		return "component" + ArrayHelper.toString( propertyNames );
	
public java.lang.String[]getPropertyNames()

		return propertyNames;
	
public boolean[]getPropertyNullability()

		return propertyNullability;
	
public java.lang.ObjectgetPropertyValue(java.lang.Object component, int i, org.hibernate.engine.SessionImplementor session)

		return getPropertyValue( component, i, session.getEntityMode() );
	
public java.lang.ObjectgetPropertyValue(java.lang.Object component, int i, org.hibernate.EntityMode entityMode)

		return tuplizerMapping.getTuplizer( entityMode ).getPropertyValue( component, i );
	
public java.lang.Object[]getPropertyValues(java.lang.Object component, org.hibernate.engine.SessionImplementor session)

		return getPropertyValues( component, session.getEntityMode() );
	
public java.lang.Object[]getPropertyValues(java.lang.Object component, org.hibernate.EntityMode entityMode)

		return tuplizerMapping.getTuplizer( entityMode ).getPropertyValues( component );
	
public java.lang.ClassgetReturnedClass()

		return tuplizerMapping.getTuplizer( EntityMode.POJO ).getMappedClass(); //TODO
	
public Type[]getSubtypes()

		return propertyTypes;
	
public java.lang.Objecthydrate(java.sql.ResultSet rs, java.lang.String[] names, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)


		int begin = 0;
		boolean notNull = false;
		Object[] values = new Object[propertySpan];
		for ( int i = 0; i < propertySpan; i++ ) {
			int length = propertyTypes[i].getColumnSpan( session.getFactory() );
			String[] range = ArrayHelper.slice( names, begin, length ); //cache this
			Object val = propertyTypes[i].hydrate( rs, range, session, owner );
			if ( val == null ) {
				if ( isKey ) {
					return null; //different nullability rules for pk/fk
				}
			}
			else {
				notNull = true;
			}
			values[i] = val;
			begin += length;
		}

		return notNull ? values : null;
	
public java.lang.Objectinstantiate(org.hibernate.EntityMode entityMode)
This method does not populate the component parent

		return tuplizerMapping.getTuplizer( entityMode ).instantiate();
	
public java.lang.Objectinstantiate(java.lang.Object parent, org.hibernate.engine.SessionImplementor session)


		Object result = instantiate( session.getEntityMode() );

		ComponentTuplizer ct = ( ComponentTuplizer ) tuplizerMapping.getTuplizer( session.getEntityMode() );
		if ( ct.hasParentProperty() && parent != null ) {
			ct.setParent(
					result,
					session.getPersistenceContext().proxyFor( parent ),
					session.getFactory()
			);
		}

		return result;
	
public final booleanisComponentType()

		return true;
	
public booleanisDirty(java.lang.Object x, java.lang.Object y, org.hibernate.engine.SessionImplementor session)

		if ( x == y ) {
			return false;
		}
		if ( x == null || y == null ) {
			return true;
		}
		EntityMode entityMode = session.getEntityMode();
		Object[] xvalues = getPropertyValues( x, entityMode );
		Object[] yvalues = getPropertyValues( y, entityMode );
		for ( int i = 0; i < xvalues.length; i++ ) {
			if ( propertyTypes[i].isDirty( xvalues[i], yvalues[i], session ) ) {
				return true;
			}
		}
		return false;
	
public booleanisDirty(java.lang.Object x, java.lang.Object y, boolean[] checkable, org.hibernate.engine.SessionImplementor session)

		if ( x == y ) {
			return false;
		}
		if ( x == null || y == null ) {
			return true;
		}
		EntityMode entityMode = session.getEntityMode();
		Object[] xvalues = getPropertyValues( x, entityMode );
		Object[] yvalues = getPropertyValues( y, entityMode );
		int loc = 0;
		for ( int i = 0; i < xvalues.length; i++ ) {
			int len = propertyTypes[i].getColumnSpan( session.getFactory() );
			if ( len <= 1 ) {
				final boolean dirty = ( len == 0 || checkable[loc] ) &&
				                      propertyTypes[i].isDirty( xvalues[i], yvalues[i], session );
				if ( dirty ) {
					return true;
				}
			}
			else {
				boolean[] subcheckable = new boolean[len];
				System.arraycopy( checkable, loc, subcheckable, 0, len );
				final boolean dirty = propertyTypes[i].isDirty( xvalues[i], yvalues[i], subcheckable, session );
				if ( dirty ) {
					return true;
				}
			}
			loc += len;
		}
		return false;
	
public booleanisEmbedded()

		return false;
	
public booleanisEqual(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode)

		if ( x == y ) {
			return true;
		}
		if ( x == null || y == null ) {
			return false;
		}
		Object[] xvalues = getPropertyValues( x, entityMode );
		Object[] yvalues = getPropertyValues( y, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			if ( !propertyTypes[i].isEqual( xvalues[i], yvalues[i], entityMode ) ) {
				return false;
			}
		}
		return true;
	
public booleanisEqual(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode, org.hibernate.engine.SessionFactoryImplementor factory)

		if ( x == y ) {
			return true;
		}
		if ( x == null || y == null ) {
			return false;
		}
		Object[] xvalues = getPropertyValues( x, entityMode );
		Object[] yvalues = getPropertyValues( y, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			if ( !propertyTypes[i].isEqual( xvalues[i], yvalues[i], entityMode, factory ) ) {
				return false;
			}
		}
		return true;
	
public booleanisMethodOf(java.lang.reflect.Method method)

		return false;
	
public booleanisModified(java.lang.Object old, java.lang.Object current, boolean[] checkable, org.hibernate.engine.SessionImplementor session)


		if ( current == null ) {
			return old != null;
		}
		if ( old == null ) {
			return current != null;
		}
		Object[] currentValues = getPropertyValues( current, session );
		Object[] oldValues = ( Object[] ) old;
		int loc = 0;
		for ( int i = 0; i < currentValues.length; i++ ) {
			int len = propertyTypes[i].getColumnSpan( session.getFactory() );
			boolean[] subcheckable = new boolean[len];
			System.arraycopy( checkable, loc, subcheckable, 0, len );
			if ( propertyTypes[i].isModified( oldValues[i], currentValues[i], subcheckable, session ) ) {
				return true;
			}
			loc += len;
		}
		return false;

	
public booleanisMutable()

		return true;
	
public booleanisSame(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode)

		if ( x == y ) {
			return true;
		}
		if ( x == null || y == null ) {
			return false;
		}
		Object[] xvalues = getPropertyValues( x, entityMode );
		Object[] yvalues = getPropertyValues( y, entityMode );
		for ( int i = 0; i < propertySpan; i++ ) {
			if ( !propertyTypes[i].isSame( xvalues[i], yvalues[i], entityMode ) ) {
				return false;
			}
		}
		return true;
	
public booleanisXMLElement()

		return true;
	
public java.lang.ObjectnullSafeGet(java.sql.ResultSet rs, java.lang.String[] names, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)

		return resolve( hydrate( rs, names, session, owner ), session, owner );
	
public java.lang.ObjectnullSafeGet(java.sql.ResultSet rs, java.lang.String name, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)


		return nullSafeGet( rs, new String[] {name}, session, owner );
	
private java.lang.Object[]nullSafeGetValues(java.lang.Object value, org.hibernate.EntityMode entityMode)

		if ( value == null ) {
			return new Object[propertySpan];
		}
		else {
			return getPropertyValues( value, entityMode );
		}
	
public voidnullSafeSet(java.sql.PreparedStatement st, java.lang.Object value, int begin, org.hibernate.engine.SessionImplementor session)


		Object[] subvalues = nullSafeGetValues( value, session.getEntityMode() );

		for ( int i = 0; i < propertySpan; i++ ) {
			propertyTypes[i].nullSafeSet( st, subvalues[i], begin, session );
			begin += propertyTypes[i].getColumnSpan( session.getFactory() );
		}
	
public voidnullSafeSet(java.sql.PreparedStatement st, java.lang.Object value, int begin, boolean[] settable, org.hibernate.engine.SessionImplementor session)


		Object[] subvalues = nullSafeGetValues( value, session.getEntityMode() );

		int loc = 0;
		for ( int i = 0; i < propertySpan; i++ ) {
			int len = propertyTypes[i].getColumnSpan( session.getFactory() );
			if ( len == 0 ) {
				//noop
			}
			else if ( len == 1 ) {
				if ( settable[loc] ) {
					propertyTypes[i].nullSafeSet( st, subvalues[i], begin, session );
					begin++;
				}
			}
			else {
				boolean[] subsettable = new boolean[len];
				System.arraycopy( settable, loc, subsettable, 0, len );
				propertyTypes[i].nullSafeSet( st, subvalues[i], begin, subsettable, session );
				begin += ArrayHelper.countTrue( subsettable );
			}
			loc += len;
		}
	
public java.lang.Objectreplace(java.lang.Object original, java.lang.Object target, org.hibernate.engine.SessionImplementor session, java.lang.Object owner, java.util.Map copyCache)


		if ( original == null ) {
			return null;
		}
		//if ( original == target ) return target;

		final Object result = target == null
				? instantiate( owner, session )
				: target;

		final EntityMode entityMode = session.getEntityMode();
		Object[] values = TypeFactory.replace(
				getPropertyValues( original, entityMode ),
				getPropertyValues( result, entityMode ),
				propertyTypes,
				session,
				owner,
				copyCache
		);

		setPropertyValues( result, values, entityMode );
		return result;
	
public java.lang.Objectreplace(java.lang.Object original, java.lang.Object target, org.hibernate.engine.SessionImplementor session, java.lang.Object owner, java.util.Map copyCache, ForeignKeyDirection foreignKeyDirection)


		if ( original == null ) {
			return null;
		}
		//if ( original == target ) return target;

		final Object result = target == null ?
				instantiate( owner, session ) :
				target;

		final EntityMode entityMode = session.getEntityMode();
		Object[] values = TypeFactory.replace(
				getPropertyValues( original, entityMode ),
				getPropertyValues( result, entityMode ),
				propertyTypes,
				session,
				owner,
				copyCache,
				foreignKeyDirection
		);

		setPropertyValues( result, values, entityMode );
		return result;
	
public java.lang.Objectresolve(java.lang.Object value, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)


		if ( value != null ) {
			Object result = instantiate( owner, session );
			Object[] values = ( Object[] ) value;
			Object[] resolvedValues = new Object[values.length]; //only really need new array during semiresolve!
			for ( int i = 0; i < values.length; i++ ) {
				resolvedValues[i] = propertyTypes[i].resolve( values[i], session, owner );
			}
			setPropertyValues( result, resolvedValues, session.getEntityMode() );
			return result;
		}
		else {
			return null;
		}
	
public java.lang.ObjectsemiResolve(java.lang.Object value, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)

		//note that this implementation is kinda broken
		//for components with many-to-one associations
		return resolve( value, session, owner );
	
public voidsetPropertyValues(java.lang.Object component, java.lang.Object[] values, org.hibernate.EntityMode entityMode)

		tuplizerMapping.getTuplizer( entityMode ).setPropertyValues( component, values );
	
public voidsetToXMLNode(org.dom4j.Node node, java.lang.Object value, org.hibernate.engine.SessionFactoryImplementor factory)

		replaceNode( node, ( Element ) value );
	
public int[]sqlTypes(org.hibernate.engine.Mapping mapping)

		//Not called at runtime so doesn't matter if its slow :)
		int[] sqlTypes = new int[getColumnSpan( mapping )];
		int n = 0;
		for ( int i = 0; i < propertySpan; i++ ) {
			int[] subtypes = propertyTypes[i].sqlTypes( mapping );
			for ( int j = 0; j < subtypes.length; j++ ) {
				sqlTypes[n++] = subtypes[j];
			}
		}
		return sqlTypes;
	
public boolean[]toColumnNullness(java.lang.Object value, org.hibernate.engine.Mapping mapping)

		boolean[] result = new boolean[ getColumnSpan( mapping ) ];
		if ( value == null ) {
			return result;
		}
		Object[] values = getPropertyValues( value, EntityMode.POJO ); //TODO!!!!!!!
		int loc = 0;
		for ( int i = 0; i < propertyTypes.length; i++ ) {
			boolean[] propertyNullness = propertyTypes[i].toColumnNullness( values[i], mapping );
			System.arraycopy( propertyNullness, 0, result, loc, propertyNullness.length );
			loc += propertyNullness.length;
		}
		return result;
	
public java.lang.StringtoLoggableString(java.lang.Object value, org.hibernate.engine.SessionFactoryImplementor factory)

		if ( value == null ) {
			return "null";
		}
		Map result = new HashMap();
		EntityMode entityMode = tuplizerMapping.guessEntityMode( value );
		if ( entityMode == null ) {
			throw new ClassCastException( value.getClass().getName() );
		}
		Object[] values = getPropertyValues( value, entityMode );
		for ( int i = 0; i < propertyTypes.length; i++ ) {
			result.put( propertyNames[i], propertyTypes[i].toLoggableString( values[i], factory ) );
		}
		return StringHelper.unqualify( getName() ) + result.toString();