Search in sources :

Example 1 with Path

use of com.blazebit.persistence.Path in project blaze-persistence by Blazebit.

the class AbstractCommonQueryBuilder method getPath.

public Path getPath(String path) {
    if (path == null || path.isEmpty()) {
        JoinNode node = joinManager.getRootNodeOrFail("No or multiple query roots, can't find single root!");
        return new SimplePathReference(node, null, node.getType());
    }
    PathExpression pathExpression = expressionFactory.createPathExpression(path);
    joinManager.implicitJoin(pathExpression, true, true, true, null, null, new HashSet<String>(), false, false, true, false);
    // If we expose the path to the outside world, we can't remove it if it is a default select node
    if (pathExpression.getPathReference() != null) {
        selectManager.removeDefaultSelectNode((JoinNode) pathExpression.getBaseNode());
    }
    return (Path) pathExpression.getPathReference();
}
Also used : Path(com.blazebit.persistence.Path) PathExpression(com.blazebit.persistence.parser.expression.PathExpression)

Example 2 with Path

use of com.blazebit.persistence.Path in project blaze-persistence by Blazebit.

the class EntityViewSettingHelper method apply.

@SuppressWarnings("unchecked")
public static <T, Q extends FullQueryBuilder<T, Q>> Q apply(EntityViewSetting<T, Q> setting, EntityViewManagerImpl evm, CriteriaBuilder<?> criteriaBuilder, String entityViewRoot) {
    ManagedViewTypeImplementor<?> managedView = evm.getMetamodel().managedView(setting.getEntityViewClass());
    if (managedView == null) {
        throw new IllegalArgumentException("There is no entity view for the class '" + setting.getEntityViewClass().getName() + "' registered!");
    }
    MappingConstructorImpl<?> mappingConstructor = (MappingConstructorImpl<?>) managedView.getConstructor(setting.getViewConstructorName());
    if (managedView instanceof FlatViewType<?>) {
        if (managedView.hasJoinFetchedCollections()) {
            throw new IllegalArgumentException("Can't use the flat view '" + managedView.getJavaType().getName() + "' as view root because it contains join fetched collections! " + "Consider adding a @IdMapping to the entity view or use a different fetch strategy for the collections!");
        }
        if (mappingConstructor == null) {
            if (managedView.getConstructors().size() > 1) {
                mappingConstructor = (MappingConstructorImpl<T>) managedView.getConstructor("init");
            } else if (managedView.getConstructors().size() == 1) {
                mappingConstructor = (MappingConstructorImpl<T>) managedView.getConstructors().toArray()[0];
            }
        }
        if (mappingConstructor != null && mappingConstructor.hasJoinFetchedCollections()) {
            throw new IllegalArgumentException("Can't use the flat view '" + managedView.getJavaType().getName() + "' with the mapping constructor '" + mappingConstructor.getName() + "' as view root because it contains join fetched collections! " + "Consider adding a @IdMapping to the entity view or use a different fetch strategy for the collections!");
        }
    }
    if (managedView.isUpdatable() && !setting.getFetches().isEmpty()) {
        throw new IllegalArgumentException("Specifying fetches for @UpdatableEntityViews is currently disallowed. Remove the fetches!");
    }
    ExpressionFactory ef = criteriaBuilder.getService(ExpressionFactory.class);
    Map<String, Object> optionalParameters;
    if (setting.getOptionalParameters().isEmpty()) {
        optionalParameters = evm.getOptionalParameters();
    } else {
        optionalParameters = new HashMap<>(evm.getOptionalParameters());
        optionalParameters.putAll(setting.getOptionalParameters());
        optionalParameters = Collections.unmodifiableMap(optionalParameters);
    }
    Collection<String> requestedFetches;
    if (setting.getFetches().isEmpty() || !setting.hasAttributeFilters() && !setting.hasAttributeSorters()) {
        requestedFetches = setting.getFetches();
    } else {
        requestedFetches = new HashSet<>(setting.getFetches());
        addFetchesForNonMappingAttributes(setting.getAttributeFilterActivations().keySet(), managedView, requestedFetches);
        addFetchesForNonMappingAttributes(setting.getAttributeSorters().keySet(), managedView, requestedFetches);
    }
    Path root = criteriaBuilder.getPath(entityViewRoot);
    entityViewRoot = root.getPath();
    Q queryBuilder = getQueryBuilder(setting, criteriaBuilder, entityViewRoot, managedView, setting.getProperties());
    EntityViewConfiguration configuration = new EntityViewConfiguration(queryBuilder, ef, new MutableViewJpqlMacro(), new MutableEmbeddingViewJpqlMacro(), optionalParameters, setting.getProperties(), requestedFetches, managedView);
    queryBuilder.selectNew(evm.createObjectBuilder(managedView, mappingConstructor, root.getJavaType(), entityViewRoot, null, criteriaBuilder, configuration, 0, 0, false));
    Set<String> fetches = configuration.getFetches();
    applyAttributeFilters(setting, evm, queryBuilder, entityViewRoot, fetches, managedView);
    applyViewFilters(setting, evm, queryBuilder, managedView);
    applyAttributeSorters(setting, queryBuilder, entityViewRoot, fetches, managedView);
    applyOptionalParameters(optionalParameters, queryBuilder);
    return queryBuilder;
}
Also used : Path(com.blazebit.persistence.Path) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) MappingConstructorImpl(com.blazebit.persistence.view.impl.metamodel.MappingConstructorImpl) FlatViewType(com.blazebit.persistence.view.metamodel.FlatViewType)

Aggregations

Path (com.blazebit.persistence.Path)2 ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)1 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)1 MutableEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro)1 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)1 MappingConstructorImpl (com.blazebit.persistence.view.impl.metamodel.MappingConstructorImpl)1 FlatViewType (com.blazebit.persistence.view.metamodel.FlatViewType)1