Search in sources :

Example 1 with DynamicResultBuilderEntityStandard

use of org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard in project hibernate-orm by hibernate.

the class ResultSetMappingProcessor method generateResultMapping.

public ResultSetMapping generateResultMapping(boolean queryHadAliases) {
    if (!queryHadAliases) {
        return this.resultSetMapping;
    }
    final ResultSetMappingImpl resultSetMapping = new ResultSetMappingImpl(null);
    final Set<String> visited = new HashSet<>();
    this.resultSetMapping.visitResultBuilders((i, resultBuilder) -> {
        if (resultBuilder instanceof NativeQuery.RootReturn) {
            final NativeQuery.RootReturn rootReturn = (NativeQuery.RootReturn) resultBuilder;
            final String suffix = alias2Suffix.get(rootReturn.getTableAlias());
            visited.add(rootReturn.getTableAlias());
            if (suffix == null) {
                resultSetMapping.addResultBuilder(resultBuilder);
            } else {
                final DynamicResultBuilderEntityStandard resultBuilderEntity = createSuffixedResultBuilder(rootReturn, suffix);
                resultSetMapping.addResultBuilder(resultBuilderEntity);
                alias2Return.put(rootReturn.getTableAlias(), resultBuilderEntity);
            }
        } else if (resultBuilder instanceof NativeQuery.CollectionReturn) {
            final NativeQuery.CollectionReturn collectionReturn = (NativeQuery.CollectionReturn) resultBuilder;
            final String suffix = alias2CollectionSuffix.get(collectionReturn.getTableAlias());
            if (suffix == null) {
                resultSetMapping.addResultBuilder(resultBuilder);
            } else {
                final CompleteResultBuilderCollectionStandard resultBuilderCollection = createSuffixedResultBuilder(collectionReturn, suffix, alias2Suffix.get(collectionReturn.getTableAlias()));
                resultSetMapping.addResultBuilder(resultBuilderCollection);
                alias2Return.put(collectionReturn.getTableAlias(), resultBuilderCollection);
            }
        } else {
            resultSetMapping.addResultBuilder(resultBuilder);
        }
    });
    this.resultSetMapping.visitLegacyFetchBuilders(fetchBuilder -> applyFetchBuilder(resultSetMapping, fetchBuilder, visited));
    return resultSetMapping;
}
Also used : CompleteResultBuilderCollectionStandard(org.hibernate.query.results.complete.CompleteResultBuilderCollectionStandard) ResultSetMappingImpl(org.hibernate.query.results.ResultSetMappingImpl) NativeQuery(org.hibernate.query.NativeQuery) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard) HashSet(java.util.HashSet)

Example 2 with DynamicResultBuilderEntityStandard

use of org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard in project hibernate-orm by hibernate.

the class NativeQueryImpl method addRoot.

@Override
public DynamicResultBuilderEntityStandard addRoot(String tableAlias, String entityName) {
    final DynamicResultBuilderEntityStandard resultBuilder = Builders.entity(tableAlias, entityName, getSessionFactory());
    resultSetMapping.addResultBuilder(resultBuilder);
    return resultBuilder;
}
Also used : DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)

Example 3 with DynamicResultBuilderEntityStandard

use of org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard in project hibernate-orm by hibernate.

the class ResultSetMappingProcessor method createSuffixedResultBuilder.

private DynamicResultBuilderEntityStandard createSuffixedResultBuilder(EntityMappingType entityMapping, String tableAlias, String suffix, LockMode lockMode, NavigablePath navigablePath) {
    final SQLLoadable loadable = (SQLLoadable) entityMapping.getEntityPersister();
    final DynamicResultBuilderEntityStandard resultBuilderEntity = new DynamicResultBuilderEntityStandard(entityMapping, tableAlias, navigablePath);
    resultBuilderEntity.setLockMode(lockMode);
    final String[] identifierAliases = loadable.getIdentifierAliases(suffix);
    resultBuilderEntity.addIdColumnAliases(identifierAliases);
    resultBuilderEntity.setDiscriminatorAlias(loadable.getDiscriminatorAlias(suffix));
    if (loadable.hasIdentifierProperty()) {
        resultBuilderEntity.addProperty(loadable.getIdentifierPropertyName(), identifierAliases);
    }
    for (String propertyName : loadable.getPropertyNames()) {
        final String[] columnAliases = loadable.getSubclassPropertyColumnAliases(propertyName, suffix);
        final Type propertyType = loadable.getPropertyType(propertyName);
        addFetchBuilder(suffix, loadable, resultBuilderEntity, tableAlias, identifierAliases, propertyName, columnAliases, propertyType);
    }
    return resultBuilderEntity;
}
Also used : CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) SQLLoadable(org.hibernate.persister.entity.SQLLoadable) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)

