Java Code Examples for org.hibernate.persister.collection.QueryableCollection#getElementPersister()

The following examples show how to use org.hibernate.persister.collection.QueryableCollection#getElementPersister() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionElementLoader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CollectionElementLoader(
		QueryableCollection collectionPersister,
		SessionFactoryImplementor factory,
		LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
	super( factory, loadQueryInfluencers );

	this.keyType = collectionPersister.getKeyType();
	this.indexType = collectionPersister.getIndexType();
	this.persister = (OuterJoinLoadable) collectionPersister.getElementPersister();
	this.entityName = persister.getEntityName();

	JoinWalker walker = new EntityJoinWalker(
			persister,
			ArrayHelper.join(
					collectionPersister.getKeyColumnNames(),
					collectionPersister.toColumns( "index" )
			),
			1,
			LockMode.NONE,
			factory,
			loadQueryInfluencers
	);
	initFromWalker( walker );

	postInstantiate();

	if ( LOG.isDebugEnabled() ) {
		LOG.debugf( "Static select for entity %s: %s", entityName, getSQLString() );
	}

}
 
Example 2
Source File: OneToManyJoinWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public OneToManyJoinWalker(
		QueryableCollection oneToManyPersister,
		int batchSize,
		String subquery,
		SessionFactoryImplementor factory,
		LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
	super( factory, loadQueryInfluencers );

	this.oneToManyPersister = oneToManyPersister;

	final OuterJoinLoadable elementPersister = (OuterJoinLoadable) oneToManyPersister.getElementPersister();
	final String alias = generateRootAlias( oneToManyPersister.getRole() );

	walkEntityTree( elementPersister, alias );

	List allAssociations = new ArrayList();
	allAssociations.addAll( associations );
	allAssociations.add(
			OuterJoinableAssociation.createRoot(
					oneToManyPersister.getCollectionType(),
					alias,
					getFactory()
			)
	);
	initPersisters( allAssociations, LockMode.NONE );
	initStatementString( elementPersister, alias, batchSize, subquery );
}
 
Example 3
Source File: FromElement.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private TypeDiscriminatorMetadata buildTypeDiscriminatorMetadata() {
	final String aliasToUse = getTableAlias();
	Queryable queryable = getQueryable();
	if ( queryable == null ) {
		QueryableCollection collection = getQueryableCollection();
		if ( ! collection.getElementType().isEntityType() ) {
			throw new QueryException( "type discrimination cannot be applied to value collection [" + collection.getRole() + "]" );
		}
		queryable = (Queryable) collection.getElementPersister();
	}

	handlePropertyBeingDereferenced( getDataType(), DISCRIMINATOR_PROPERTY_NAME );

	return new TypeDiscriminatorMetadataImpl( queryable.getTypeDiscriminatorMetadata(), aliasToUse );
}
 
Example 4
Source File: PathExpressionParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public String addFromCollection(QueryTranslatorImpl q) throws QueryException {
	Type collectionElementType = getPropertyType();

	if ( collectionElementType == null ) {
		throw new QueryException( "must specify 'elements' for collection valued property in from clause: " + path );
	}

	if ( collectionElementType.isEntityType() ) {
		// an association
		QueryableCollection collectionPersister = q.getCollectionPersister( collectionRole );
		Queryable entityPersister = ( Queryable ) collectionPersister.getElementPersister();
		String clazz = entityPersister.getEntityName();

		final String elementName;
		if ( collectionPersister.isOneToMany() ) {
			elementName = collectionName;
			//allow index() function:
			q.decoratePropertyMapping( elementName, collectionPersister );
		}
		else { //many-to-many
			q.addCollection( collectionName, collectionRole );
			elementName = q.createNameFor( clazz );
			addJoin( elementName, ( AssociationType ) collectionElementType );
		}
		q.addFrom( elementName, clazz, joinSequence );
		currentPropertyMapping = new CollectionPropertyMapping( collectionPersister );
		return elementName;
	}
	else {
		// collections of values
		q.addFromCollection( collectionName, collectionRole, joinSequence );
		return collectionName;
	}

}
 
