FileDocCategorySizeDatePackage
SessionFactoryHelper.javaAPI DocHibernate 3.2.513646Thu Nov 16 12:33:20 GMT 2006org.hibernate.hql.ast.util

SessionFactoryHelper

public class SessionFactoryHelper extends Object
Helper for performing common and/or complex operations with the {@link SessionFactoryImplementor} during translation of an HQL query.
author
Joshua Davis

Fields Summary
private org.hibernate.engine.SessionFactoryImplementor
sfi
private Map
collectionPropertyMappingByRole
Constructors Summary
public SessionFactoryHelper(org.hibernate.engine.SessionFactoryImplementor sfi)
Construct a new SessionFactoryHelper instance.

param
sfi The SessionFactory impl to be encapsualted.

		this.sfi = sfi;
		collectionPropertyMappingByRole = new HashMap();
	
Methods Summary
public org.hibernate.engine.JoinSequencecreateCollectionJoinSequence(org.hibernate.persister.collection.QueryableCollection collPersister, java.lang.String collectionName)
Create a join sequence rooted at the given collection.

param
collPersister The persister for the collection at which the join should be rooted.
param
collectionName The alias to use for qualifying column references.
return
The generated join sequence.

		JoinSequence joinSequence = createJoinSequence();
		joinSequence.setRoot( collPersister, collectionName );
		joinSequence.setUseThetaStyle( true );		// TODO: figure out how this should be set.
///////////////////////////////////////////////////////////////////////////////
// This was the reason for failures regarding INDEX_OP and subclass joins on
// theta-join dialects; not sure what behaviour we were trying to emulate ;)
//		joinSequence = joinSequence.getFromPart();	// Emulate the old addFromOnly behavior.
		return joinSequence;
	
public org.hibernate.engine.JoinSequencecreateJoinSequence()
Generate an empty join sequence instance.

return
The generate join sequence.

		return new JoinSequence( sfi );
	
public org.hibernate.engine.JoinSequencecreateJoinSequence(boolean implicit, org.hibernate.type.AssociationType associationType, java.lang.String tableAlias, int joinType, java.lang.String[] columns)
Generate a join sequence representing the given association type.

param
implicit Should implicit joins (theta-style) or explicit joins (ANSI-style) be rendered
param
associationType The type representing the thing to be joined into.
param
tableAlias The table alias to use in qualifing the join conditions
param
joinType The type of join to render (inner, outer, etc); see {@link org.hibernate.sql.JoinFragment}
param
columns The columns making up the condition of the join.
return
The generated join sequence.

		JoinSequence joinSequence = createJoinSequence();
		joinSequence.setUseThetaStyle( implicit );	// Implicit joins use theta style (WHERE pk = fk), explicit joins use JOIN (after from)
		joinSequence.addJoin( associationType, tableAlias, joinType, columns );
		return joinSequence;
	
private org.hibernate.persister.entity.EntityPersisterfindEntityPersisterByName(java.lang.String name)
Locate the persister by class or entity name.

param
name The class or entity name
return
The defined persister for this entity, or null if none found.
throws
MappingException

		// First, try to get the persister using the given name directly.
		try {
			return sfi.getEntityPersister( name );
		}
		catch ( MappingException ignore ) {
			// unable to locate it using this name
		}

		// If that didn't work, try using the 'import' name.
		String importedClassName = sfi.getImportedClassName( name );
		if ( importedClassName == null ) {
			return null;
		}
		return sfi.getEntityPersister( importedClassName );
	
public org.hibernate.type.TypefindFunctionReturnType(java.lang.String functionName, antlr.collections.AST first)
Find the function return type given the function name and the first argument expression node.