Example 4 with DynamicResultBuilderEntityStandard

use of org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard in project hibernate-orm by hibernate.

the class FetchMementoHbmStandard method resolve.

@Override
public FetchBuilder resolve(Parent parent, Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
    final Map<String, FetchBuilder> fetchBuilderMap = new HashMap<>();
    fetchMementoMap.forEach((attrName, fetchMemento) -> fetchBuilderMap.put(attrName, fetchMemento.resolve(this, querySpaceConsumer, context)));
    final DynamicResultBuilderEntityStandard resultBuilder;
    if (fetchable instanceof PluralAttributeMapping) {
        resultBuilder = new DynamicResultBuilderEntityStandard((EntityMappingType) ((PluralAttributeMapping) fetchable).getElementDescriptor().getPartMappingType(), tableAlias, navigablePath);
        FetchBuilder element = fetchBuilderMap.get("element");
        if (element != null) {
            if (element instanceof DynamicFetchBuilder) {
                resultBuilder.addIdColumnAliases(((DynamicFetchBuilder) element).getColumnAliases().toArray(new String[0]));
            } else {
                resultBuilder.addIdColumnAliases(((CompleteFetchBuilderEntityValuedModelPart) element).getColumnAliases().toArray(new String[0]));
            }
        }
        FetchBuilder index = fetchBuilderMap.get("index");
        if (index != null) {
            resultBuilder.addFetchBuilder(CollectionPart.Nature.INDEX.getName(), index);
        }
        for (Map.Entry<String, FetchBuilder> entry : fetchBuilderMap.entrySet()) {
            if (entry.getKey().startsWith(ELEMENT_PREFIX)) {
                resultBuilder.addFetchBuilder(entry.getKey().substring(ELEMENT_PREFIX.length()), entry.getValue());
            }
        }
    } else {
        resultBuilder = new DynamicResultBuilderEntityStandard(((ToOneAttributeMapping) fetchable).getEntityMappingType(), tableAlias, navigablePath);
        fetchBuilderMap.forEach(resultBuilder::addFetchBuilder);
    }
    return new DynamicFetchBuilderLegacy(tableAlias, ownerTableAlias, fetchable.getFetchableName(), keyColumnNames, fetchBuilderMap, resultBuilder);
}
Also used : DynamicFetchBuilder(org.hibernate.query.results.dynamic.DynamicFetchBuilder) FetchBuilder(org.hibernate.query.results.FetchBuilder) CompleteFetchBuilderEntityValuedModelPart(org.hibernate.query.results.complete.CompleteFetchBuilderEntityValuedModelPart) HashMap(java.util.HashMap) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) DynamicFetchBuilder(org.hibernate.query.results.dynamic.DynamicFetchBuilder) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with DynamicResultBuilderEntityStandard

use of org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard in project hibernate-orm by hibernate.

the class Builders method entity.

/**
 * Creates a EntityResultBuilder allowing for further configuring of the mapping.
 */
public static DynamicResultBuilderEntityStandard entity(String tableAlias, String entityName, SessionFactoryImplementor sessionFactory) {
    final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
    final EntityMappingType entityMapping = runtimeMetamodels.getEntityMappingType(entityName);
    return new DynamicResultBuilderEntityStandard(entityMapping, tableAlias);
}
Also used : RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)

Aggregations

DynamicResultBuilderEntityStandard (org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)6 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)3 SQLLoadable (org.hibernate.persister.entity.SQLLoadable)2 DynamicFetchBuilderLegacy (org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1 NativeQuery (org.hibernate.query.NativeQuery)1 FetchBuilder (org.hibernate.query.results.FetchBuilder)1 ResultSetMappingImpl (org.hibernate.query.results.ResultSetMappingImpl)1 CompleteFetchBuilderEntityValuedModelPart (org.hibernate.query.results.complete.CompleteFetchBuilderEntityValuedModelPart)1 CompleteResultBuilderCollectionStandard (org.hibernate.query.results.complete.CompleteResultBuilderCollectionStandard)1 DynamicFetchBuilder (org.hibernate.query.results.dynamic.DynamicFetchBuilder)1 CollectionType (org.hibernate.type.CollectionType)1 ComponentType (org.hibernate.type.ComponentType)1 EntityType (org.hibernate.type.EntityType)1