use of org.hibernate.sql.results.graph.DomainResult in project hibernate-orm by hibernate.
the class ToOneAttributeMapping method createSnapshotDomainResult.
@Override
public <T> DomainResult<T> createSnapshotDomainResult(NavigablePath navigablePath, TableGroup tableGroup, String resultVariable, DomainResultCreationState creationState) {
// We need a join if either
// - the association is mapped with `@NotFound`
// - the key is on the referring side i.e. this is an inverse to-one
// and if the FK refers to a non-PK
final boolean forceJoin = hasNotFoundAction() || sideNature == ForeignKeyDescriptor.Nature.TARGET || referencedPropertyName != null;
final TableGroup tableGroupToUse;
if (forceJoin) {
tableGroupToUse = creationState.getSqlAstCreationState().getFromClauseAccess().resolveTableGroup(navigablePath, np -> {
final TableGroupJoin tableGroupJoin = createTableGroupJoin(navigablePath, tableGroup, null, getDefaultSqlAstJoinType(tableGroup), true, false, creationState.getSqlAstCreationState());
tableGroup.addTableGroupJoin(tableGroupJoin);
return tableGroupJoin.getJoinedGroup();
});
} else {
tableGroupToUse = tableGroup;
}
if (hasNotFoundAction()) {
assert tableGroupToUse != tableGroup;
// noinspection unchecked
return new NotFoundSnapshotResult(navigablePath, this, tableGroupToUse, tableGroup, creationState);
}
if (referencedPropertyName == null) {
// noinspection unchecked
return new EntityDelayedResultImpl(navigablePath.append(EntityIdentifierMapping.ROLE_LOCAL_NAME), this, tableGroupToUse, creationState);
} else {
// We don't support proxies based on a non-PK yet, so we must fetch the whole entity
final EntityResultImpl entityResult = new EntityResultImpl(navigablePath, this, tableGroupToUse, null, creationState);
entityResult.afterInitialize(entityResult, creationState);
// noinspection unchecked
return entityResult;
}
}
use of org.hibernate.sql.results.graph.DomainResult in project hibernate-orm by hibernate.
the class CollectionIdentifierDescriptorImpl method createDomainResult.
public DomainResult<?> createDomainResult(NavigablePath collectionPath, TableGroup tableGroup, DomainResultCreationState creationState) {
final SqlAstCreationState astCreationState = creationState.getSqlAstCreationState();
final SqlAstCreationContext astCreationContext = astCreationState.getCreationContext();
final SessionFactoryImplementor sessionFactory = astCreationContext.getSessionFactory();
final SqlExpressionResolver sqlExpressionResolver = astCreationState.getSqlExpressionResolver();
final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), columnName), p -> new ColumnReference(tableGroup.getPrimaryTableReference().getIdentificationVariable(), columnName, false, null, null, type, sessionFactory)), type.getJavaTypeDescriptor(), null, sessionFactory.getTypeConfiguration());
// noinspection unchecked
return new BasicResult<>(sqlSelection.getValuesArrayPosition(), null, (JavaType<Object>) type.getJavaTypeDescriptor(), collectionPath);
}
use of org.hibernate.sql.results.graph.DomainResult in project hibernate-orm by hibernate.
the class CompoundNaturalIdMapping method createDomainResult.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ModelPart
@Override
public <T> DomainResult<T> createDomainResult(NavigablePath navigablePath, TableGroup tableGroup, String resultVariable, DomainResultCreationState creationState) {
assert navigablePath.getLocalName().equals(NaturalIdMapping.PART_NAME);
final SessionFactoryImplementor sessionFactory = creationState.getSqlAstCreationState().getCreationContext().getSessionFactory();
final JavaType<Object[]> jtd = sessionFactory.getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Object[].class);
// register the table group under `...{natural-id}` as well
creationState.getSqlAstCreationState().getFromClauseAccess().resolveTableGroup(navigablePath, (np) -> tableGroup);
return (DomainResult<T>) new DomainResultImpl(navigablePath, this, jtd, resultVariable, creationState);
}
use of org.hibernate.sql.results.graph.DomainResult in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method generateSelect.
private SelectStatement generateSelect() {
if (loadable instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
if (pluralAttributeMapping.getMappedType().getCollectionSemantics() instanceof BagSemantics) {
currentBagRole = pluralAttributeMapping.getNavigableRole().getNavigableName();
}
}
final NavigablePath rootNavigablePath = new NavigablePath(loadable.getRootPathName());
final QuerySpec rootQuerySpec = new QuerySpec(true);
final List<DomainResult<?>> domainResults;
final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), lockOptions, this::visitFetches, forceIdentifierSelection, creationContext);
final TableGroup rootTableGroup = loadable.createRootTableGroup(true, rootNavigablePath, null, () -> rootQuerySpec::applyPredicate, sqlAstCreationState, creationContext);
rootQuerySpec.getFromClause().addRoot(rootTableGroup);
sqlAstCreationState.getFromClauseAccess().registerTableGroup(rootNavigablePath, rootTableGroup);
registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), rootTableGroup);
if (partsToSelect != null && !partsToSelect.isEmpty()) {
domainResults = new ArrayList<>(partsToSelect.size());
for (ModelPart part : partsToSelect) {
final NavigablePath navigablePath = rootNavigablePath.append(part.getPartName());
final TableGroup tableGroup;
if (part instanceof TableGroupJoinProducer) {
final TableGroupJoinProducer tableGroupJoinProducer = (TableGroupJoinProducer) part;
final TableGroupJoin tableGroupJoin = tableGroupJoinProducer.createTableGroupJoin(navigablePath, rootTableGroup, null, SqlAstJoinType.LEFT, true, false, sqlAstCreationState);
rootTableGroup.addTableGroupJoin(tableGroupJoin);
tableGroup = tableGroupJoin.getJoinedGroup();
sqlAstCreationState.getFromClauseAccess().registerTableGroup(navigablePath, tableGroup);
registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), tableGroup);
} else {
tableGroup = rootTableGroup;
}
domainResults.add(part.createDomainResult(navigablePath, tableGroup, null, sqlAstCreationState));
}
} else {
// use the one passed to the constructor or create one (maybe always create and pass?)
// allows re-use as they can be re-used to save on memory - they
// do not share state between
// noinspection rawtypes
final DomainResult domainResult;
if (this.cachedDomainResult != null) {
// used the one passed to the constructor
domainResult = this.cachedDomainResult;
} else {
// create one
domainResult = loadable.createDomainResult(rootNavigablePath, rootTableGroup, null, sqlAstCreationState);
}
// noinspection unchecked
domainResults = Collections.singletonList(domainResult);
}
for (ModelPart restrictedPart : restrictedParts) {
final int numberOfRestrictionColumns = restrictedPart.getJdbcTypeCount();
applyRestriction(rootQuerySpec, rootNavigablePath, rootTableGroup, restrictedPart, numberOfRestrictionColumns, jdbcParameterConsumer, sqlAstCreationState);
}
if (loadable instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
applyFiltering(rootQuerySpec, rootTableGroup, pluralAttributeMapping, sqlAstCreationState);
applyOrdering(rootTableGroup, pluralAttributeMapping);
} else {
applyFiltering(rootQuerySpec, rootTableGroup, (Restrictable) loadable, sqlAstCreationState);
}
if (orderByFragments != null) {
orderByFragments.forEach(entry -> entry.getKey().apply(rootQuerySpec, entry.getValue(), sqlAstCreationState));
}
return new SelectStatement(rootQuerySpec, domainResults);
}
use of org.hibernate.sql.results.graph.DomainResult in project hibernate-orm by hibernate.
the class JdbcSelectExecutorStandardImpl method doExecuteQuery.
private <T, R> T doExecuteQuery(JdbcSelect jdbcSelect, JdbcParameterBindings jdbcParameterBindings, ExecutionContext executionContext, RowTransformer<R> rowTransformer, Class<R> domainResultType, Function<String, PreparedStatement> statementCreator, ResultsConsumer<T, R> resultsConsumer) {
final DeferredResultSetAccess deferredResultSetAccess = new DeferredResultSetAccess(jdbcSelect, jdbcParameterBindings, executionContext, statementCreator);
final JdbcValues jdbcValues = resolveJdbcValuesSource(executionContext.getQueryIdentifier(deferredResultSetAccess.getFinalSql()), jdbcSelect, resultsConsumer.canResultsBeCached(), executionContext, deferredResultSetAccess);
if (rowTransformer == null) {
@SuppressWarnings("unchecked") final TupleTransformer<R> tupleTransformer = (TupleTransformer<R>) executionContext.getQueryOptions().getTupleTransformer();
if (tupleTransformer == null) {
rowTransformer = RowTransformerStandardImpl.instance();
} else {
final List<DomainResult<?>> domainResults = jdbcValues.getValuesMapping().getDomainResults();
final String[] aliases = new String[domainResults.size()];
for (int i = 0; i < domainResults.size(); i++) {
aliases[i] = domainResults.get(i).getResultVariable();
}
rowTransformer = new RowTransformerTupleTransformerAdapter<>(aliases, tupleTransformer);
}
}
final boolean stats;
long startTime = 0;
final StatisticsImplementor statistics = executionContext.getSession().getFactory().getStatistics();
if (executionContext.hasQueryExecutionToBeAddedToStatistics() && jdbcValues instanceof JdbcValuesResultSetImpl) {
stats = statistics.isStatisticsEnabled();
if (stats) {
startTime = System.nanoTime();
}
} else {
stats = false;
}
/*
* Processing options effectively are only used for entity loading. Here we don't need these values.
*/
final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() {
@Override
public Object getEffectiveOptionalObject() {
return executionContext.getEntityInstance();
}
@Override
public String getEffectiveOptionalEntityName() {
return null;
}
@Override
public Object getEffectiveOptionalId() {
return executionContext.getEntityId();
}
@Override
public boolean shouldReturnProxies() {
return true;
}
};
final JdbcValuesSourceProcessingStateStandardImpl valuesProcessingState = new JdbcValuesSourceProcessingStateStandardImpl(executionContext, processingOptions, executionContext::registerLoadingEntityEntry);
final RowReader<R> rowReader = ResultsHelper.createRowReader(executionContext, // because the EntityEntrys would already have the desired lock mode
deferredResultSetAccess.usesFollowOnLocking() ? LockOptions.NONE : executionContext.getQueryOptions().getLockOptions(), rowTransformer, domainResultType, jdbcValues);
final RowProcessingStateStandardImpl rowProcessingState = new RowProcessingStateStandardImpl(valuesProcessingState, executionContext, rowReader, jdbcValues);
final T result = resultsConsumer.consume(jdbcValues, executionContext.getSession(), processingOptions, valuesProcessingState, rowProcessingState, rowReader);
if (stats) {
final long endTime = System.nanoTime();
final long milliseconds = TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
statistics.queryExecuted(executionContext.getQueryIdentifier(jdbcSelect.getSql()), getResultSize(result), milliseconds);
}
return result;
}
Aggregations