下面列出了org.hibernate.persister.collection.QueryableCollection#getElementColumnNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public String[] toAliasedColumns(String alias, String propertyName) {
final QueryableCollection queryableCollection = (QueryableCollection) persister;
if ( propertyName.equals( CollectionPropertyNames.COLLECTION_ELEMENTS ) ) {
return queryableCollection.getElementColumnNames( alias );
}
else if ( propertyName.equals( CollectionPropertyNames.COLLECTION_INDICES ) ) {
return queryableCollection.getIndexColumnNames( alias );
}
else {
throw new IllegalArgumentException(
String.format(
"Collection propertyName must be either %s or %s; instead it was %s.",
CollectionPropertyNames.COLLECTION_ELEMENTS,
CollectionPropertyNames.COLLECTION_INDICES,
propertyName
)
);
}
}
@Override
public AssociationKey getAssociationKey() {
final AssociationType type = getType();
if ( type.isAnyType() ) {
return new AssociationKey(
JoinHelper.getLHSTableName( type, attributeNumber(), (OuterJoinLoadable) getSource() ),
JoinHelper.getLHSColumnNames(
type,
attributeNumber(),
0,
(OuterJoinLoadable) getSource(),
sessionFactory()
)
);
}
final Joinable joinable = type.getAssociatedJoinable( sessionFactory() );
if ( type.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT ) {
final String lhsTableName;
final String[] lhsColumnNames;
if ( joinable.isCollection() ) {
final QueryableCollection collectionPersister = (QueryableCollection) joinable;
lhsTableName = collectionPersister.getTableName();
lhsColumnNames = collectionPersister.getElementColumnNames();
}
else {
final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
lhsTableName = getLHSTableName( type, attributeNumber(), entityPersister );
lhsColumnNames = getLHSColumnNames( type, attributeNumber(), entityPersister, sessionFactory() );
}
return new AssociationKey( lhsTableName, lhsColumnNames );
}
else {
return new AssociationKey( joinable.getTableName(), getRHSColumnNames( type, sessionFactory() ) );
}
}
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 );
}
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 );
}
/**
* 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
);
}
}
}
@Override
public void resolve(boolean generateJoin, boolean implicitJoin, String classAlias, AST parent, AST parentPredicate)
throws SemanticException {
if ( isResolved() ) {
return;
}
FromReferenceNode collectionNode = (FromReferenceNode) getFirstChild();
SessionFactoryHelper sessionFactoryHelper = getSessionFactoryHelper();
collectionNode.resolveIndex( this ); // Fully resolve the map reference, create implicit joins.
Type type = collectionNode.getDataType();
if ( !type.isCollectionType() ) {
throw new SemanticException( "The [] operator cannot be applied to type " + type.toString() );
}
String collectionRole = ( (CollectionType) type ).getRole();
QueryableCollection queryableCollection = sessionFactoryHelper.requireQueryableCollection( collectionRole );
if ( !queryableCollection.hasIndex() ) {
throw new QueryException( "unindexed fromElement before []: " + collectionNode.getPath() );
}
// Generate the inner join -- The elements need to be joined to the collection they are in.
FromElement fromElement = collectionNode.getFromElement();
String elementTable = fromElement.getTableAlias();
FromClause fromClause = fromElement.getFromClause();
String path = collectionNode.getPath();
FromElement elem = fromClause.findCollectionJoin( path );
if ( elem == null ) {
FromElementFactory factory = new FromElementFactory( fromClause, fromElement, path );
elem = factory.createCollectionElementsJoin( queryableCollection, elementTable );
LOG.debugf( "No FROM element found for the elements of collection join path %s, created %s", path, elem );
}
else {
LOG.debugf( "FROM element found for collection join path %s", path );
}
// The 'from element' that represents the elements of the collection.
setFromElement( fromElement );
// Add the condition to the join sequence that qualifies the indexed element.
AST selector = collectionNode.getNextSibling();
if ( selector == null ) {
throw new QueryException( "No index value!" );
}
// Sometimes use the element table alias, sometimes use the... umm... collection table alias (many to many)
String collectionTableAlias = elementTable;
if ( elem.getCollectionTableAlias() != null ) {
collectionTableAlias = elem.getCollectionTableAlias();
}
// TODO: get SQL rendering out of here, create an AST for the join expressions.
// Use the SQL generator grammar to generate the SQL text for the index expression.
JoinSequence joinSequence = fromElement.getJoinSequence();
String[] indexCols = queryableCollection.getIndexColumnNames();
if ( indexCols.length != 1 ) {
throw new QueryException( "composite-index appears in []: " + collectionNode.getPath() );
}
SqlGenerator gen = new SqlGenerator( getSessionFactoryHelper().getFactory() );
try {
gen.simpleExpr( selector ); //TODO: used to be exprNoParens! was this needed?
}
catch (RecognitionException e) {
throw new QueryException( e.getMessage(), e );
}
String selectorExpression = gen.getSQL();
joinSequence.addCondition( collectionTableAlias + '.' + indexCols[0] + " = " + selectorExpression );
List<ParameterSpecification> paramSpecs = gen.getCollectedParameters();
if ( paramSpecs != null ) {
switch ( paramSpecs.size() ) {
case 0:
// nothing to do
break;
case 1:
ParameterSpecification paramSpec = paramSpecs.get( 0 );
paramSpec.setExpectedType( queryableCollection.getIndexType() );
fromElement.setIndexCollectionSelectorParamSpec( paramSpec );
break;
default:
fromElement.setIndexCollectionSelectorParamSpec(
new AggregatedIndexCollectionSelectorParameterSpecifications( paramSpecs )
);
break;
}
}
// Now, set the text for this node. It should be the element columns.
String[] elementColumns = queryableCollection.getElementColumnNames( elementTable );
setText( elementColumns[0] );
setResolved();
}
/**
* 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
);
}
}
}
public void resolve(boolean generateJoin, boolean implicitJoin, String classAlias, AST parent)
throws SemanticException {
if ( isResolved() ) {
return;
}
FromReferenceNode collectionNode = ( FromReferenceNode ) getFirstChild();
SessionFactoryHelper sessionFactoryHelper = getSessionFactoryHelper();
collectionNode.resolveIndex( this ); // Fully resolve the map reference, create implicit joins.
Type type = collectionNode.getDataType();
if ( !type.isCollectionType() ) {
throw new SemanticException( "The [] operator cannot be applied to type " + type.toString() );
}
String collectionRole = ( ( CollectionType ) type ).getRole();
QueryableCollection queryableCollection = sessionFactoryHelper.requireQueryableCollection( collectionRole );
if ( !queryableCollection.hasIndex() ) {
throw new QueryException( "unindexed fromElement before []: " + collectionNode.getPath() );
}
// Generate the inner join -- The elements need to be joined to the collection they are in.
FromElement fromElement = collectionNode.getFromElement();
String elementTable = fromElement.getTableAlias();
FromClause fromClause = fromElement.getFromClause();
String path = collectionNode.getPath();
FromElement elem = fromClause.findCollectionJoin( path );
if ( elem == null ) {
FromElementFactory factory = new FromElementFactory( fromClause, fromElement, path );
elem = factory.createCollectionElementsJoin( queryableCollection, elementTable );
if ( log.isDebugEnabled() ) {
log.debug( "No FROM element found for the elements of collection join path " + path
+ ", created " + elem );
}
}
else {
if ( log.isDebugEnabled() ) {
log.debug( "FROM element found for collection join path " + path );
}
}
// Add the condition to the join sequence that qualifies the indexed element.
AST index = collectionNode.getNextSibling(); // The index should be a constant, which will have been processed already.
if ( index == null ) {
throw new QueryException( "No index value!" );
}
setFromElement( fromElement ); // The 'from element' that represents the elements of the collection.
// Sometimes use the element table alias, sometimes use the... umm... collection table alias (many to many)
String collectionTableAlias = elementTable;
if ( elem.getCollectionTableAlias() != null ) {
collectionTableAlias = elem.getCollectionTableAlias();
}
// TODO: get SQL rendering out of here, create an AST for the join expressions.
// Use the SQL generator grammar to generate the SQL text for the index expression.
JoinSequence joinSequence = fromElement.getJoinSequence();
String[] indexCols = queryableCollection.getIndexColumnNames();
if ( indexCols.length != 1 ) {
throw new QueryException( "composite-index appears in []: " + collectionNode.getPath() );
}
SqlGenerator gen = new SqlGenerator( getSessionFactoryHelper().getFactory() );
try {
gen.simpleExpr( index ); //TODO: used to be exprNoParens! was this needed?
}
catch ( RecognitionException e ) {
throw new QueryException( e.getMessage(), e );
}
String expression = gen.getSQL();
joinSequence.addCondition( collectionTableAlias + '.' + indexCols[0] + " = " + expression );
// Now, set the text for this node. It should be the element columns.
String[] elementColumns = queryableCollection.getElementColumnNames( elementTable );
setText( elementColumns[0] );
setResolved();
}