param
functionName The function name.
param
first The first argument expression.
return
the function return type given the function name and the first argument expression node.

		// locate the registered function by the given name
		SQLFunction sqlFunction = requireSQLFunction( functionName );

		// determine the type of the first argument...
		Type argumentType = null;
		if ( first != null ) {
			if ( "cast".equals(functionName) ) {
				argumentType = TypeFactory.heuristicType( first.getNextSibling().getText() );
			}
			else if ( first instanceof SqlNode ) {
				argumentType = ( (SqlNode) first ).getDataType();
			}
		}

		return sqlFunction.getReturnType( argumentType, sfi );
	
public org.hibernate.persister.entity.QueryablefindQueryableUsingImports(java.lang.String className)
Given a (potentially unqualified) class name, locate its persister.

param
className The (potentially unqualified) class name.
return
The defined persister for this class, or null if none found.

		return findQueryableUsingImports( sfi, className );
	
public static org.hibernate.persister.entity.QueryablefindQueryableUsingImports(org.hibernate.engine.SessionFactoryImplementor sfi, java.lang.String className)
Given a (potentially unqualified) class name, locate its persister.

param
sfi The session factory implementor.
param
className The (potentially unqualified) class name.
return
The defined persister for this class, or null if none found.

		final String importedClassName = sfi.getImportedClassName( className );
		if ( importedClassName == null ) {
			return null;
		}
		try {
			return ( Queryable ) sfi.getEntityPersister( importedClassName );
		}
		catch ( MappingException me ) {
			return null;
		}
	
public org.hibernate.dialect.function.SQLFunctionfindSQLFunction(java.lang.String functionName)
Locate a registered sql function by name.

param
functionName The name of the function to locate
return
The sql function, or null if not found.

		return sfi.getSqlFunctionRegistry().findSQLFunction( functionName.toLowerCase() );
	
public java.lang.String[][]generateColumnNames(org.hibernate.type.Type[] sqlResultTypes)

		return NameGenerator.generateColumnNames( sqlResultTypes, sfi );
	
public java.lang.StringgetAssociatedEntityName(org.hibernate.type.CollectionType collectionType)
Given a collection type, determine the entity name of the elements contained within instance of that collection.

param
collectionType The collection type to check.
return
The entity name of the elements of this collection.

		return collectionType.getAssociatedEntityName( sfi );
	
public java.lang.String[]getCollectionElementColumns(java.lang.String role, java.lang.String roleAlias)
Retrieves the column names corresponding to the collection elements for the given collection role.

param
role The collection role
param
roleAlias The sql column-qualification alias (i.e., the table alias)
return
the collection element columns

		return getCollectionPropertyMapping( role ).toColumns( roleAlias, CollectionPropertyNames.COLLECTION_ELEMENTS );
	
public org.hibernate.persister.collection.QueryableCollectiongetCollectionPersister(java.lang.String role)
Locate the collection persister by the collection role.

param
role The collection role name.
return
The defined CollectionPersister for this collection role, or null.

		try {
			return ( QueryableCollection ) sfi.getCollectionPersister( role );
		}
		catch ( ClassCastException cce ) {
			throw new QueryException( "collection is not queryable: " + role );
		}
		catch ( Exception e ) {
			throw new QueryException( "collection not found: " + role );
		}
	
private org.hibernate.persister.entity.PropertyMappinggetCollectionPropertyMapping(java.lang.String role)
Retreive a PropertyMapping describing the given collection role.

param
role The collection role for whcih to retrieve the property mapping.
return
The property mapping.

		return ( PropertyMapping ) collectionPropertyMappingByRole.get( role );
	
public intgetColumnSpan(org.hibernate.type.Type type)
Retreive the number of columns represented by this type.

param
type The type.
return
The number of columns.

		return type.getColumnSpan( sfi );
	
