FileDocCategorySizeDatePackage
AbstractEntityTuplizer.javaAPI DocHibernate 3.2.513626Mon Jul 24 12:56:30 BST 2006org.hibernate.tuple.entity

AbstractEntityTuplizer

public abstract class AbstractEntityTuplizer extends Object implements EntityTuplizer
Support for tuplizers relating to entities.
author
Steve Ebersole
author
Gavin King

Fields Summary
private final EntityMetamodel
entityMetamodel
private final org.hibernate.property.Getter
idGetter
private final org.hibernate.property.Setter
idSetter
protected final org.hibernate.property.Getter[]
getters
protected final org.hibernate.property.Setter[]
setters
protected final int
propertySpan
protected final boolean
hasCustomAccessors
private final org.hibernate.tuple.Instantiator
instantiator
private final org.hibernate.proxy.ProxyFactory
proxyFactory
private final org.hibernate.type.AbstractComponentType
identifierMapperType
Constructors Summary
public AbstractEntityTuplizer(EntityMetamodel entityMetamodel, org.hibernate.mapping.PersistentClass mappingInfo)
Constructs a new AbstractEntityTuplizer instance.

param
entityMetamodel The "interpreted" information relating to the mapped entity.
param
mappingInfo The parsed "raw" mapping data relating to the given entity.

		this.entityMetamodel = entityMetamodel;

		if ( !entityMetamodel.getIdentifierProperty().isVirtual() ) {
			idGetter = buildPropertyGetter( mappingInfo.getIdentifierProperty(), mappingInfo );
			idSetter = buildPropertySetter( mappingInfo.getIdentifierProperty(), mappingInfo );
		}
		else {
			idGetter = null;
			idSetter = null;
		}

		propertySpan = entityMetamodel.getPropertySpan();

        getters = new Getter[propertySpan];
		setters = new Setter[propertySpan];

		Iterator iter = mappingInfo.getPropertyClosureIterator();
		boolean foundCustomAccessor=false;
		int i=0;
		while ( iter.hasNext() ) {
			//TODO: redesign how PropertyAccessors are acquired...
			Property property = (Property) iter.next();
			getters[i] = buildPropertyGetter(property, mappingInfo);
			setters[i] = buildPropertySetter(property, mappingInfo);
			if ( !property.isBasicPropertyAccessor() ) foundCustomAccessor = true;
			i++;
		}
		hasCustomAccessors = foundCustomAccessor;

        instantiator = buildInstantiator( mappingInfo );

		if ( entityMetamodel.isLazy() ) {
			proxyFactory = buildProxyFactory( mappingInfo, idGetter, idSetter );
			if (proxyFactory == null) {
				entityMetamodel.setLazy( false );
			}
		}
		else {
			proxyFactory = null;
		}
		
		Component mapper = mappingInfo.getIdentifierMapper();
		identifierMapperType = mapper==null ? null : (AbstractComponentType) mapper.getType();
	
Methods Summary
public voidafterInitialize(java.lang.Object entity, boolean lazyPropertiesAreUnfetched, org.hibernate.engine.SessionImplementor session)

protected abstract org.hibernate.tuple.InstantiatorbuildInstantiator(org.hibernate.mapping.PersistentClass mappingInfo)
Build an appropriate Instantiator for the given mapped entity.

param
mappingInfo The mapping information regarding the mapped entity.
return
An appropriate Instantiator instance.

protected abstract org.hibernate.property.GetterbuildPropertyGetter(org.hibernate.mapping.Property mappedProperty, org.hibernate.mapping.PersistentClass mappedEntity)
Build an appropriate Getter for the given property.

param
mappedProperty The property to be accessed via the built Getter.
param
mappedEntity The entity information regarding the mapped entity owning this property.
return
An appropriate Getter instance.

protected abstract org.hibernate.property.SetterbuildPropertySetter(org.hibernate.mapping.Property mappedProperty, org.hibernate.mapping.PersistentClass mappedEntity)
Build an appropriate Setter for the given property.

param
mappedProperty The property to be accessed via the built Setter.
param
mappedEntity The entity information regarding the mapped entity owning this property.
return
An appropriate Setter instance.

protected abstract org.hibernate.proxy.ProxyFactorybuildProxyFactory(org.hibernate.mapping.PersistentClass mappingInfo, org.hibernate.property.Getter idGetter, org.hibernate.property.Setter idSetter)
Build an appropriate ProxyFactory for the given mapped entity.

