use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.
the class LoadPlanBuilderTest method testCascadeBasedBuild.
@Test
public void testCascadeBasedBuild(SessionFactoryScope scope) {
final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
final EntityPersister entityDescriptor = (EntityPersister) sessionFactory.getRuntimeMetamodels().getEntityMappingType(Message.class);
final SingleIdEntityLoaderStandardImpl<?> loader = new SingleIdEntityLoaderStandardImpl<>(entityDescriptor, sessionFactory);
final LoadQueryInfluencers influencers = new LoadQueryInfluencers() {
@Override
public CascadingFetchProfile getEnabledCascadingFetchProfile() {
return CascadingFetchProfile.MERGE;
}
};
final SingleIdLoadPlan<?> loadPlan = loader.resolveLoadPlan(LockOptions.READ, influencers, sessionFactory);
final List<DomainResult<?>> domainResults = loadPlan.getJdbcSelect().getJdbcValuesMappingProducer().resolve(null, sessionFactory).getDomainResults();
assertThat(domainResults).hasSize(1);
final DomainResult<?> domainResult = domainResults.get(0);
assertThat(domainResult).isInstanceOf(EntityResult.class);
final EntityResult entityResult = (EntityResult) domainResult;
assertThat(entityResult.getFetches()).hasSize(2);
final Fetch txtFetch = entityResult.getFetches().get(0);
assertThat(txtFetch).isNotNull();
assertThat(txtFetch.getFetchedMapping().getFetchableName()).isEqualTo("msgTxt");
assertThat(txtFetch.getTiming()).isEqualTo(FetchTiming.IMMEDIATE);
final Fetch posterFetch = entityResult.getFetches().get(1);
assertThat(posterFetch).isNotNull();
assertThat(posterFetch.getFetchedMapping().getFetchableName()).isEqualTo("poster");
assertThat(posterFetch.getTiming()).isEqualTo(FetchTiming.IMMEDIATE);
}
use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.
the class DynamicResultBuilderEntityCalculated method buildResult.
@Override
public EntityResult buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
final TableGroup tableGroup = entityMapping.createRootTableGroup(true, navigablePath, tableAlias, null, new SqlAliasBaseConstant(tableAlias), creationStateImpl, creationStateImpl.getFromClauseAccess(), creationStateImpl.getCreationContext());
creationStateImpl.getFromClauseAccess().registerTableGroup(navigablePath, tableGroup);
if (explicitLockMode != null) {
domainResultCreationState.getSqlAstCreationState().registerLockMode(tableAlias, explicitLockMode);
}
return (EntityResult) entityMapping.createDomainResult(navigablePath, tableGroup, tableAlias, domainResultCreationState);
}
use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.
the class ImplicitModelPartResultBuilderEntity method buildResult.
@Override
public EntityResult buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
final DomainResultCreationStateImpl creationStateImpl = ResultsHelper.impl(domainResultCreationState);
creationStateImpl.disallowPositionalSelections();
final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().resolveTableGroup(navigablePath, np -> {
if (navigablePath.getParent() != null) {
return creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
}
return modelPart.getEntityMappingType().createRootTableGroup(// since this is only used for result set mappings, the canUseInnerJoins value is irrelevant.
true, navigablePath, null, null, creationStateImpl, creationStateImpl.getCreationContext());
});
return (EntityResult) modelPart.createDomainResult(navigablePath, tableGroup, null, domainResultCreationState);
}
use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.
the class ResultSetMappingImpl method resolve.
@Override
public JdbcValuesMapping resolve(JdbcValuesMetadata jdbcResultsMetadata, SessionFactoryImplementor sessionFactory) {
final int numberOfResults;
final int rowSize = jdbcResultsMetadata.getColumnCount();
if (resultBuilders == null) {
numberOfResults = rowSize;
} else {
numberOfResults = resultBuilders.size();
}
final List<SqlSelection> sqlSelections = new ArrayList<>(rowSize);
final List<DomainResult<?>> domainResults = new ArrayList<>(numberOfResults);
final DomainResultCreationStateImpl creationState = new DomainResultCreationStateImpl(mappingIdentifier, jdbcResultsMetadata, legacyFetchBuilders, sqlSelections::add, sessionFactory);
for (int i = 0; i < numberOfResults; i++) {
final ResultBuilder resultBuilder = resultBuilders != null ? resultBuilders.get(i) : null;
final DomainResult<?> domainResult;
if (resultBuilder == null) {
domainResult = makeImplicitDomainResult(i, sqlSelections::add, jdbcResultsMetadata, sessionFactory);
} else {
domainResult = resultBuilder.buildResult(jdbcResultsMetadata, domainResults.size(), creationState.getLegacyFetchResolver()::resolve, creationState);
}
if (domainResult.containsAnyNonScalarResults()) {
creationState.disallowPositionalSelections();
}
domainResults.add(domainResult);
}
// As people should be able to just run native queries and work with tuples
if (resultBuilders != null) {
final Set<String> knownDuplicateAliases = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
if (resultBuilders.size() == 1 && domainResults.size() == 1 && domainResults.get(0) instanceof EntityResult) {
// Special case for result set mappings that just fetch a single polymorphic entity
final EntityResult entityResult = (EntityResult) domainResults.get(0);
final boolean polymorphic = entityResult.getReferencedMappingContainer().getEntityPersister().getEntityMetamodel().isPolymorphic();
// If we don't do that, there is no way to fetch joined inheritance entities
if (polymorphic && (legacyFetchBuilders == null || legacyFetchBuilders.isEmpty()) && !hasJoinFetches(entityResult.getFetches())) {
final Set<String> aliases = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
final AbstractEntityPersister entityPersister = (AbstractEntityPersister) entityResult.getReferencedMappingContainer().getEntityPersister();
for (String[] columns : entityPersister.getContraintOrderedTableKeyColumnClosure()) {
addColumns(aliases, knownDuplicateAliases, columns);
}
addColumn(aliases, knownDuplicateAliases, entityPersister.getDiscriminatorColumnName());
addColumn(aliases, knownDuplicateAliases, entityPersister.getVersionColumnName());
for (int i = 0; i < entityPersister.countSubclassProperties(); i++) {
addColumns(aliases, knownDuplicateAliases, entityPersister.getSubclassPropertyColumnNames(i));
}
}
}
final String[] aliases = new String[rowSize];
final Map<String, Boolean> aliasHasDuplicates = new HashMap<>(rowSize);
for (int i = 0; i < rowSize; i++) {
aliasHasDuplicates.compute(aliases[i] = jdbcResultsMetadata.resolveColumnName(i + 1), (k, v) -> v == null ? Boolean.FALSE : Boolean.TRUE);
}
// Only check for duplicates for the selections that we actually use
for (SqlSelection sqlSelection : sqlSelections) {
final String alias = aliases[sqlSelection.getValuesArrayPosition()];
if (!knownDuplicateAliases.contains(alias) && aliasHasDuplicates.get(alias) == Boolean.TRUE) {
throw new NonUniqueDiscoveredSqlAliasException("Encountered a duplicated sql alias [" + alias + "] during auto-discovery of a native-sql query");
}
}
}
return new JdbcValuesMappingImpl(sqlSelections, domainResults, rowSize, creationState.getRegisteredLockModes());
}
use of org.hibernate.sql.results.graph.entity.EntityResult in project hibernate-orm by hibernate.
the class ScrollableResultsConsumer method containsCollectionFetches.
private boolean containsCollectionFetches(JdbcValuesMapping valuesMapping) {
final List<DomainResult<?>> domainResults = valuesMapping.getDomainResults();
for (DomainResult domainResult : domainResults) {
if (domainResult instanceof EntityResult) {
EntityResult entityResult = (EntityResult) domainResult;
final List<Fetch> fetches = entityResult.getFetches();
for (Fetch fetch : fetches) {
if (fetch instanceof EagerCollectionFetch) {
return true;
}
}
}
}
return false;
}
Aggregations