public org.hibernate.type.AssociationTypegetElementAssociationType(org.hibernate.type.CollectionType collectionType)
Essentially the same as {@link #getElementType}, but requiring that the element type be an association type.

param
collectionType The collection type to be checked.
return
The AssociationType of the elements of the collection.

		return ( AssociationType ) getElementType( collectionType );
	
private org.hibernate.type.TypegetElementType(org.hibernate.type.CollectionType collectionType)
Given a collection type, determine the Type representing elements within instances of that collection.

param
collectionType The collection type to be checked.
return
The Type of the elements of the collection.

		return collectionType.getElementType( sfi );
	
public org.hibernate.engine.SessionFactoryImplementorgetFactory()
Get a handle to the encapsulated SessionFactory.

return
The encapsulated SessionFactory.

		return sfi;
	
public java.lang.StringgetIdentifierOrUniqueKeyPropertyName(org.hibernate.type.EntityType entityType)
Determine the name of the property for the entity encapsulated by the given type which represents the id or unique-key.

param
entityType The type representing the entity.
return
The corresponding property name
throws
QueryException Indicates such a property could not be found.

		try {
			return entityType.getIdentifierOrUniqueKeyPropertyName( sfi );
		}
		catch ( MappingException me ) {
			throw new QueryException( me );
		}
	
public java.lang.StringgetImportedClassName(java.lang.String className)
Given a (potentially unqualified) class name, locate its imported qualified name.

param
className The potentially unqualified class name
return
The qualified class name.

		return sfi.getImportedClassName( className );
	
public booleanhasPhysicalDiscriminatorColumn(org.hibernate.persister.entity.Queryable persister)
Does the given persister define a physical discriminator column for the purpose of inheritence discrimination?

param
persister The persister to be checked.
return
True if the persister does define an actual discriminator column.

		if ( persister.getDiscriminatorType() != null ) {
			String discrimColumnName = persister.getDiscriminatorColumnName();
			// Needed the "clazz_" check to work around union-subclasses
			// TODO : is there a way to tell whether a persister is truly discrim-column based inheritence?
			if ( discrimColumnName != null && !"clazz_".equals( discrimColumnName ) ) {
				return true;
			}
		}
		return false;
	
public booleanisStrictJPAQLComplianceEnabled()

		return sfi.getSettings().isStrictJPAQLCompliance();
	
public org.hibernate.persister.entity.EntityPersisterrequireClassPersister(java.lang.String name)
Locate the persister by class or entity name, requiring that such a persister exist.

param
name The class or entity name
return
The defined persister for this entity
throws
SemanticException Indicates the persister could not be found

		EntityPersister cp;
		try {
			cp = findEntityPersisterByName( name );
			if ( cp == null ) {
				throw new QuerySyntaxException( name + " is not mapped" );
			}
		}
		catch ( MappingException e ) {
			throw new DetailedSemanticException( e.getMessage(), e );
		}
		return cp;
	
public org.hibernate.persister.collection.QueryableCollectionrequireQueryableCollection(java.lang.String role)
Locate the collection persister by the collection role, requiring that such a persister exist.

param
role The collection role name.
return
The defined CollectionPersister for this collection role.
throws
QueryException Indicates that the collection persister could not be found.

		try {
			QueryableCollection queryableCollection = ( QueryableCollection ) sfi.getCollectionPersister( role );
			if ( queryableCollection != null ) {
				collectionPropertyMappingByRole.put( role, new CollectionPropertyMapping( queryableCollection ) );
			}
			return queryableCollection;
		}
		catch ( ClassCastException cce ) {
			throw new QueryException( "collection role is not queryable: " + role );
		}
		catch ( Exception e ) {
			throw new QueryException( "collection role not found: " + role );
		}
	
private org.hibernate.dialect.function.SQLFunctionrequireSQLFunction(java.lang.String functionName)
Locate a registered sql function by name, requiring that such a registered function exist.

param
functionName The name of the function to locate
return
The sql function.
throws
QueryException Indicates no matching sql functions could be found.

		SQLFunction f = findSQLFunction( functionName );
		if ( f == null ) {
			throw new QueryException( "Unable to find SQL function: " + functionName );
		}
		return f;