param
mappingInfo The mapping information regarding the mapped entity.
param
idGetter The constructed Getter relating to the entity's id property.
param
idSetter The constructed Setter relating to the entity's id property.
return
An appropriate ProxyFactory instance.

public final java.lang.ObjectcreateProxy(java.io.Serializable id, org.hibernate.engine.SessionImplementor session)

		return getProxyFactory().getProxy( id, session );
	
protected java.lang.ObjectgetComponentValue(org.hibernate.type.ComponentType type, java.lang.Object component, java.lang.String propertyPath)
Extract a component property value.

param
type The component property types.
param
component The component instance itself.
param
propertyPath The property path for the property to be extracted.
return
The property value extracted.

		
		int loc = propertyPath.indexOf('.");
		String basePropertyName = loc>0 ?
			propertyPath.substring(0, loc) : propertyPath;
		
		String[] propertyNames = type.getPropertyNames();
		int index=0;
		for ( ; index<propertyNames.length; index++ ) {
			if ( basePropertyName.equals( propertyNames[index] ) ) break;
		}
		if (index==propertyNames.length) {
			throw new MappingException( "component property not found: " + basePropertyName );
		}
		
		Object baseValue = type.getPropertyValue( component, index, getEntityMode() );
		
		if ( loc>0 ) {
			ComponentType subtype = (ComponentType) type.getSubtypes()[index];
			return getComponentValue( subtype, baseValue, propertyPath.substring(loc+1) );
		}
		else {
			return baseValue;
		}
		
	
protected final EntityMetamodelgetEntityMetamodel()

		return entityMetamodel;
	
protected abstract org.hibernate.EntityModegetEntityMode()
Return the entity-mode handled by this tuplizer instance.

return
The entity-mode

protected java.lang.StringgetEntityName()
Retreives the defined entity-name for the tuplized entity.

return
The entity-name.

		return entityMetamodel.getName();
	
protected final org.hibernate.engine.SessionFactoryImplementorgetFactory()

		return entityMetamodel.getSessionFactory();
	
public java.io.SerializablegetIdentifier(java.lang.Object entity)

		final Object id;
		if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
			id = entity;
		}
		else {
			if ( idGetter == null ) {
				if (identifierMapperType==null) {
					throw new HibernateException( "The class has no identifier property: " + getEntityName() );
				}
				else {
					ComponentType copier = (ComponentType) entityMetamodel.getIdentifierProperty().getType();
					id = copier.instantiate( getEntityMode() );
					copier.setPropertyValues( id, identifierMapperType.getPropertyValues( entity, getEntityMode() ), getEntityMode() );
				}
			}
			else {
				id = idGetter.get( entity );
			}
		}

		try {
			return (Serializable) id;
		}
		catch ( ClassCastException cce ) {
			StringBuffer msg = new StringBuffer( "Identifier classes must be serializable. " );
			if ( id != null ) {
				msg.append( id.getClass().getName() + " is not serializable. " );
			}
			if ( cce.getMessage() != null ) {
				msg.append( cce.getMessage() );
			}
			throw new ClassCastException( msg.toString() );
		}
	
protected final org.hibernate.tuple.InstantiatorgetInstantiator()

		return instantiator;
	
public java.lang.ObjectgetPropertyValue(java.lang.Object entity, int i)

		return getters[i].get( entity );
	
