FileDocCategorySizeDatePackage
PojoEntityTuplizer.javaAPI DocHibernate 3.2.510425Thu Jul 13 19:09:20 BST 2006org.hibernate.tuple.entity

PojoEntityTuplizer

public class PojoEntityTuplizer extends org.hibernate.tuple.entity.AbstractEntityTuplizer
An {@link EntityTuplizer} specific to the pojo entity mode.
author
Steve Ebersole
author
Gavin King

Fields Summary
static final Log
log
private final Class
mappedClass
private final Class
proxyInterface
private final boolean
lifecycleImplementor
private final boolean
validatableImplementor
private final Set
lazyPropertyNames
private org.hibernate.bytecode.ReflectionOptimizer
optimizer
Constructors Summary
public PojoEntityTuplizer(org.hibernate.tuple.entity.EntityMetamodel entityMetamodel, org.hibernate.mapping.PersistentClass mappedEntity)


	     
		super( entityMetamodel, mappedEntity );
		this.mappedClass = mappedEntity.getMappedClass();
		this.proxyInterface = mappedEntity.getProxyInterface();
		this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
		this.validatableImplementor = Validatable.class.isAssignableFrom( mappedClass );

		Iterator iter = mappedEntity.getPropertyClosureIterator();
		while ( iter.hasNext() ) {
			Property property = (Property) iter.next();
			if ( property.isLazy() ) {
				lazyPropertyNames.add( property.getName() );
			}
		}

		String[] getterNames = new String[propertySpan];
		String[] setterNames = new String[propertySpan];
		Class[] propTypes = new Class[propertySpan];
		for ( int i = 0; i < propertySpan; i++ ) {
			getterNames[i] = getters[i].getMethodName();
			setterNames[i] = setters[i].getMethodName();
			propTypes[i] = getters[i].getReturnType();
		}

		if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
			optimizer = null;
		}
		else {
			// todo : YUCK!!!
			optimizer = Environment.getBytecodeProvider().getReflectionOptimizer( mappedClass, getterNames, setterNames, propTypes );
//			optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
//					mappedClass, getterNames, setterNames, propTypes
//			);
		}
	
	
Methods Summary
public voidafterInitialize(java.lang.Object entity, boolean lazyPropertiesAreUnfetched, org.hibernate.engine.SessionImplementor session)

		if ( isInstrumented() ) {
			Set lazyProps = lazyPropertiesAreUnfetched && getEntityMetamodel().hasLazyProperties() ?
					lazyPropertyNames : null;
			//TODO: if we support multiple fetch groups, we would need
			//      to clone the set of lazy properties!
			FieldInterceptionHelper.injectFieldInterceptor( entity, getEntityName(), lazyProps, session );
		}
	
protected org.hibernate.tuple.InstantiatorbuildInstantiator(org.hibernate.mapping.PersistentClass persistentClass)

		if ( optimizer == null ) {
			return new PojoInstantiator( persistentClass, null );
		}
		else {
			return new PojoInstantiator( persistentClass, optimizer.getInstantiationOptimizer() );
		}
	
protected org.hibernate.property.GetterbuildPropertyGetter(org.hibernate.mapping.Property mappedProperty, org.hibernate.mapping.PersistentClass mappedEntity)

		return mappedProperty.getGetter( mappedEntity.getMappedClass() );
	
protected org.hibernate.property.SetterbuildPropertySetter(org.hibernate.mapping.Property mappedProperty, org.hibernate.mapping.PersistentClass mappedEntity)

		return mappedProperty.getSetter( mappedEntity.getMappedClass() );
	
