if ( StringHelper.isNotEmpty( ordering ) ) {
selectStatementBuilder.appendOrderByFragment( ordering );
}
final EntityReferenceAliases entityReferenceAliases = new EntityReferenceAliases() {
@Override
public String getTableAlias() {
return aliases.getElementTableAlias();
}
@Override
public EntityAliases getColumnAliases() {
return aliases.getEntityElementColumnAliases();
}
};
final EntityReference elementEntityReference = (EntityReference) fetch.getElementGraph();
readerCollector.addReader(
new EntityReferenceReader(
elementEntityReference,
entityReferenceAliases,
buildIdentifierReader(
selectStatementBuilder,
factory,
joinFragment,
elementEntityReference,
buildingParameters,
aliasResolutionContext,
readerCollector,
entityReferenceAliases,
stats
)
)
);
}
else {
final QueryableCollection queryableCollection = (QueryableCollection) fetch.getCollectionPersister();
final Joinable joinableCollection = (Joinable) fetch.getCollectionPersister();
final String rhsTableAlias = aliases.getElementTableAlias();
final String[] rhsColumnNames = JoinHelper.getRHSColumnNames( fetch.getFetchedType(), factory );
final String lhsTableAlias = resolveLhsTableAlias( fetchOwner, fetch, aliasResolutionContext );
// todo : this is not exactly correct. it assumes the join refers to the LHS PK
final String[] aliasedLhsColumnNames = fetch.toSqlSelectFragments( lhsTableAlias );
final String on = resolveAdditionalJoinCondition(
factory,
rhsTableAlias,
fetchOwner,
fetch,
buildingParameters.getQueryInfluencers(),
aliasResolutionContext
);
addJoins(
joinFragment,
joinableCollection,
fetch.isNullable() ? JoinType.LEFT_OUTER_JOIN : JoinType.INNER_JOIN,
rhsTableAlias,
rhsColumnNames,
aliasedLhsColumnNames,
on
);
// select the "collection columns"
selectStatementBuilder.appendSelectClauseFragment(
queryableCollection.selectFragment(
rhsTableAlias,
aliases.getCollectionColumnAliases().getSuffix()
)
);
if ( fetch.getCollectionPersister().isOneToMany() ) {
// if the collection elements are entities, select the entity columns as well
final OuterJoinLoadable elementPersister = (OuterJoinLoadable) queryableCollection.getElementPersister();
selectStatementBuilder.appendSelectClauseFragment(
elementPersister.selectFragment(
aliases.getElementTableAlias(),
aliases.getEntityElementColumnAliases().getSuffix()
)
);
final EntityReferenceAliases entityReferenceAliases = new EntityReferenceAliases() {
@Override
public String getTableAlias() {
return aliases.getElementTableAlias();
}