public java.lang.ObjectgetPropertyValue(java.lang.Object entity, java.lang.String propertyPath)

		
		int loc = propertyPath.indexOf('.");
		String basePropertyName = loc>0 ?
			propertyPath.substring(0, loc) : propertyPath;
			
		int index = entityMetamodel.getPropertyIndex( basePropertyName );
		Object baseValue = getPropertyValue( entity, index );
		if ( loc>0 ) {
			ComponentType type = (ComponentType) entityMetamodel.getPropertyTypes()[index];
			return getComponentValue( type, baseValue, propertyPath.substring(loc+1) );
		}
		else {
			return baseValue;
		}
	
public java.lang.Object[]getPropertyValues(java.lang.Object entity)

		boolean getAll = shouldGetAllProperties( entity );
		final int span = entityMetamodel.getPropertySpan();
		final Object[] result = new Object[span];

		for ( int j = 0; j < span; j++ ) {
			StandardProperty property = entityMetamodel.getProperties()[j];
			if ( getAll || !property.isLazy() ) {
				result[j] = getters[j].get( entity );
			}
			else {
				result[j] = LazyPropertyInitializer.UNFETCHED_PROPERTY;
			}
		}
		return result;
	
public java.lang.Object[]getPropertyValuesToInsert(java.lang.Object entity, java.util.Map mergeMap, org.hibernate.engine.SessionImplementor session)

		final int span = entityMetamodel.getPropertySpan();
		final Object[] result = new Object[span];

		for ( int j = 0; j < span; j++ ) {
			result[j] = getters[j].getForInsert( entity, mergeMap, session );
		}
		return result;
	
protected final org.hibernate.proxy.ProxyFactorygetProxyFactory()

		return proxyFactory;
	
protected java.util.SetgetSubclassEntityNames()
Retreives the defined entity-names for any subclasses defined for this entity.

return
Any subclass entity-names.

		return entityMetamodel.getSubclassEntityNames();
	
public java.lang.ObjectgetVersion(java.lang.Object entity)

		if ( !entityMetamodel.isVersioned() ) return null;
		return getters[ entityMetamodel.getVersionPropertyIndex() ].get( entity );
	
public booleanhasProxy()

		return entityMetamodel.isLazy();
	
public booleanhasUninitializedLazyProperties(java.lang.Object entity)

		// the default is to simply not lazy fetch properties for now...
		return false;
	
public final java.lang.Objectinstantiate(java.io.Serializable id)

		Object result = getInstantiator().instantiate( id );
		if ( id != null ) {
			setIdentifier( result, id );
		}
		return result;
	
public final java.lang.Objectinstantiate()

		return instantiate( null );
	
public final booleanisInstance(java.lang.Object object)

        return getInstantiator().isInstance( object );
	
public booleanisLifecycleImplementor()

		return false;
	
public booleanisValidatableImplementor()

		return false;
	
public voidresetIdentifier(java.lang.Object entity, java.io.Serializable currentId, java.lang.Object currentVersion)

		if ( entityMetamodel.getIdentifierProperty().getIdentifierGenerator() instanceof Assigned ) {
			//return currentId;
		}
		else {
			//reset the id
			Serializable result = entityMetamodel.getIdentifierProperty()
					.getUnsavedValue()
					.getDefaultValue( currentId );
			setIdentifier( entity, result );
			//reset the version
			VersionProperty versionProperty = entityMetamodel.getVersionProperty();
			if ( entityMetamodel.isVersioned() ) {
				setPropertyValue(
				        entity,
				        entityMetamodel.getVersionPropertyIndex(),
						versionProperty.getUnsavedValue().getDefaultValue( currentVersion )
					);
			}
			//return the id, so we can use it to reset the proxy id
			//return result;
		}
	
public voidsetIdentifier(java.lang.Object entity, java.io.Serializable id)

		if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
			if ( entity != id ) {
				AbstractComponentType copier = (AbstractComponentType) entityMetamodel.getIdentifierProperty().getType();
				copier.setPropertyValues( entity, copier.getPropertyValues( id, getEntityMode() ), getEntityMode() );
			}
		}
		else if ( idSetter != null ) {
			idSetter.set( entity, id, getFactory() );
		}
	
public voidsetPropertyValue(java.lang.Object entity, int i, java.lang.Object value)

		setters[i].set( entity, value, getFactory() );
	
public voidsetPropertyValue(java.lang.Object entity, java.lang.String propertyName, java.lang.Object value)

		setters[ entityMetamodel.getPropertyIndex( propertyName ) ].set( entity, value, getFactory() );
	
public voidsetPropertyValues(java.lang.Object entity, java.lang.Object[] values)

		boolean setAll = !entityMetamodel.hasLazyProperties();

		for ( int j = 0; j < entityMetamodel.getPropertySpan(); j++ ) {
			if ( setAll || values[j] != LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
				setters[j].set( entity, values[j], getFactory() );
			}
		}
	
protected booleanshouldGetAllProperties(java.lang.Object entity)

		return !hasUninitializedLazyProperties( entity );
	
public java.lang.StringtoString()

		return getClass().getName() + '(" + getEntityMetamodel().getName() + ')";