Example 5
Source File: CollectionElementLoader.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public CollectionElementLoader(
		QueryableCollection collectionPersister,
		SessionFactoryImplementor factory, 
		Map enabledFilters) 
throws MappingException {
	super(factory, enabledFilters);

	this.keyType = collectionPersister.getKeyType();
	this.indexType = collectionPersister.getIndexType();
	this.persister = (OuterJoinLoadable) collectionPersister.getElementPersister();
	this.entityName = persister.getEntityName();
	
	JoinWalker walker = new EntityJoinWalker(
			persister, 
			ArrayHelper.join( 
					collectionPersister.getKeyColumnNames(), 
					collectionPersister.getIndexColumnNames()
				),
			1, 
			LockMode.NONE, 
			factory, 
			enabledFilters
		);
	initFromWalker( walker );

	postInstantiate();
	
	log.debug( "Static select for entity " + entityName + ": " + getSQLString() );

}
 
Example 6
Source File: OneToManyJoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public OneToManyJoinWalker(
		QueryableCollection oneToManyPersister, 
		int batchSize, 
		String subquery, 
		SessionFactoryImplementor factory, 
		Map enabledFilters)
throws MappingException {

	super(factory, enabledFilters);

	this.oneToManyPersister = oneToManyPersister;

	final OuterJoinLoadable elementPersister = (OuterJoinLoadable) oneToManyPersister.getElementPersister();
	final String alias = generateRootAlias( oneToManyPersister.getRole() );

	walkEntityTree(elementPersister, alias);

	List allAssociations = new ArrayList();
	allAssociations.addAll(associations);
	allAssociations.add( new OuterJoinableAssociation( 
			oneToManyPersister.getCollectionType(),
			null, 
			null, 
			alias, 
			JoinFragment.LEFT_OUTER_JOIN, 
			getFactory(), 
			CollectionHelper.EMPTY_MAP 
		) );
	
	initPersisters(allAssociations, LockMode.NONE);
	initStatementString(elementPersister, alias, batchSize, subquery);

}
 
Example 7
Source File: PathExpressionParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String addFromCollection(QueryTranslatorImpl q) throws QueryException {
	Type collectionElementType = getPropertyType();

	if ( collectionElementType == null ) {
		throw new QueryException( "must specify 'elements' for collection valued property in from clause: " + path );
	}

	if ( collectionElementType.isEntityType() ) {
		// an association
		QueryableCollection collectionPersister = q.getCollectionPersister( collectionRole );
		Queryable entityPersister = ( Queryable ) collectionPersister.getElementPersister();
		String clazz = entityPersister.getEntityName();

		final String elementName;
		if ( collectionPersister.isOneToMany() ) {
			elementName = collectionName;
			//allow index() function:
			q.decoratePropertyMapping( elementName, collectionPersister );
		}
		else { //many-to-many
			q.addCollection( collectionName, collectionRole );
			elementName = q.createNameFor( clazz );
			addJoin( elementName, ( AssociationType ) collectionElementType );
		}
		q.addFrom( elementName, clazz, joinSequence );
		currentPropertyMapping = new CollectionPropertyMapping( collectionPersister );
		return elementName;
	}
	else {
		// collections of values
		q.addFromCollection( collectionName, collectionRole, joinSequence );
		return collectionName;
	}

}
 
