Search in sources :

Example 1 with NullTupleTransformer

use of com.blazebit.persistence.view.impl.objectbuilder.transformer.NullTupleTransformer 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

FullQueryBuilder (com.blazebit.persistence.FullQueryBuilder)1 LimitBuilder (com.blazebit.persistence.LimitBuilder)1 CorrelationProvider (com.blazebit.persistence.view.CorrelationProvider)1 StaticPathCorrelationProvider (com.blazebit.persistence.view.impl.StaticPathCorrelationProvider)1 NullTupleTransformer (com.blazebit.persistence.view.impl.objectbuilder.transformer.NullTupleTransformer)1 EmbeddingViewJpqlMacro (com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro)1 ViewJpqlMacro (com.blazebit.persistence.view.spi.ViewJpqlMacro)1