Search in sources :

Example 1 with CorrelationProvider

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

the class CorrelationMultisetTupleElementMapper method applyMapping.

@Override
public void applyMapping(SelectBuilder<?> queryBuilder, ParameterHolder<?> parameterHolder, Map<String, Object> optionalParameters, ViewJpqlMacro viewJpqlMacro, EmbeddingViewJpqlMacro embeddingViewJpqlMacro, boolean asString) {
    String oldEmbeddingViewPath = embeddingViewJpqlMacro.getEmbeddingViewPath();
    embeddingViewJpqlMacro.setEmbeddingViewPath(embeddingViewPath);
    SubqueryInitiator<?> subqueryInitiator = queryBuilder.selectSubquery("subquery", "TO_MULTISET(subquery)");
    CorrelationProvider correlationProvider = correlationProviderFactory.create(parameterHolder, optionalParameters);
    MultisetCorrelationBuilder correlationBuilder = new MultisetCorrelationBuilder(subqueryInitiator, (ServiceProvider) queryBuilder, correlationAlias);
    correlationProvider.applyCorrelation(correlationBuilder, correlationBasis);
    SubqueryBuilder<?> subqueryBuilder = correlationBuilder.getSubqueryBuilder();
    for (TupleElementMapper mapper : subviewTemplate.getMappers()) {
        mapper.applyMapping(subqueryBuilder, parameterHolder, optionalParameters, viewJpqlMacro, embeddingViewJpqlMacro, true);
    }
    if (indexTemplate != null) {
        for (TupleElementMapper mapper : indexTemplate.getMappers()) {
            mapper.applyMapping(subqueryBuilder, parameterHolder, optionalParameters, viewJpqlMacro, embeddingViewJpqlMacro, true);
        }
    } else if (indexExpression != null) {
        subqueryBuilder.select(indexExpression);
    }
    if (limiter != null) {
        limiter.apply(parameterHolder, optionalParameters, subqueryBuilder);
    }
    subqueryBuilder.end();
    embeddingViewJpqlMacro.setEmbeddingViewPath(oldEmbeddingViewPath);
}
Also used : MultisetCorrelationBuilder(com.blazebit.persistence.view.impl.objectbuilder.transformer.correlation.MultisetCorrelationBuilder) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider)

Example 2 with CorrelationProvider

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

the class EntityViewRootSecondaryMapper method apply.

@Override
public void apply(FullQueryBuilder<?, ?> fullQueryBuilder, ParameterHolder<?> parameterHolder, Map<String, Object> optionalParameters, ViewJpqlMacro viewJpqlMacro, EmbeddingViewJpqlMacro embeddingViewJpqlMacro) {
    String oldViewPath = viewJpqlMacro.getViewPath();
    String oldEmbeddingViewPath = embeddingViewJpqlMacro.getEmbeddingViewPath();
    viewJpqlMacro.setViewPath(viewPath);
    embeddingViewJpqlMacro.setEmbeddingViewPath(embeddingViewPath);
    CorrelationProvider correlationProvider = correlationProviderFactory.create(parameterHolder, optionalParameters);
    String correlationAlias;
    if (limiter == null) {
        correlationAlias = entityViewRootName;
    } else {
        correlationAlias = "_sub_" + entityViewRootName;
    }
    JoinCorrelationBuilder correlationBuilder = new JoinCorrelationBuilder(parameterHolder, optionalParameters, fullQueryBuilder, viewJpqlMacro.getViewPath(), correlationAlias, entityViewRootName, attributePath, joinType, limiter);
    correlationProvider.applyCorrelation(correlationBuilder, viewJpqlMacro.getViewPath());
    correlationBuilder.finish();
    fullQueryBuilder.fetch(fetches);
    embeddingViewJpqlMacro.setEmbeddingViewPath(oldEmbeddingViewPath);
    viewJpqlMacro.setViewPath(oldViewPath);
}
Also used : JoinCorrelationBuilder(com.blazebit.persistence.view.impl.objectbuilder.transformer.correlation.JoinCorrelationBuilder) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider)

