FileDocCategorySizeDatePackage
BasicCollectionPersister.javaAPI DocHibernate 3.2.59675Thu Jun 22 14:51:44 BST 2006org.hibernate.persister.collection

BasicCollectionPersister

public class BasicCollectionPersister extends AbstractCollectionPersister
Collection persister for collections of values and many-to-many associations.
author
Gavin King

Fields Summary
Constructors Summary
public BasicCollectionPersister(org.hibernate.mapping.Collection collection, org.hibernate.cache.CacheConcurrencyStrategy cache, org.hibernate.cfg.Configuration cfg, org.hibernate.engine.SessionFactoryImplementor factory)

		super( collection, cache, cfg, factory );
	
Methods Summary
public booleanconsumesCollectionAlias()

//		return !isOneToMany();
		return true;
	
public booleanconsumesEntityAlias()

		return false;
	
protected org.hibernate.loader.collection.CollectionInitializercreateCollectionInitializer(java.util.Map enabledFilters)
Create the CollectionLoader

see
org.hibernate.loader.collection.BasicCollectionLoader

		return BatchingCollectionInitializer.createBatchingCollectionInitializer( this, batchSize, getFactory(), enabledFilters );
	
protected org.hibernate.loader.collection.CollectionInitializercreateSubselectInitializer(org.hibernate.engine.SubselectFetch subselect, org.hibernate.engine.SessionImplementor session)

		return new SubselectCollectionLoader( 
				this,
				subselect.toSubselectString( getCollectionType().getLHSPropertyName() ),
				subselect.getResult(),
				subselect.getQueryParameters(),
				subselect.getNamedParameterLocMap(),
				session.getFactory(),
				session.getEnabledFilters() 
			);
	
protected intdoUpdateRows(java.io.Serializable id, org.hibernate.collection.PersistentCollection collection, org.hibernate.engine.SessionImplementor session)

		
		if ( ArrayHelper.isAllFalse(elementColumnIsSettable) ) return 0;

		try {
			PreparedStatement st = null;
			Expectation expectation = Expectations.appropriateExpectation( getUpdateCheckStyle() );
			boolean callable = isUpdateCallable();
			boolean useBatch = expectation.canBeBatched();
			Iterator entries = collection.entries( this );
			String sql = getSQLUpdateRowString();
			int i = 0;
			int count = 0;
			while ( entries.hasNext() ) {
				Object entry = entries.next();
				if ( collection.needsUpdating( entry, i, elementType ) ) {
					int offset = 1;

					if ( useBatch ) {
						if ( st == null ) {
							if ( callable ) {
								st = session.getBatcher().prepareBatchCallableStatement( sql );
							}
							else {
								st = session.getBatcher().prepareBatchStatement( sql );
							}
						}
					}
					else {
						if ( callable ) {
							st = session.getBatcher().prepareCallableStatement( sql );
						}
						else {
							st = session.getBatcher().prepareStatement( sql );
						}
					}

					try {
						offset+= expectation.prepare( st );
						int loc = writeElement( st, collection.getElement( entry ), offset, session );
						if ( hasIdentifier ) {
							writeIdentifier( st, collection.getIdentifier( entry, i ), loc, session );
						}
						else {
							loc = writeKey( st, id, loc, session );
							if ( hasIndex && !indexContainsFormula ) {
								writeIndexToWhere( st, collection.getIndex( entry, i, this ), loc, session );
							}
							else {
								writeElementToWhere( st, collection.getSnapshotElement( entry, i ), loc, session );
							}
						}

						if ( useBatch ) {
							session.getBatcher().addToBatch( expectation );
						}
						else {
							expectation.verifyOutcome( st.executeUpdate(), st, -1 );
						}
					}
					catch ( SQLException sqle ) {
						if ( useBatch ) {
							session.getBatcher().abortBatch( sqle );
						}
						throw sqle;
					}
					finally {
						if ( !useBatch ) {
							session.getBatcher().closeStatement( st );
						}
					}
					count++;
				}
				i++;
			}
			return count;
		}
		catch ( SQLException sqle ) {
			throw JDBCExceptionHelper.convert(
					getSQLExceptionConverter(),
					sqle,
					"could not update collection rows: " + MessageHelper.collectionInfoString( this, id, getFactory() ),
					getSQLUpdateRowString()
				);
		}
	