protected org.hibernate.proxy.ProxyFactorybuildProxyFactory(org.hibernate.mapping.PersistentClass persistentClass, org.hibernate.property.Getter idGetter, org.hibernate.property.Setter idSetter)

		// determine the id getter and setter methods from the proxy interface (if any)
        // determine all interfaces needed by the resulting proxy
		HashSet proxyInterfaces = new HashSet();
		proxyInterfaces.add( HibernateProxy.class );
		
		Class mappedClass = persistentClass.getMappedClass();
		Class proxyInterface = persistentClass.getProxyInterface();

		if ( proxyInterface!=null && !mappedClass.equals( proxyInterface ) ) {
			if ( !proxyInterface.isInterface() ) {
				throw new MappingException(
				        "proxy must be either an interface, or the class itself: " + 
				        getEntityName()
					);
			}
			proxyInterfaces.add( proxyInterface );
		}

		if ( mappedClass.isInterface() ) {
			proxyInterfaces.add( mappedClass );
		}

		Iterator iter = persistentClass.getSubclassIterator();
		while ( iter.hasNext() ) {
			Subclass subclass = ( Subclass ) iter.next();
			Class subclassProxy = subclass.getProxyInterface();
			Class subclassClass = subclass.getMappedClass();
			if ( subclassProxy!=null && !subclassClass.equals( subclassProxy ) ) {
				if ( !proxyInterface.isInterface() ) {
					throw new MappingException(
					        "proxy must be either an interface, or the class itself: " + 
					        subclass.getEntityName()
					);
				}
				proxyInterfaces.add( subclassProxy );
			}
		}

		Iterator properties = persistentClass.getPropertyIterator();
		Class clazz = persistentClass.getMappedClass();
		while ( properties.hasNext() ) {
			Property property = (Property) properties.next();
			Method method = property.getGetter(clazz).getMethod();
			if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
				log.error(
						"Getters of lazy classes cannot be final: " + persistentClass.getEntityName() + 
						"." + property.getName() 
					);
			}
			method = property.getSetter(clazz).getMethod();
            if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
				log.error(
						"Setters of lazy classes cannot be final: " + persistentClass.getEntityName() + 
						"." + property.getName() 
					);
			}
		}

		Method idGetterMethod = idGetter==null ? null : idGetter.getMethod();
		Method idSetterMethod = idSetter==null ? null : idSetter.getMethod();

		Method proxyGetIdentifierMethod = idGetterMethod==null || proxyInterface==null ? 
				null :
		        ReflectHelper.getMethod(proxyInterface, idGetterMethod);
		Method proxySetIdentifierMethod = idSetterMethod==null || proxyInterface==null  ? 
				null :
		        ReflectHelper.getMethod(proxyInterface, idSetterMethod);

		ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
		try {
			pf.postInstantiate(
					getEntityName(),
					mappedClass,
					proxyInterfaces,
					proxyGetIdentifierMethod,
					proxySetIdentifierMethod,
					persistentClass.hasEmbeddedIdentifier() ?
			                (AbstractComponentType) persistentClass.getIdentifier().getType() :
			                null
			);
		}
		catch ( HibernateException he ) {
			log.warn( "could not create proxy factory for:" + getEntityName(), he );
			pf = null;
		}
		return pf;
	
protected org.hibernate.proxy.ProxyFactorybuildProxyFactoryInternal(org.hibernate.mapping.PersistentClass persistentClass, org.hibernate.property.Getter idGetter, org.hibernate.property.Setter idSetter)

		// TODO : YUCK!!!  finx after HHH-1907 is complete
		return Environment.getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory();
//		return getFactory().getSettings().getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory();
	
public java.lang.ClassgetConcreteProxyClass()

		return proxyInterface;
	
public org.hibernate.EntityModegetEntityMode()

		return EntityMode.POJO;
	
public java.lang.ClassgetMappedClass()

		return mappedClass;
	
public java.lang.Object[]getPropertyValues(java.lang.Object entity)

		if ( shouldGetAllProperties( entity ) && optimizer != null && optimizer.getAccessOptimizer() != null ) {
			return getPropertyValuesWithOptimizer( entity );
		}
		else {
			return super.getPropertyValues( entity );
		}
	
public java.lang.Object[]getPropertyValuesToInsert(java.lang.Object entity, java.util.Map mergeMap, org.hibernate.engine.SessionImplementor session)

		if ( shouldGetAllProperties( entity ) && optimizer != null && optimizer.getAccessOptimizer() != null ) {
			return getPropertyValuesWithOptimizer( entity );
		}
		else {
			return super.getPropertyValuesToInsert( entity, mergeMap, session );
		}
	
protected java.lang.Object[]getPropertyValuesWithOptimizer(java.lang.Object object)

		return optimizer.getAccessOptimizer().getPropertyValues( object );
	
public booleanhasUninitializedLazyProperties(java.lang.Object entity)

		if ( getEntityMetamodel().hasLazyProperties() ) {
			FieldInterceptor callback = FieldInterceptionHelper.extractFieldInterceptor( entity );
			return callback != null && !callback.isInitialized();
		}
		else {
			return false;
		}
	
public booleanisInstrumented()

		return FieldInterceptionHelper.isInstrumented( getMappedClass() );
	
public booleanisLifecycleImplementor()

		return lifecycleImplementor;
	
public booleanisValidatableImplementor()

		return validatableImplementor;
	
public voidsetPropertyValues(java.lang.Object entity, java.lang.Object[] values)

		if ( !getEntityMetamodel().hasLazyProperties() && optimizer != null && optimizer.getAccessOptimizer() != null ) {
			setPropertyValuesWithOptimizer( entity, values );
		}
		else {
			super.setPropertyValues( entity, values );
		}
	
protected voidsetPropertyValuesWithOptimizer(java.lang.Object object, java.lang.Object[] values)

		optimizer.getAccessOptimizer().setPropertyValues( object, values );