Example 3 with CorrelationProvider

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

the class AbstractCorrelatedBatchTupleListTransformer method applyAndGetCorrelationRoot.

private String applyAndGetCorrelationRoot(BatchCorrelationMode batchCorrelationMode) {
    Class<?> viewRootEntityClass = viewRootType.getEntityClass();
    Class<?> embeddingViewEntityClass = embeddingViewType.getEntityClass();
    String viewRootIdAttributePath = getEntityIdName(viewRootEntityClass);
    String embeddingViewIdAttributePath = getEntityIdName(embeddingViewEntityClass);
    FullQueryBuilder<?, ?> queryBuilder = entityViewConfiguration.getCriteriaBuilder();
    Map<String, Object> optionalParameters = entityViewConfiguration.getOptionalParameters();
    Class<?> correlationBasisEntityType;
    String viewRootExpression;
    String embeddingViewExpression;
    boolean batchedIdValues = false;
    if (batchCorrelationMode == BatchCorrelationMode.VALUES) {
        correlationBasisEntityType = correlationBasisEntity;
        viewRootExpression = null;
        batchedIdValues = correlatesThis && correlationBasisEntity == null;
        embeddingViewExpression = correlatesThis ? CORRELATION_KEY_ALIAS : null;
    } else if (batchCorrelationMode == BatchCorrelationMode.VIEW_ROOTS) {
        correlationBasisEntityType = viewRootEntityClass;
        viewRootExpression = CORRELATION_KEY_ALIAS;
        embeddingViewExpression = null;
    } else {
        correlationBasisEntityType = embeddingViewEntityClass;
        viewRootExpression = null;
        embeddingViewExpression = CORRELATION_KEY_ALIAS;
    }
    this.criteriaBuilder = queryBuilder.getCriteriaBuilderFactory().create(queryBuilder.getEntityManager(), Object[].class);
    if (queryBuilder instanceof CTEBuilder<?>) {
        this.criteriaBuilder.withCtesFrom((CTEBuilder<?>) queryBuilder);
    }
    this.viewRootJpqlMacro = new CorrelatedSubqueryViewRootJpqlMacro(criteriaBuilder, optionalParameters, viewRootExpression != null, viewRootEntityClass, viewRootIdAttributePath, viewRootExpression);
    this.embeddingViewJpqlMacro = new CorrelatedSubqueryEmbeddingViewJpqlMacro(criteriaBuilder, optionalParameters, embeddingViewExpression != null, embeddingViewEntityClass, embeddingViewIdAttributePath, embeddingViewExpression, batchedIdValues, viewRootJpqlMacro);
    this.criteriaBuilder.registerMacro("view", new MutableViewJpqlMacro(correlationResult));
    this.criteriaBuilder.registerMacro("view_root", viewRootJpqlMacro);
    this.criteriaBuilder.registerMacro("embedding_view", embeddingViewJpqlMacro);
    String joinBase = CORRELATION_KEY_ALIAS;
    SubqueryCorrelationBuilder correlationBuilder = new SubqueryCorrelationBuilder(queryBuilder, optionalParameters, criteriaBuilder, correlationAlias, correlationExternalAlias, correlationResult, correlationBasisType, correlationBasisEntityType, joinBase, attributePath, batchSize, limiter, false);
    CorrelationProvider provider = correlationProviderFactory.create(entityViewConfiguration.getCriteriaBuilder(), entityViewConfiguration.getOptionalParameters());
    String correlationKeyExpression;
    if (batchSize > 1) {
        if (batchCorrelationMode == BatchCorrelationMode.VALUES) {
            this.correlationParamName = CORRELATION_KEY_ALIAS;
        // TODO: when using EMBEDDING_VIEW, we could make use of correlationBasis instead of binding parameters separately
        } else {
            this.correlationParamName = generateCorrelationParamName();
        }
        if (correlationBasisEntityType != null) {
            correlationKeyExpression = CORRELATION_KEY_ALIAS;
            if (batchCorrelationMode == BatchCorrelationMode.VALUES) {
                correlationSelectExpression = CORRELATION_KEY_ALIAS + '.' + getEntityIdName(correlationBasisEntityType);
            } else {
                correlationSelectExpression = CORRELATION_KEY_ALIAS + '.' + viewRootIdAttributePath;
            }
        } else {
            // The correlation key is basic type
            correlationSelectExpression = correlationKeyExpression = CORRELATION_KEY_ALIAS;
        }
    } else {
        this.correlationParamName = generateCorrelationParamName();
        this.correlationSelectExpression = correlationKeyExpression = null;
    }
    int originalFirstResult = criteriaBuilder.getFirstResult();
    int originalMaxResults = criteriaBuilder.getMaxResults();
    if (batchSize > 1 && batchCorrelationMode == BatchCorrelationMode.VALUES) {
        provider.applyCorrelation(correlationBuilder, correlationKeyExpression);
    } else {
        provider.applyCorrelation(correlationBuilder, ':' + correlationParamName);
    }
    if (batchSize > 1 && (originalFirstResult != criteriaBuilder.getFirstResult() || originalMaxResults != criteriaBuilder.getMaxResults())) {
        throw new IllegalArgumentException("Correlation provider '" + provider + "' wrongly uses setFirstResult() or setMaxResults() on the query builder which might lead to wrong results. Use SELECT fetching with batch size 1 or reformulate the correlation provider to use the limit/offset in a subquery!");
    }
    if (fetches.length != 0) {
        for (int i = 0; i < fetches.length; i++) {
            criteriaBuilder.fetch(fetches[i]);
        }
    }
    if (indexFetches.length != 0) {
        for (int i = 0; i < indexFetches.length; i++) {
            criteriaBuilder.fetch(indexFetches[i]);
        }
    }
    return correlationBuilder.getCorrelationRoot();
}
Also used : CorrelatedSubqueryViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryViewRootJpqlMacro) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider) CorrelatedSubqueryEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryEmbeddingViewJpqlMacro) CTEBuilder(com.blazebit.persistence.CTEBuilder)

