FileDocCategorySizeDatePackage
AbstractPropertyMapping.javaAPI DocHibernate 3.2.57683Tue Nov 21 10:38:44 GMT 2006org.hibernate.persister.entity

AbstractPropertyMapping

public abstract class AbstractPropertyMapping extends Object implements PropertyMapping
Base implementation of a PropertyMapping
author
Gavin King

Fields Summary
private final Map
typesByPropertyPath
private final Map
columnsByPropertyPath
private final Map
formulaTemplatesByPropertyPath
Constructors Summary
Methods Summary
protected voidaddPropertyPath(java.lang.String path, org.hibernate.type.Type type, java.lang.String[] columns, java.lang.String[] formulaTemplates)

		typesByPropertyPath.put(path, type);
		columnsByPropertyPath.put(path, columns);
		if (formulaTemplates!=null) {
			formulaTemplatesByPropertyPath.put(path, formulaTemplates);
		}
	
private static java.lang.StringextendPath(java.lang.String path, java.lang.String property)

		if ( path==null || "".equals(path) ) {
			return property;
		}
		else {
			return StringHelper.qualify(path, property);
		}
	
public java.lang.String[]getColumnNames(java.lang.String propertyName)

		String[] cols = (String[]) columnsByPropertyPath.get(propertyName);
		if (cols==null) {
			throw new MappingException("unknown property: " + propertyName);
		}
		return cols;
	
protected abstract java.lang.StringgetEntityName()

public java.lang.String[]getIdentifierColumnNames()


	   
		throw new UnsupportedOperationException("one-to-one is not supported here");
	
private booleanhasNonIdentifierPropertyNamedId(org.hibernate.type.EntityType entityType, org.hibernate.engine.Mapping factory)

		// TODO : would be great to have a Mapping#hasNonIdentifierPropertyNamedId method
		// I don't believe that Mapping#getReferencedPropertyType accounts for the identifier property; so
		// if it returns for a property named 'id', then we should have a non-id field named id
		try {
			return factory.getReferencedPropertyType( entityType.getAssociatedEntityName(), EntityPersister.ENTITY_ID ) != null;
		}
		catch( MappingException e ) {
			return false;
		}
	
protected voidinitComponentPropertyPaths(java.lang.String path, org.hibernate.type.AbstractComponentType type, java.lang.String[] columns, java.lang.String[] formulaTemplates, org.hibernate.engine.Mapping factory)


		Type[] types = type.getSubtypes();
		String[] properties = type.getPropertyNames();
		int begin=0;
		for ( int i=0; i<properties.length; i++ ) {
			String subpath = extendPath( path, properties[i] );
			try {
				int length = types[i].getColumnSpan(factory);
				String[] columnSlice = ArrayHelper.slice(columns, begin, length);
				String[] formulaSlice = formulaTemplates==null ? 
						null : ArrayHelper.slice(formulaTemplates, begin, length);
				initPropertyPaths(subpath, types[i], columnSlice, formulaSlice, factory);
				begin+=length;
			}
			catch (Exception e) {
				throw new MappingException("bug in initComponentPropertyPaths", e);
			}
		}
	
protected voidinitIdentifierPropertyPaths(java.lang.String path, org.hibernate.type.EntityType etype, java.lang.String[] columns, org.hibernate.engine.Mapping factory)


		Type idtype = etype.getIdentifierOrUniqueKeyType( factory );
		String idPropName = etype.getIdentifierOrUniqueKeyPropertyName(factory);
		boolean hasNonIdentifierPropertyNamedId = hasNonIdentifierPropertyNamedId( etype, factory );

		if ( etype.isReferenceToPrimaryKey() ) {
			if ( !hasNonIdentifierPropertyNamedId ) {
				String idpath1 = extendPath(path, EntityPersister.ENTITY_ID);
				addPropertyPath(idpath1, idtype, columns, null);
				initPropertyPaths(idpath1, idtype, columns, null, factory);
			}
		}

		if (idPropName!=null) {
			String idpath2 = extendPath(path, idPropName);
			addPropertyPath(idpath2, idtype, columns, null);
			initPropertyPaths(idpath2, idtype, columns, null, factory);
		}
	
protected voidinitPropertyPaths(java.lang.String path, org.hibernate.type.Type type, java.lang.String[] columns, java.lang.String[] formulaTemplates, org.hibernate.engine.Mapping factory)

		
		if ( columns.length!=type.getColumnSpan(factory) ) {
			throw new MappingException( 
					"broken column mapping for: " + path + 
					" of: " + getEntityName() 
				);
		}

		if ( type.isAssociationType() ) {
			AssociationType actype = (AssociationType) type;
			if ( actype.useLHSPrimaryKey() ) {
				columns = getIdentifierColumnNames();
			}
			else {
				String foreignKeyProperty = actype.getLHSPropertyName();
				if ( foreignKeyProperty!=null && !path.equals(foreignKeyProperty) ) {
					//TODO: this requires that the collection is defined after the
					//      referenced property in the mapping file (ok?)
					columns = (String[]) columnsByPropertyPath.get(foreignKeyProperty);
					if (columns==null) return; //get em on the second pass!
				}
			}
		}

		if (path!=null) addPropertyPath(path, type, columns, formulaTemplates);

		if ( type.isComponentType() ) {
			AbstractComponentType actype = (AbstractComponentType) type;
			initComponentPropertyPaths( path, actype, columns, formulaTemplates, factory );
			if ( actype.isEmbedded() ) {
				initComponentPropertyPaths( 
						path==null ? null : StringHelper.qualifier(path), 
						actype, 
						columns, 
						formulaTemplates, 
						factory 
					);
			}
		}
		else if ( type.isEntityType() ) {
			initIdentifierPropertyPaths( path, (EntityType) type, columns, factory );
		}
	
protected final org.hibernate.QueryExceptionpropertyException(java.lang.String propertyName)

		return new QueryException( "could not resolve property: " + propertyName + " of: " + getEntityName() );
	
public java.lang.String[]toColumns(java.lang.String alias, java.lang.String propertyName)

		//TODO: *two* hashmap lookups here is one too many...
		String[] columns = (String[]) columnsByPropertyPath.get(propertyName);
		if ( columns == null ) {
			throw propertyException( propertyName );
		}
		String[] templates = (String[]) formulaTemplatesByPropertyPath.get(propertyName);
		String[] result = new String[columns.length];
		for ( int i=0; i<columns.length; i++ ) {
			if ( columns[i]==null ) {
				result[i] = StringHelper.replace( templates[i], Template.TEMPLATE, alias );
			}
			else {
				result[i] = StringHelper.qualify( alias, columns[i] );
			}
		}
		return result;
	
public java.lang.String[]toColumns(java.lang.String propertyName)

		String[] columns = (String[]) columnsByPropertyPath.get(propertyName);
		if ( columns == null ) {
			throw propertyException( propertyName );
		}
		String[] templates = (String[]) formulaTemplatesByPropertyPath.get(propertyName);
		String[] result = new String[columns.length];
		for ( int i=0; i<columns.length; i++ ) {
			if ( columns[i]==null ) {
				result[i] = StringHelper.replace( templates[i], Template.TEMPLATE, "" );
			}
			else {
				result[i] = columns[i];
			}
		}
		return result;
	
public org.hibernate.type.TypetoType(java.lang.String propertyName)

		Type type = (Type) typesByPropertyPath.get(propertyName);
		if ( type == null ) {
			throw propertyException( propertyName );
		}
		return type;