use of org.hibernate.query.sqm.tree.select.SqmSubQuery in project hibernate-orm by hibernate.
the class SqmPathRegistryImpl method findFromByAlias.
@Override
public <X extends SqmFrom<?, ?>> X findFromByAlias(String alias, boolean searchParent) {
final String localAlias = jpaCompliance.isJpaQueryComplianceEnabled() ? alias.toLowerCase(Locale.getDefault()) : alias;
final SqmFrom<?, ?> registered = sqmFromByAlias.get(localAlias);
if (registered != null) {
// noinspection unchecked
return (X) registered;
}
SqmCreationProcessingState parentProcessingState = associatedProcessingState.getParentProcessingState();
if (searchParent && parentProcessingState != null) {
X parentRegistered;
do {
parentRegistered = parentProcessingState.getPathRegistry().findFromByAlias(alias, false);
parentProcessingState = parentProcessingState.getParentProcessingState();
} while (parentProcessingState != null && parentRegistered == null);
if (parentRegistered != null) {
// If a parent query contains the alias, we need to create a correlation on the subquery
final SqmSubQuery<?> selectQuery = (SqmSubQuery<?>) associatedProcessingState.getProcessingQuery();
SqmFrom<?, ?> correlated;
if (parentRegistered instanceof Root<?>) {
correlated = selectQuery.correlate((Root<?>) parentRegistered);
} else if (parentRegistered instanceof Join<?, ?>) {
correlated = selectQuery.correlate((Join<?, ?>) parentRegistered);
} else if (parentRegistered instanceof SqmCrossJoin<?>) {
correlated = selectQuery.correlate((SqmCrossJoin<?>) parentRegistered);
} else if (parentRegistered instanceof SqmEntityJoin<?>) {
correlated = selectQuery.correlate((SqmEntityJoin<?>) parentRegistered);
} else {
throw new UnsupportedOperationException("Can't correlate from node: " + parentRegistered);
}
register(correlated);
// noinspection unchecked
return (X) correlated;
}
}
return null;
}
use of org.hibernate.query.sqm.tree.select.SqmSubQuery in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method determineValueMapping.
private MappingModelExpressible<?> determineValueMapping(SqmExpression<?> sqmExpression, FromClauseIndex fromClauseIndex) {
if (sqmExpression instanceof SqmParameter) {
return determineValueMapping((SqmParameter<?>) sqmExpression);
} else if (sqmExpression instanceof SqmPath) {
log.debugf("Determining mapping-model type for SqmPath : %s ", sqmExpression);
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
return SqmMappingModelHelper.resolveMappingModelExpressible(sqmExpression, domainModel, fromClauseIndex::findTableGroup);
} else // The model type of an enum literal is always inferred
if (sqmExpression instanceof SqmEnumLiteral<?>) {
final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
if (mappingModelExpressible != null) {
return mappingModelExpressible;
}
} else if (sqmExpression instanceof SqmSubQuery<?>) {
final SqmSubQuery<?> subQuery = (SqmSubQuery<?>) sqmExpression;
final SqmSelectClause selectClause = subQuery.getQuerySpec().getSelectClause();
if (selectClause.getSelections().size() == 1) {
final SqmSelection<?> subQuerySelection = selectClause.getSelections().get(0);
final SqmSelectableNode<?> selectableNode = subQuerySelection.getSelectableNode();
if (selectableNode instanceof SqmExpression<?>) {
return determineValueMapping((SqmExpression<?>) selectableNode, fromClauseIndex);
}
final SqmExpressible<?> selectionNodeType = subQuerySelection.getNodeType();
if (selectionNodeType != null) {
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final MappingModelExpressible<?> expressible = domainModel.resolveMappingExpressible(selectionNodeType, this::findTableGroupByPath);
if (expressible != null) {
return expressible;
}
try {
final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
if (mappingModelExpressible != null) {
return mappingModelExpressible;
}
} catch (Exception ignore) {
return null;
}
}
}
}
log.debugf("Determining mapping-model type for generalized SqmExpression : %s", sqmExpression);
final SqmExpressible<?> nodeType = sqmExpression.getNodeType();
if (nodeType == null) {
// We can't determine the type of the expression
return null;
}
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final MappingModelExpressible<?> valueMapping = domainModel.resolveMappingExpressible(nodeType, fromClauseIndex::getTableGroup);
if (valueMapping == null) {
final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
if (mappingModelExpressible != null) {
return mappingModelExpressible;
}
}
if (valueMapping == null) {
// For literals it is totally possible that we can't figure out a mapping type
if (sqmExpression instanceof SqmLiteral<?>) {
return null;
}
throw new ConversionException("Could not determine ValueMapping for SqmExpression: " + sqmExpression);
}
return valueMapping;
}
Aggregations