Example 4 with CorrelationProvider

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

the class AbstractCorrelatedSubselectTupleTransformer method prepare.

private void prepare() {
    JpaProvider jpaProvider = entityViewConfiguration.getCriteriaBuilder().getService(JpaProvider.class);
    FullQueryBuilder<?, ?> queryBuilder = entityViewConfiguration.getCriteriaBuilder();
    Map<String, Object> optionalParameters = entityViewConfiguration.getOptionalParameters();
    Class<?> correlationBasisEntityType = correlationBasisEntity;
    String viewRootExpression = viewRootAlias;
    EmbeddingViewJpqlMacro embeddingViewJpqlMacro = entityViewConfiguration.getEmbeddingViewJpqlMacro();
    ViewJpqlMacro viewJpqlMacro = entityViewConfiguration.getViewJpqlMacro();
    if (queryBuilder instanceof PaginatedCriteriaBuilder<?>) {
        criteriaBuilder = queryBuilder.copyCriteriaBuilder(Object[].class, false);
    } else {
        LimitBuilder<?> limitBuilder = (LimitBuilder<?>) queryBuilder;
        // To set the limit, we need the JPA provider to support this
        if (jpaProvider.supportsSubqueryInFunction() && (limitBuilder.getFirstResult() > 0 || limitBuilder.getMaxResults() < Integer.MAX_VALUE)) {
            // we must turn this query builder into a paginated criteria builder first
            try {
                criteriaBuilder = queryBuilder.copyCriteriaBuilder(Object[].class, true).page(limitBuilder.getFirstResult(), limitBuilder.getMaxResults()).copyCriteriaBuilder(Object[].class, false);
            } catch (IllegalStateException ex) {
                LOG.log(Level.WARNING, "Could not create a paginated criteria builder for SUBSELECT fetching which might lead to bad performance", ex);
                criteriaBuilder = queryBuilder.copyCriteriaBuilder(Object[].class, false);
            }
        } else {
            // Regular query without limit/offset
            criteriaBuilder = queryBuilder.copyCriteriaBuilder(Object[].class, false);
        }
    }
    int originalFirstResult = 0;
    int originalMaxResults = Integer.MAX_VALUE;
    // A copied query that is extended with further joins can't possibly use the limits provided by the outer query
    ((LimitBuilder<?>) criteriaBuilder).setFirstResult(originalFirstResult);
    ((LimitBuilder<?>) criteriaBuilder).setMaxResults(originalMaxResults);
    this.viewRootJpqlMacro = new CorrelatedSubqueryViewRootJpqlMacro(criteriaBuilder, optionalParameters, false, viewRootEntityClass, idAttributePath, viewRootExpression);
    criteriaBuilder.registerMacro("view", viewJpqlMacro);
    criteriaBuilder.registerMacro("view_root", viewRootJpqlMacro);
    criteriaBuilder.registerMacro("embedding_view", embeddingViewJpqlMacro);
    String oldViewPath = viewJpqlMacro.getViewPath();
    String oldEmbeddingViewPath = embeddingViewJpqlMacro.getEmbeddingViewPath();
    viewJpqlMacro.setViewPath(correlationResultExpression);
    embeddingViewJpqlMacro.setEmbeddingViewPath(embeddingViewPath);
    String joinBase = embeddingViewPath;
    SubqueryCorrelationBuilder correlationBuilder = new SubqueryCorrelationBuilder(queryBuilder, optionalParameters, criteriaBuilder, correlationAlias, correlationExternalAlias, correlationResultExpression, correlationBasisType, correlationBasisEntityType, joinBase, attributePath, 1, limiter, true);
    CorrelationProvider provider = correlationProviderFactory.create(entityViewConfiguration.getCriteriaBuilder(), entityViewConfiguration.getOptionalParameters());
    provider.applyCorrelation(correlationBuilder, correlationBasisExpression);
    if (criteriaBuilder instanceof LimitBuilder<?>) {
        if (originalFirstResult != ((LimitBuilder<?>) criteriaBuilder).getFirstResult() || originalMaxResults != ((LimitBuilder<?>) criteriaBuilder).getMaxResults()) {
            throw new IllegalArgumentException("Correlation provider '" + provider + "' wrongly uses setFirstResult() or setMaxResults() on the query builder which might lead to wrong results. Use SELECT fetching with batch size 1 or reformulate the correlation provider to use the limit/offset in a subquery!");
        }
    }
    if (fetches.length != 0) {
        for (int i = 0; i < fetches.length; i++) {
            criteriaBuilder.fetch(fetches[i]);
        }
    }
    if (indexFetches.length != 0) {
        for (int i = 0; i < indexFetches.length; i++) {
            criteriaBuilder.fetch(indexFetches[i]);
        }
    }
    // Before we can determine whether we use view roots or embedding views, we need to add all selects, otherwise macros might report false although they are used
    final String correlationRoot = correlationBuilder.getCorrelationRoot();
    final int tupleSuffix = maximumViewMapperCount + 1 + (indexCorrelator == null && indexExpression == null ? 0 : 1);
    ObjectBuilder<Object[]> objectBuilder = (ObjectBuilder<Object[]>) correlator.finish(criteriaBuilder, entityViewConfiguration, 0, tupleSuffix, correlationRoot, embeddingViewJpqlMacro, true);
    final boolean usesViewRoot = viewRootJpqlMacro.usesViewMacro();
    final boolean usesEmbeddingView = embeddingViewJpqlMacro.usesEmbeddingView();
    if (usesEmbeddingView && !(embeddingViewType instanceof ViewType<?>)) {
        throw new IllegalStateException("The use of EMBEDDING_VIEW in the correlation for '" + embeddingViewType.getJavaType().getName() + "." + attributePath.substring(attributePath.lastIndexOf('.') + 1) + "' is illegal because the embedding view type '" + embeddingViewType.getJavaType().getName() + "' does not declare a @IdMapping!");
    } else if (usesViewRoot && !(viewRootType instanceof ViewType<?>)) {
        throw new IllegalStateException("The use of VIEW_ROOT in the correlation for '" + embeddingViewType.getJavaType().getName() + "." + attributePath.substring(attributePath.lastIndexOf('.') + 1) + "' is illegal because the view root type '" + viewRootType.getJavaType().getName() + "' does not declare a @IdMapping!");
    }
    final int maximumSlotsFilled;
    final int elementKeyIndex;
    final int elementViewIndex;
    if (usesEmbeddingView) {
        maximumSlotsFilled = embeddingViewIdMapperCount == 0 ? 1 : embeddingViewIdMapperCount;
        elementKeyIndex = (maximumViewMapperCount - maximumSlotsFilled) + 2 + valueIndex;
        elementViewIndex = (maximumViewMapperCount - maximumSlotsFilled) + 1 + valueIndex;
        viewIndex = embeddingViewIndex;
    } else if (usesViewRoot) {
        maximumSlotsFilled = viewRootIdMapperCount == 0 ? 1 : viewRootIdMapperCount;
        elementKeyIndex = (maximumViewMapperCount - maximumSlotsFilled) + 2 + valueIndex;
        elementViewIndex = (maximumViewMapperCount - maximumSlotsFilled) + 1 + valueIndex;
        viewIndex = viewRootIndex;
    } else {
        maximumSlotsFilled = 0;
        elementKeyIndex = maximumViewMapperCount + 1 + valueIndex;
        elementViewIndex = 1 + valueIndex;
        viewIndex = -1;
    }
    for (int i = maximumSlotsFilled; i < maximumViewMapperCount; i++) {
        criteriaBuilder.select("NULL");
    }
    ExpressionFactory ef = criteriaBuilder.getService(ExpressionFactory.class);
    if (usesEmbeddingView) {
        EntityViewConfiguration configuration = new EntityViewConfiguration(criteriaBuilder, ef, new MutableViewJpqlMacro(), new MutableEmbeddingViewJpqlMacro(), Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), entityViewConfiguration.getFetches(), attributePath);
        ObjectBuilder<Object[]> embeddingViewObjectBuilder = createViewAwareObjectBuilder(criteriaBuilder, embeddingViewType, configuration, embeddingViewIdExpression);
        if (embeddingViewObjectBuilder == null) {
            criteriaBuilder.select(embeddingViewIdExpression);
        } else {
            criteriaBuilder.selectNew(objectBuilder = new LateAdditionalObjectBuilder(objectBuilder, embeddingViewObjectBuilder, true));
        }
    } else if (usesViewRoot) {
        EntityViewConfiguration configuration = new EntityViewConfiguration(criteriaBuilder, ef, new MutableViewJpqlMacro(), new MutableEmbeddingViewJpqlMacro(), Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), entityViewConfiguration.getFetches(), attributePath);
        ObjectBuilder<Object[]> viewRootObjectBuilder = createViewAwareObjectBuilder(criteriaBuilder, viewRootType, configuration, viewRootIdExpression);
        if (viewRootObjectBuilder == null) {
            criteriaBuilder.select(viewRootIdExpression);
        } else {
            criteriaBuilder.selectNew(objectBuilder = new LateAdditionalObjectBuilder(objectBuilder, viewRootObjectBuilder, true));
        }
    }
    criteriaBuilder.select(correlationKeyExpression);
    if (indexCorrelator != null) {
        ObjectBuilder<?> indexBuilder = indexCorrelator.finish(criteriaBuilder, entityViewConfiguration, maximumViewMapperCount + 2, 0, indexExpression, embeddingViewJpqlMacro, true);
        if (indexBuilder != null) {
            criteriaBuilder.selectNew(new LateAdditionalObjectBuilder(objectBuilder, indexBuilder, false));
        }
    }
    populateParameters(entityViewConfiguration, criteriaBuilder);
    viewJpqlMacro.setViewPath(oldViewPath);
    embeddingViewJpqlMacro.setEmbeddingViewPath(oldEmbeddingViewPath);
    List<Object[]> resultList = (List<Object[]>) criteriaBuilder.getResultList();
    Map<Object, Map<Object, Object>> collections = new HashMap<>(resultList.size());
    for (int i = 0; i < resultList.size(); i++) {
        Object[] element = resultList.get(i);
        Map<Object, Object> viewRootResult = collections.get(element[elementViewIndex]);
        if (viewRootResult == null) {
            viewRootResult = new HashMap<>();
            collections.put(element[elementViewIndex], viewRootResult);
        }
        if (this.containerAccumulator == null) {
            viewRootResult.put(element[elementKeyIndex], element[valueIndex]);
        } else {
            Object result = viewRootResult.get(element[elementKeyIndex]);
            if (result == null) {
                result = createDefaultResult();
                viewRootResult.put(element[elementKeyIndex], result);
            }
            Object indexObject = null;
            if (indexCorrelator != null || indexExpression != null) {
                indexObject = element[elementKeyIndex + 1];
            }
            this.containerAccumulator.add(result, indexObject, element[valueIndex], isRecording());
        }
    }
    this.collections = collections;
}
Also used : EntityViewConfiguration(com.blazebit.persistence.view.impl.EntityViewConfiguration) LateAdditionalObjectBuilder(com.blazebit.persistence.view.impl.objectbuilder.LateAdditionalObjectBuilder) HashMap(java.util.HashMap) ViewJpqlMacro(com.blazebit.persistence.view.spi.ViewJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) PaginatedCriteriaBuilder(com.blazebit.persistence.PaginatedCriteriaBuilder) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) List(java.util.List) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) JpaProvider(com.blazebit.persistence.spi.JpaProvider) LimitBuilder(com.blazebit.persistence.LimitBuilder) CorrelatedSubqueryViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryViewRootJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) ObjectBuilder(com.blazebit.persistence.ObjectBuilder) LateAdditionalObjectBuilder(com.blazebit.persistence.view.impl.objectbuilder.LateAdditionalObjectBuilder) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider) HashMap(java.util.HashMap) Map(java.util.Map) UpdatableViewMap(com.blazebit.persistence.view.impl.objectbuilder.transformator.UpdatableViewMap) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType)

