下面列出了org.hibernate.persister.collection.QueryableCollection#getElementType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void determineValueSelectExpressions(QueryableCollection collectionPersister, List selections) {
AliasGenerator aliasGenerator = new LocalAliasGenerator( 1 );
appendSelectExpressions( collectionPersister.getElementColumnNames(), selections, aliasGenerator );
Type valueType = collectionPersister.getElementType();
if ( valueType.isAssociationType() ) {
EntityType valueEntityType = (EntityType) valueType;
Queryable valueEntityPersister = (Queryable) sfi().getEntityPersister(
valueEntityType.getAssociatedEntityName( sfi() )
);
SelectFragment fragment = valueEntityPersister.propertySelectFragmentFragment(
elementTableAlias(),
null,
false
);
appendSelectExpressions( fragment, selections, aliasGenerator );
}
}
ComponentCollectionCriteriaInfoProvider(QueryableCollection persister) {
this.persister = persister;
if ( !persister.getElementType().isComponentType() ) {
throw new IllegalArgumentException( "persister for role " + persister.getRole() + " is not a collection-of-component" );
}
CompositeType componentType = (CompositeType) persister.getElementType();
String[] names = componentType.getPropertyNames();
Type[] types = componentType.getSubtypes();
for ( int i = 0; i < names.length; i++ ) {
subTypes.put( names[i], types[i] );
}
}
public boolean isManyToManyWith(OuterJoinableAssociation other) {
if ( joinable.isCollection() ) {
QueryableCollection persister = (QueryableCollection) joinable;
if ( persister.isManyToMany() ) {
return persister.getElementType() == other.getJoinableType();
}
}
return false;
}
@Override
@SuppressWarnings("unchecked")
protected Type resolveType(QueryableCollection collectionPersister) {
final Type keyType = collectionPersister.getIndexType();
final Type valueType = collectionPersister.getElementType();
types.add( keyType );
types.add( valueType );
mapEntryBuilder = new MapEntryBuilder();
// an entry (as an aggregated select expression) does not have a type...
return null;
}
private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
QueryableCollection collPersister = q.getCollectionPersister( collectionRole );
if ( !collPersister.hasIndex() ) {
throw new QueryException( "unindexed collection before []: " + path );
}
String[] indexCols = collPersister.getIndexColumnNames();
if ( indexCols.length != 1 ) {
throw new QueryException( "composite-index appears in []: " + path );
}
//String[] keyCols = collPersister.getKeyColumnNames();
JoinSequence fromJoins = new JoinSequence( q.getFactory() )
.setUseThetaStyle( useThetaStyleJoin )
.setRoot( collPersister, collectionName )
.setNext( joinSequence.copy() );
if ( !continuation ) {
addJoin( collectionName, collPersister.getCollectionType() );
}
joinSequence.addCondition( collectionName + '.' + indexCols[0] + " = " ); //TODO: get SQL rendering out of here
CollectionElement elem = new CollectionElement();
elem.elementColumns = collPersister.getElementColumnNames(collectionName);
elem.elementType = collPersister.getElementType();
elem.isOneToMany = collPersister.isOneToMany();
elem.alias = collectionName;
elem.joinSequence = joinSequence;
collectionElements.addLast( elem );
setExpectingCollectionIndex();
q.addCollection( collectionName, collectionRole );
q.addFromJoinOnly( collectionName, fromJoins );
}
/**
* Used for collection filters
*/
private void addFromAssociation(final String elementName, final String collectionRole)
throws QueryException {
//q.addCollection(collectionName, collectionRole);
QueryableCollection persister = getCollectionPersister( collectionRole );
Type collectionElementType = persister.getElementType();
if ( !collectionElementType.isEntityType() ) {
throw new QueryException( "collection of values in filter: " + elementName );
}
String[] keyColumnNames = persister.getKeyColumnNames();
//if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);
String collectionName;
JoinSequence join = new JoinSequence( getFactory() );
collectionName = persister.isOneToMany() ?
elementName :
createNameForCollection( collectionRole );
join.setRoot( persister, collectionName );
if ( !persister.isOneToMany() ) {
//many-to-many
addCollection( collectionName, collectionRole );
try {
join.addJoin(
(AssociationType) persister.getElementType(),
elementName,
JoinType.INNER_JOIN,
persister.getElementColumnNames( collectionName )
);
}
catch (MappingException me) {
throw new QueryException( me );
}
}
join.addCondition( collectionName, keyColumnNames, " = ?" );
//if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
EntityType elemType = (EntityType) collectionElementType;
addFrom( elementName, elemType.getAssociatedEntityName(), join );
}
public boolean isManyToManyWith(OuterJoinableAssociation other) {
if ( joinable.isCollection() ) {
QueryableCollection persister = ( QueryableCollection ) joinable;
if ( persister.isManyToMany() ) {
return persister.getElementType() == other.getJoinableType();
}
}
return false;
}
private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
QueryableCollection collPersister = q.getCollectionPersister( collectionRole );
if ( !collPersister.hasIndex() ) throw new QueryException( "unindexed collection before []: " + path );
String[] indexCols = collPersister.getIndexColumnNames();
if ( indexCols.length != 1 ) throw new QueryException( "composite-index appears in []: " + path );
//String[] keyCols = collPersister.getKeyColumnNames();
JoinSequence fromJoins = new JoinSequence( q.getFactory() )
.setUseThetaStyle( useThetaStyleJoin )
.setRoot( collPersister, collectionName )
.setNext( joinSequence.copy() );
if ( !continuation ) addJoin( collectionName, collPersister.getCollectionType() );
joinSequence.addCondition( collectionName + '.' + indexCols[0] + " = " ); //TODO: get SQL rendering out of here
CollectionElement elem = new CollectionElement();
elem.elementColumns = collPersister.getElementColumnNames(collectionName);
elem.elementType = collPersister.getElementType();
elem.isOneToMany = collPersister.isOneToMany();
elem.alias = collectionName;
elem.joinSequence = joinSequence;
collectionElements.addLast( elem );
setExpectingCollectionIndex();
q.addCollection( collectionName, collectionRole );
q.addFromJoinOnly( collectionName, fromJoins );
}
/**
* Used for collection filters
*/
private void addFromAssociation(final String elementName, final String collectionRole)
throws QueryException {
//q.addCollection(collectionName, collectionRole);
QueryableCollection persister = getCollectionPersister( collectionRole );
Type collectionElementType = persister.getElementType();
if ( !collectionElementType.isEntityType() ) {
throw new QueryException( "collection of values in filter: " + elementName );
}
String[] keyColumnNames = persister.getKeyColumnNames();
//if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);
String collectionName;
JoinSequence join = new JoinSequence( getFactory() );
collectionName = persister.isOneToMany() ?
elementName :
createNameForCollection( collectionRole );
join.setRoot( persister, collectionName );
if ( !persister.isOneToMany() ) {
//many-to-many
addCollection( collectionName, collectionRole );
try {
join.addJoin( ( AssociationType ) persister.getElementType(),
elementName,
JoinFragment.INNER_JOIN,
persister.getElementColumnNames(collectionName) );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
}
join.addCondition( collectionName, keyColumnNames, " = ?" );
//if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
EntityType elemType = ( EntityType ) collectionElementType;
addFrom( elementName, elemType.getAssociatedEntityName(), join );
}
/**
* For a collection role, return a list of associations to be fetched by outerjoin
*/
private void walkCollectionTree(
final QueryableCollection persister,
final String alias,
final PropertyPath path,
final int currentDepth) throws MappingException {
if ( persister.isOneToMany() ) {
walkEntityTree(
(OuterJoinLoadable) persister.getElementPersister(),
alias,
path,
currentDepth
);
}
else {
Type type = persister.getElementType();
if ( type.isAssociationType() ) {
// a many-to-many;
// decrement currentDepth here to allow join across the association table
// without exceeding MAX_FETCH_DEPTH (i.e. the "currentDepth - 1" bit)
AssociationType associationType = (AssociationType) type;
String[] aliasedLhsColumns = persister.getElementColumnNames( alias );
String[] lhsColumns = persister.getElementColumnNames();
// if the current depth is 0, the root thing being loaded is the
// many-to-many collection itself. Here, it is alright to use
// an inner join...
boolean useInnerJoin = currentDepth == 0;
final JoinType joinType = getJoinType(
associationType,
persister.getFetchMode(),
path,
persister.getTableName(),
lhsColumns,
!useInnerJoin,
currentDepth - 1,
null //operations which cascade as far as the collection also cascade to collection elements
);
addAssociationToJoinTreeIfNecessary(
associationType,
aliasedLhsColumns,
alias,
path,
currentDepth - 1,
joinType
);
}
else if ( type.isComponentType() ) {
walkCompositeElementTree(
(CompositeType) type,
persister.getElementColumnNames(),
persister,
alias,
path,
currentDepth
);
}
}
}
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;
}
@Override
protected Type resolveType(QueryableCollection collectionPersister) {
return collectionPersister.getElementType();
}
@Override
protected void prepareFromClauseInputTree(AST fromClauseInput) {
if ( !isSubQuery() ) {
// // inject param specifications to account for dynamic filter param values
// if ( ! getEnabledFilters().isEmpty() ) {
// Iterator filterItr = getEnabledFilters().values().iterator();
// while ( filterItr.hasNext() ) {
// FilterImpl filter = ( FilterImpl ) filterItr.next();
// if ( ! filter.getFilterDefinition().getParameterNames().isEmpty() ) {
// Iterator paramItr = filter.getFilterDefinition().getParameterNames().iterator();
// while ( paramItr.hasNext() ) {
// String parameterName = ( String ) paramItr.next();
// // currently param filters *only* work with single-column parameter types;
// // if that limitation is ever lifted, this logic will need to change to account for that
// ParameterNode collectionFilterKeyParameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
// DynamicFilterParameterSpecification paramSpec = new DynamicFilterParameterSpecification(
// filter.getName(),
// parameterName,
// filter.getFilterDefinition().getParameterType( parameterName ),
// positionalParameterCount++
// );
// collectionFilterKeyParameter.setHqlParameterSpecification( paramSpec );
// parameterSpecs.add( paramSpec );
// }
// }
// }
// }
if ( isFilter() ) {
// Handle collection-filter compilation.
// IMPORTANT NOTE: This is modifying the INPUT (HQL) tree, not the output tree!
QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
Type collectionElementType = persister.getElementType();
if ( !collectionElementType.isEntityType() ) {
throw new QueryException( "collection of values in filter: this" );
}
String collectionElementEntityName = persister.getElementPersister().getEntityName();
ASTFactory inputAstFactory = hqlParser.getASTFactory();
AST fromElement = inputAstFactory.create( HqlTokenTypes.FILTER_ENTITY, collectionElementEntityName );
ASTUtil.createSibling( inputAstFactory, HqlTokenTypes.ALIAS, "this", fromElement );
fromClauseInput.addChild( fromElement );
// Show the modified AST.
LOG.debug( "prepareFromClauseInputTree() : Filter - Added 'this' as a from element..." );
queryTranslatorImpl.showHqlAst( hqlParser.getAST() );
// Create a parameter specification for the collection filter...
final Type collectionFilterKeyType = sessionFactoryHelper.requireQueryableCollection( collectionFilterRole )
.getKeyType();
final ParameterNode collectionFilterKeyParameter = (ParameterNode) astFactory.create( PARAM, "?" );
final CollectionFilterKeyParameterSpecification collectionFilterKeyParameterSpec = new CollectionFilterKeyParameterSpecification(
collectionFilterRole,
collectionFilterKeyType
);
parameterCount++;
collectionFilterKeyParameter.setHqlParameterSpecification( collectionFilterKeyParameterSpec );
parameterSpecs.add( collectionFilterKeyParameterSpec );
}
}
}
/**
* For a collection role, return a list of associations to be fetched by outerjoin
*/
private void walkCollectionTree(
final QueryableCollection persister,
final String alias,
final String path,
final int currentDepth)
throws MappingException {
if ( persister.isOneToMany() ) {
walkEntityTree(
(OuterJoinLoadable) persister.getElementPersister(),
alias,
path,
currentDepth
);
}
else {
Type type = persister.getElementType();
if ( type.isAssociationType() ) {
// a many-to-many;
// decrement currentDepth here to allow join across the association table
// without exceeding MAX_FETCH_DEPTH (i.e. the "currentDepth - 1" bit)
AssociationType associationType = (AssociationType) type;
String[] aliasedLhsColumns = persister.getElementColumnNames(alias);
String[] lhsColumns = persister.getElementColumnNames();
// if the current depth is 0, the root thing being loaded is the
// many-to-many collection itself. Here, it is alright to use
// an inner join...
boolean useInnerJoin = currentDepth == 0;
final int joinType = getJoinType(
associationType,
persister.getFetchMode(),
path,
persister.getTableName(),
lhsColumns,
!useInnerJoin,
currentDepth - 1,
null //operations which cascade as far as the collection also cascade to collection elements
);
addAssociationToJoinTreeIfNecessary(
associationType,
aliasedLhsColumns,
alias,
path,
currentDepth - 1,
joinType
);
}
else if ( type.isComponentType() ) {
walkCompositeElementTree(
(AbstractComponentType) type,
persister.getElementColumnNames(),
persister,
alias,
path,
currentDepth
);
}
}
}
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;
}
protected void prepareFromClauseInputTree(AST fromClauseInput) {
if ( !isSubQuery() ) {
// // inject param specifications to account for dynamic filter param values
// if ( ! getEnabledFilters().isEmpty() ) {
// Iterator filterItr = getEnabledFilters().values().iterator();
// while ( filterItr.hasNext() ) {
// FilterImpl filter = ( FilterImpl ) filterItr.next();
// if ( ! filter.getFilterDefinition().getParameterNames().isEmpty() ) {
// Iterator paramItr = filter.getFilterDefinition().getParameterNames().iterator();
// while ( paramItr.hasNext() ) {
// String parameterName = ( String ) paramItr.next();
// // currently param filters *only* work with single-column parameter types;
// // if that limitation is ever lifted, this logic will need to change to account for that
// ParameterNode collectionFilterKeyParameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
// DynamicFilterParameterSpecification paramSpec = new DynamicFilterParameterSpecification(
// filter.getName(),
// parameterName,
// filter.getFilterDefinition().getParameterType( parameterName ),
// positionalParameterCount++
// );
// collectionFilterKeyParameter.setHqlParameterSpecification( paramSpec );
// parameters.add( paramSpec );
// }
// }
// }
// }
if ( isFilter() ) {
// Handle collection-fiter compilation.
// IMPORTANT NOTE: This is modifying the INPUT (HQL) tree, not the output tree!
QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
Type collectionElementType = persister.getElementType();
if ( !collectionElementType.isEntityType() ) {
throw new QueryException( "collection of values in filter: this" );
}
String collectionElementEntityName = persister.getElementPersister().getEntityName();
ASTFactory inputAstFactory = hqlParser.getASTFactory();
AST fromElement = ASTUtil.create( inputAstFactory, HqlTokenTypes.FILTER_ENTITY, collectionElementEntityName );
ASTUtil.createSibling( inputAstFactory, HqlTokenTypes.ALIAS, "this", fromElement );
fromClauseInput.addChild( fromElement );
// Show the modified AST.
if ( log.isDebugEnabled() ) {
log.debug( "prepareFromClauseInputTree() : Filter - Added 'this' as a from element..." );
}
queryTranslatorImpl.showHqlAst( hqlParser.getAST() );
// Create a parameter specification for the collection filter...
Type collectionFilterKeyType = sessionFactoryHelper.requireQueryableCollection( collectionFilterRole ).getKeyType();
ParameterNode collectionFilterKeyParameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
CollectionFilterKeyParameterSpecification collectionFilterKeyParameterSpec = new CollectionFilterKeyParameterSpecification(
collectionFilterRole, collectionFilterKeyType, positionalParameterCount++
);
collectionFilterKeyParameter.setHqlParameterSpecification( collectionFilterKeyParameterSpec );
parameters.add( collectionFilterKeyParameterSpec );
}
}
}