use of org.hibernate.metamodel.mapping.SelectableConsumer in project hibernate-orm by hibernate.
the class DynamicFetchBuilderStandard method buildFetch.
@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
final TableGroup ownerTableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(parent.getNavigablePath());
final Fetchable attributeMapping = (Fetchable) parent.getReferencedMappingContainer().findSubPart(fetchableName, null);
final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
final SelectableConsumer selectableConsumer = (selectionIndex, selectableMapping) -> {
final TableReference tableReference = ownerTableGroup.resolveTableReference(fetchPath, selectableMapping.getContainingTableExpression());
final String columnAlias = columnNames.get(selectionIndex);
sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), state -> {
final int resultSetPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
final int valuesArrayPosition = resultSetPosition - 1;
return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
}), selectableMapping.getJdbcMapping().getMappedJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
};
if (attributeMapping instanceof BasicValuedMapping) {
attributeMapping.forEachSelectable(selectableConsumer);
return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.IMMEDIATE, true, null, creationStateImpl);
} else if (attributeMapping instanceof EmbeddedAttributeMapping) {
attributeMapping.forEachSelectable(selectableConsumer);
return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.IMMEDIATE, false, null, creationStateImpl);
} else if (attributeMapping instanceof ToOneAttributeMapping) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
toOneAttributeMapping.getForeignKeyDescriptor().getPart(toOneAttributeMapping.getSideNature()).forEachSelectable(selectableConsumer);
return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.DELAYED, false, null, creationStateImpl);
} else {
assert attributeMapping instanceof PluralAttributeMapping;
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) attributeMapping;
pluralAttributeMapping.getKeyDescriptor().visitTargetSelectables(selectableConsumer);
return parent.generateFetchableFetch(attributeMapping, fetchPath, FetchTiming.DELAYED, false, null, creationStateImpl);
}
}
use of org.hibernate.metamodel.mapping.SelectableConsumer in project hibernate-orm by hibernate.
the class EntityValuedPathInterpretation method from.
public static <T> EntityValuedPathInterpretation<T> from(NavigablePath navigablePath, TableGroup tableGroup, ModelPart resultModelPart, EntityValuedModelPart mapping, EntityValuedModelPart treatedMapping, SqmToSqlAstConverter sqlAstCreationState) {
final SqlExpressionResolver sqlExprResolver = sqlAstCreationState.getSqlExpressionResolver();
final SessionFactoryImplementor sessionFactory = sqlAstCreationState.getCreationContext().getSessionFactory();
final Expression sqlExpression;
if (resultModelPart == null) {
final EntityMappingType entityMappingType = mapping.getEntityMappingType();
final EntityIdentifierMapping identifierMapping = entityMappingType.getIdentifierMapping();
final EntityDiscriminatorMapping discriminatorMapping = entityMappingType.getDiscriminatorMapping();
final List<Expression> expressions = new ArrayList<>(entityMappingType.getJdbcTypeCount() + identifierMapping.getJdbcTypeCount() + (discriminatorMapping == null ? 0 : 1));
final TableGroup parentTableGroup = tableGroup;
final SelectableConsumer selectableConsumer = (selectionIndex, selectableMapping) -> {
final TableReference tableReference = parentTableGroup.resolveTableReference(navigablePath, selectableMapping.getContainingTableExpression(), false);
expressions.add(sqlExprResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), processingState -> new ColumnReference(tableReference, selectableMapping, sessionFactory)));
};
identifierMapping.forEachSelectable(selectableConsumer);
if (discriminatorMapping != null) {
discriminatorMapping.forEachSelectable(selectableConsumer);
}
entityMappingType.forEachSelectable(selectableConsumer);
sqlExpression = new SqlTuple(expressions, entityMappingType);
} else {
if (resultModelPart instanceof BasicValuedModelPart) {
final BasicValuedModelPart basicValuedModelPart = (BasicValuedModelPart) resultModelPart;
final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, basicValuedModelPart.getContainingTableExpression());
sqlExpression = sqlExprResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, basicValuedModelPart.getSelectionExpression()), processingState -> new ColumnReference(tableReference, basicValuedModelPart, sessionFactory));
} else {
final List<Expression> expressions = new ArrayList<>(resultModelPart.getJdbcTypeCount());
resultModelPart.forEachSelectable((selectionIndex, selectableMapping) -> {
final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, selectableMapping.getContainingTableExpression());
expressions.add(sqlExprResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), processingState -> new ColumnReference(tableReference, selectableMapping, sessionFactory)));
});
sqlExpression = new SqlTuple(expressions, resultModelPart);
}
}
return new EntityValuedPathInterpretation<>(sqlExpression, navigablePath, tableGroup, treatedMapping);
}
use of org.hibernate.metamodel.mapping.SelectableConsumer in project hibernate-orm by hibernate.
the class CompleteResultBuilderCollectionStandard method resolveSelections.
private void resolveSelections(TableGroup tableGroup, ModelPart modelPart, String[] columnNames, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationStateImpl creationStateImpl) {
final SelectableConsumer consumer = (selectionIndex, selectableMapping) -> {
final String columnName = columnNames[selectionIndex];
creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), processingState -> {
final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnName);
final BasicValuedMapping basicType = (BasicValuedMapping) selectableMapping.getJdbcMapping();
final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
}), selectableMapping.getJdbcMapping().getMappedJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
};
if (modelPart instanceof EntityValuedModelPart) {
final EntityMappingType entityMappingType = ((EntityValuedModelPart) modelPart).getEntityMappingType();
int index = entityMappingType.getIdentifierMapping().forEachSelectable(consumer);
if (entityMappingType.getDiscriminatorMapping() != null) {
index += entityMappingType.getDiscriminatorMapping().forEachSelectable(index, consumer);
}
entityMappingType.forEachSelectable(index, consumer);
} else {
modelPart.forEachSelectable(consumer);
}
}
use of org.hibernate.metamodel.mapping.SelectableConsumer in project hibernate-orm by hibernate.
the class UnionSubclassEntityPersister method generateSubquery.
protected String generateSubquery(Set<String> treated) {
if (!hasSubclasses()) {
return getTableName();
}
final Dialect dialect = getFactory().getJdbcServices().getDialect();
final MappingMetamodelImplementor metamodel = getFactory().getRuntimeMetamodels().getMappingMetamodel();
// Collect all selectables of every entity subtype and group by selection expression as well as table name
final LinkedHashMap<String, Map<String, SelectableMapping>> selectables = new LinkedHashMap<>();
final SelectableConsumer selectableConsumer = (i, selectable) -> {
selectables.computeIfAbsent(selectable.getSelectionExpression(), k -> new HashMap<>()).put(selectable.getContainingTableExpression(), selectable);
};
// Collect the concrete subclass table names for the treated entity names
final Set<String> treatedTableNames = new HashSet<>(treated.size());
for (String subclassName : treated) {
final UnionSubclassEntityPersister subPersister = (UnionSubclassEntityPersister) metamodel.getEntityDescriptor(subclassName);
for (String subclassTableName : subPersister.getSubclassTableNames()) {
if (ArrayHelper.indexOf(subclassSpaces, subclassTableName) != -1) {
treatedTableNames.add(subclassTableName);
}
}
subPersister.getIdentifierMapping().forEachSelectable(selectableConsumer);
if (subPersister.getVersionMapping() != null) {
subPersister.getVersionMapping().forEachSelectable(selectableConsumer);
}
subPersister.visitSubTypeAttributeMappings(attributeMapping -> attributeMapping.forEachSelectable(selectableConsumer));
}
// Create a union sub-query for the table names, like generateSubquery(PersistentClass model, Mapping mapping)
final StringBuilder buf = new StringBuilder(subquery.length()).append("( ");
for (String name : getSubclassEntityNames()) {
final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor(name);
final String subclassTableName = persister.getTableName();
if (treatedTableNames.contains(subclassTableName)) {
buf.append("select ");
for (Map<String, SelectableMapping> selectableMappings : selectables.values()) {
SelectableMapping selectableMapping = selectableMappings.get(subclassTableName);
if (selectableMapping == null) {
// If there is no selectable mapping for a table name, we render a null expression
selectableMapping = selectableMappings.values().iterator().next();
final int sqlType = selectableMapping.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode();
buf.append(dialect.getSelectClauseNullString(sqlType)).append(" as ");
}
buf.append(new ColumnReference((String) null, selectableMapping, getFactory()).getExpressionText());
buf.append(", ");
}
buf.append(persister.getDiscriminatorSQLValue()).append(" as clazz_");
buf.append(" from ").append(subclassTableName);
buf.append(" union ");
if (dialect.supportsUnionAll()) {
buf.append("all ");
}
}
}
if (buf.length() > 2) {
// chop the last union (all)
buf.setLength(buf.length() - (dialect.supportsUnionAll() ? 11 : 7));
}
return buf.append(" )").toString();
}
Aggregations