Example 5 with CorrelationProvider

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

the class CorrelatedSubviewJoinTupleTransformerFactory method create.

@Override
public TupleTransformer create(ParameterHolder<?> parameterHolder, Map<String, Object> optionalParameters, EntityViewConfiguration entityViewConfiguration) {
    if (!entityViewConfiguration.hasSubFetches(attributePath)) {
        return new NullTupleTransformer(template);
    }
    // For now it's ok, but at some point we will want to support correlated attributes somehow and need to think of a fallback solution here
    if (parameterHolder instanceof FullQueryBuilder<?, ?>) {
        FullQueryBuilder<?, ?> queryBuilder = (FullQueryBuilder<?, ?>) parameterHolder;
        CorrelationProvider provider = correlationProviderFactory.create(parameterHolder, optionalParameters);
        JoinCorrelationBuilder correlationBuilder = new JoinCorrelationBuilder(parameterHolder, optionalParameters, queryBuilder, joinBase, correlationAlias, correlationExternalAlias, attributePath, JoinType.LEFT, limiter);
        int originalFirstResult = -1;
        int originalMaxResults = -1;
        if (queryBuilder instanceof LimitBuilder<?>) {
            originalFirstResult = ((LimitBuilder<?>) queryBuilder).getFirstResult();
            originalMaxResults = ((LimitBuilder<?>) queryBuilder).getMaxResults();
        }
        ViewJpqlMacro viewJpqlMacro = entityViewConfiguration.getViewJpqlMacro();
        EmbeddingViewJpqlMacro embeddingViewJpqlMacro = entityViewConfiguration.getEmbeddingViewJpqlMacro();
        String oldViewPath = viewJpqlMacro.getViewPath();
        String oldEmbeddingViewPath = embeddingViewJpqlMacro.getEmbeddingViewPath();
        // If this uses a static path, we need to avoid setting the embedding view path etc.
        if (!(provider instanceof StaticPathCorrelationProvider)) {
            viewJpqlMacro.setViewPath(correlationBuilder.getCorrelationAlias());
            embeddingViewJpqlMacro.setEmbeddingViewPath(embeddingViewPath);
        }
        provider.applyCorrelation(correlationBuilder, correlationBasis);
        if (queryBuilder instanceof LimitBuilder<?>) {
            if (originalFirstResult != ((LimitBuilder<?>) queryBuilder).getFirstResult() || originalMaxResults != ((LimitBuilder<?>) queryBuilder).getMaxResults()) {
                throw new IllegalArgumentException("Correlation provider '" + provider + "' wrongly uses setFirstResult() or setMaxResults() on the query builder which might lead to wrong results. Use SELECT fetching with batch size 1 or reformulate the correlation provider to use the limit/offset in a subquery!");
            }
        }
        correlationBuilder.finish();
        if (!(provider instanceof StaticPathCorrelationProvider)) {
            viewJpqlMacro.setViewPath(oldViewPath);
            embeddingViewJpqlMacro.setEmbeddingViewPath(oldEmbeddingViewPath);
        }
        if (fetches.length != 0) {
            for (int i = 0; i < fetches.length; i++) {
                queryBuilder.fetch(correlationBuilder.getCorrelationAlias() + "." + fetches[i]);
            }
        }
        ObjectBuilder<Object[]> objectBuilder = template.createObjectBuilder(parameterHolder, optionalParameters, entityViewConfiguration, 0, true, false);
        return new CorrelatedSubviewJoinTupleTransformer(template, objectBuilder);
    } else {
        throw new UnsupportedOperationException("Converting views with correlated attributes isn't supported!");
    }
}
Also used : LimitBuilder(com.blazebit.persistence.LimitBuilder) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) ViewJpqlMacro(com.blazebit.persistence.view.spi.ViewJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) CorrelationProvider(com.blazebit.persistence.view.CorrelationProvider) StaticPathCorrelationProvider(com.blazebit.persistence.view.impl.StaticPathCorrelationProvider) StaticPathCorrelationProvider(com.blazebit.persistence.view.impl.StaticPathCorrelationProvider) FullQueryBuilder(com.blazebit.persistence.FullQueryBuilder) NullTupleTransformer(com.blazebit.persistence.view.impl.objectbuilder.transformer.NullTupleTransformer)