Example 8
Source File: JoinWalker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void initPersisters(
		final List associations,
		final LockOptions lockOptions,
		final AssociationInitCallback callback) throws MappingException {
	final int joins = countEntityPersisters( associations );
	final int collections = countCollectionPersisters( associations );

	collectionOwners = collections == 0 ? null : new int[collections];
	collectionPersisters = collections == 0 ? null : new CollectionPersister[collections];
	collectionSuffixes = BasicLoader.generateSuffixes( joins + 1, collections );

	this.lockOptions = lockOptions;

	persisters = new Loadable[joins];
	aliases = new String[joins];
	owners = new int[joins];
	ownerAssociationTypes = new EntityType[joins];
	lockModeArray = ArrayHelper.fillArray( lockOptions.getLockMode(), joins );

	int i = 0;
	int j = 0;
	for ( Object association : associations ) {
		final OuterJoinableAssociation oj = (OuterJoinableAssociation) association;
		if ( !oj.isCollection() ) {

			persisters[i] = (Loadable) oj.getJoinable();
			aliases[i] = oj.getRHSAlias();
			owners[i] = oj.getOwner( associations );
			ownerAssociationTypes[i] = (EntityType) oj.getJoinableType();
			callback.associationProcessed( oj, i );
			i++;

		}
		else {

			QueryableCollection collPersister = (QueryableCollection) oj.getJoinable();
			if ( oj.getJoinType() == JoinType.LEFT_OUTER_JOIN && !oj.hasRestriction() ) {
				//it must be a collection fetch
				collectionPersisters[j] = collPersister;
				collectionOwners[j] = oj.getOwner( associations );
				j++;
			}

			if ( collPersister.isOneToMany() ) {
				persisters[i] = (Loadable) collPersister.getElementPersister();
				aliases[i] = oj.getRHSAlias();
				callback.associationProcessed( oj, i );
				i++;
			}
		}
	}

	if ( ArrayHelper.isAllNegative( owners ) ) {
		owners = null;
	}
	if ( collectionOwners != null && ArrayHelper.isAllNegative( collectionOwners ) ) {
		collectionOwners = null;
	}
}
 
Example 9
Source File: FromElementFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
FromElement createElementJoin(QueryableCollection queryableCollection) throws SemanticException {
		FromElement elem;

		implied = true; //TODO: always true for now, but not if we later decide to support elements() in the from clause
		inElementsFunction = true;
		Type elementType = queryableCollection.getElementType();
		if ( !elementType.isEntityType() ) {
			throw new IllegalArgumentException( "Cannot create element join for a collection of non-entities!" );
		}
		this.queryableCollection = queryableCollection;
		SessionFactoryHelper sfh = fromClause.getSessionFactoryHelper();
		FromElement destination = null;
		String tableAlias = null;
		EntityPersister entityPersister = queryableCollection.getElementPersister();
		tableAlias = fromClause.getAliasGenerator().createName( entityPersister.getEntityName() );
		String associatedEntityName = entityPersister.getEntityName();
		EntityPersister targetEntityPersister = sfh.requireClassPersister( associatedEntityName );
		// Create the FROM element for the target (the elements of the collection).
		destination = createAndAddFromElement(
				associatedEntityName,
				classAlias,
				targetEntityPersister,
				(EntityType) queryableCollection.getElementType(),
				tableAlias
		);
		// If the join is implied, then don't include sub-classes on the element.
		if ( implied ) {
			destination.setIncludeSubclasses( false );
		}
		fromClause.addCollectionJoinFromElementByPath( path, destination );
//		origin.addDestination(destination);
		// Add the query spaces.
		fromClause.getWalker().addQuerySpaces( entityPersister.getQuerySpaces() );

		CollectionType type = queryableCollection.getCollectionType();
		String role = type.getRole();
		String roleAlias = origin.getTableAlias();

		String[] targetColumns = sfh.getCollectionElementColumns( role, roleAlias );
		AssociationType elementAssociationType = sfh.getElementAssociationType( type );

		// Create the join element under the from element.
		JoinType joinType = JoinType.INNER_JOIN;
		JoinSequence joinSequence = sfh.createJoinSequence(
				implied,
				elementAssociationType,
				tableAlias,
				joinType,
				targetColumns
		);
		elem = initializeJoin( path, destination, joinSequence, targetColumns, origin, false );
		elem.setUseFromFragment( true );    // The associated entity is implied, but it must be included in the FROM.
		elem.setCollectionTableAlias( roleAlias );    // The collection alias is the role.
		return elem;
	}
 