public java.lang.StringfromJoinFragment(java.lang.String alias, boolean innerJoin, boolean includeSubclasses)

		return "";
	
protected java.lang.StringgenerateDeleteRowString()
Generate the SQL DELETE that deletes a particular row

		
		Delete delete = new Delete()
			.setTableName( qualifiedTableName );
		
		if ( hasIdentifier ) {
			delete.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } );
		}
		else if ( hasIndex && !indexContainsFormula ) {
			delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
		}
		else {
			delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) );
		}
		
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			delete.setComment( "delete collection row " + getRole() );
		}
		
		return delete.toStatementString();
	
protected java.lang.StringgenerateDeleteString()
Generate the SQL DELETE that deletes all rows

		
		Delete delete = new Delete()
				.setTableName( qualifiedTableName )
				.setPrimaryKeyColumnNames( keyColumnNames );
		
		if ( hasWhere ) delete.setWhere( sqlWhereString );
		
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			delete.setComment( "delete collection " + getRole() );
		}
		
		return delete.toStatementString();
	
protected java.lang.StringgenerateInsertRowString()
Generate the SQL INSERT that creates a new row

		
		Insert insert = new Insert( getDialect() )
				.setTableName( qualifiedTableName )
				.addColumns( keyColumnNames );
		
		if ( hasIdentifier) insert.addColumn( identifierColumnName );
		
		if ( hasIndex /*&& !indexIsFormula*/ ) {
			insert.addColumns( indexColumnNames, indexColumnIsSettable );
		}
		
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			insert.setComment( "insert collection row " + getRole() );
		}
		
		//if ( !elementIsFormula ) {
			insert.addColumns( elementColumnNames, elementColumnIsSettable );
		//}
		
		return insert.toStatementString();
	
protected java.lang.StringgenerateUpdateRowString()
Generate the SQL UPDATE that updates a row

		
		Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName );
		
		//if ( !elementIsFormula ) {
			update.addColumns( elementColumnNames, elementColumnIsSettable );
		//}
		
		if ( hasIdentifier ) {
			update.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } );
		}
		else if ( hasIndex && !indexContainsFormula ) {
			update.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
		}
		else {
			update.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) );
		}
		
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			update.setComment( "update collection row " + getRole() );
		}
		
		return update.toStatementString();
	
public booleanisCascadeDeleteEnabled()

		return false;
	
public booleanisManyToMany()

		return elementType.isEntityType(); //instanceof AssociationType;
	
public booleanisOneToMany()

		return false;
	
private java.lang.StringmanyToManySelectFragment(org.hibernate.persister.entity.Joinable rhs, java.lang.String rhsAlias, java.lang.String lhsAlias, java.lang.String collectionSuffix)

		SelectFragment frag = generateSelectFragment( lhsAlias, collectionSuffix );

		String[] elementColumnNames = rhs.getKeyColumnNames();
		frag.addColumns( rhsAlias, elementColumnNames, elementColumnAliases );
		appendIndexColumns( frag, lhsAlias );
		appendIdentifierColumns( frag, lhsAlias );

		return frag.toFragmentString()
				.substring( 2 ); //strip leading ','
	
public java.lang.StringselectFragment(org.hibernate.persister.entity.Joinable rhs, java.lang.String rhsAlias, java.lang.String lhsAlias, java.lang.String entitySuffix, java.lang.String collectionSuffix, boolean includeCollectionColumns)

		// we need to determine the best way to know that two joinables
		// represent a single many-to-many...
		if ( rhs != null && isManyToMany() && !rhs.isCollection() ) {
			AssociationType elementType = ( ( AssociationType ) getElementType() );
			if ( rhs.equals( elementType.getAssociatedJoinable( getFactory() ) ) ) {
				return manyToManySelectFragment( rhs, rhsAlias, lhsAlias, collectionSuffix );
			}
		}
		return includeCollectionColumns ? selectFragment( lhsAlias, collectionSuffix ) : "";
	
public java.lang.StringwhereJoinFragment(java.lang.String alias, boolean innerJoin, boolean includeSubclasses)

		return "";