Aggregations

CorrelationProvider (com.blazebit.persistence.view.CorrelationProvider)7 LimitBuilder (com.blazebit.persistence.LimitBuilder)3 JoinCorrelationBuilder (com.blazebit.persistence.view.impl.objectbuilder.transformer.correlation.JoinCorrelationBuilder)3 CTEBuilder (com.blazebit.persistence.CTEBuilder)2 ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)2 StaticPathCorrelationProvider (com.blazebit.persistence.view.impl.StaticPathCorrelationProvider)2 CorrelatedSubqueryViewRootJpqlMacro (com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryViewRootJpqlMacro)2 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)2 EmbeddingViewJpqlMacro (com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro)2 ViewJpqlMacro (com.blazebit.persistence.view.spi.ViewJpqlMacro)2 FetchBuilder (com.blazebit.persistence.FetchBuilder)1 FullQueryBuilder (com.blazebit.persistence.FullQueryBuilder)1 ObjectBuilder (com.blazebit.persistence.ObjectBuilder)1 PaginatedCriteriaBuilder (com.blazebit.persistence.PaginatedCriteriaBuilder)1 JpaProvider (com.blazebit.persistence.spi.JpaProvider)1 CTEProvider (com.blazebit.persistence.view.CTEProvider)1 EntityViewConfiguration (com.blazebit.persistence.view.impl.EntityViewConfiguration)1 StaticCorrelationProvider (com.blazebit.persistence.view.impl.StaticCorrelationProvider)1 CorrelatedSubqueryEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.CorrelatedSubqueryEmbeddingViewJpqlMacro)1 MutableEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro)1