Example 10
Source File: JoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void initPersisters(final List associations, final LockMode lockMode) throws MappingException {
	
	final int joins = countEntityPersisters(associations);
	final int collections = countCollectionPersisters(associations);

	collectionOwners = collections==0 ? null : new int[collections];
	collectionPersisters = collections==0 ? null : new CollectionPersister[collections];
	collectionSuffixes = BasicLoader.generateSuffixes( joins + 1, collections );

	persisters = new Loadable[joins];
	aliases = new String[joins];
	owners = new int[joins];
	ownerAssociationTypes = new EntityType[joins];
	lockModeArray = ArrayHelper.fillArray(lockMode, joins);
	
	int i=0;
	int j=0;
	Iterator iter = associations.iterator();
	while ( iter.hasNext() ) {
		final OuterJoinableAssociation oj = (OuterJoinableAssociation) iter.next();
		if ( !oj.isCollection() ) {
			
			persisters[i] = (Loadable) oj.getJoinable();
			aliases[i] = oj.getRHSAlias();
			owners[i] = oj.getOwner(associations);
			ownerAssociationTypes[i] = (EntityType) oj.getJoinableType();
			i++;
			
		}
		else {
			
			QueryableCollection collPersister = (QueryableCollection) oj.getJoinable();
			if ( oj.getJoinType()==JoinFragment.LEFT_OUTER_JOIN ) {
				//it must be a collection fetch
				collectionPersisters[j] = collPersister;
				collectionOwners[j] = oj.getOwner(associations);
				j++;
			}

			if ( collPersister.isOneToMany() ) {
				persisters[i] = (Loadable) collPersister.getElementPersister();
				aliases[i] = oj.getRHSAlias();
				i++;
			}
		}
	}

	if ( ArrayHelper.isAllNegative(owners) ) owners = null;
	if ( collectionOwners!=null && ArrayHelper.isAllNegative(collectionOwners) ) {
		collectionOwners = null;
	}
}
 
Example 11
Source File: FromElementFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
FromElement createElementJoin(QueryableCollection queryableCollection) throws SemanticException {
		FromElement elem;

		implied = true; //TODO: always true for now, but not if we later decide to support elements() in the from clause
		inElementsFunction = true;
		Type elementType = queryableCollection.getElementType();
		if ( !elementType.isEntityType() ) {
			throw new IllegalArgumentException( "Cannot create element join for a collection of non-entities!" );
		}
		this.queryableCollection = queryableCollection;
		SessionFactoryHelper sfh = fromClause.getSessionFactoryHelper();
		FromElement destination = null;
		String tableAlias = null;
		EntityPersister entityPersister = queryableCollection.getElementPersister();
		tableAlias = fromClause.getAliasGenerator().createName( entityPersister.getEntityName() );
		String associatedEntityName = entityPersister.getEntityName();
		EntityPersister targetEntityPersister = sfh.requireClassPersister( associatedEntityName );
		// Create the FROM element for the target (the elements of the collection).
		destination = createAndAddFromElement( 
				associatedEntityName,
				classAlias,
				targetEntityPersister,
				( EntityType ) queryableCollection.getElementType(),
				tableAlias
			);
		// If the join is implied, then don't include sub-classes on the element.
		if ( implied ) {
			destination.setIncludeSubclasses( false );
		}
		fromClause.addCollectionJoinFromElementByPath( path, destination );
//		origin.addDestination(destination);
		// Add the query spaces.
		fromClause.getWalker().addQuerySpaces( entityPersister.getQuerySpaces() );

		CollectionType type = queryableCollection.getCollectionType();
		String role = type.getRole();
		String roleAlias = origin.getTableAlias();

		String[] targetColumns = sfh.getCollectionElementColumns( role, roleAlias );
		AssociationType elementAssociationType = sfh.getElementAssociationType( type );

		// Create the join element under the from element.
		int joinType = JoinFragment.INNER_JOIN;
		JoinSequence joinSequence = sfh.createJoinSequence( implied, elementAssociationType, tableAlias, joinType, targetColumns );
		elem = initializeJoin( path, destination, joinSequence, targetColumns, origin, false );
		elem.setUseFromFragment( true );	// The associated entity is implied, but it must be included in the FROM.
		elem.setCollectionTableAlias( roleAlias );	// The collection alias is the role.
		return elem;
	}