Search in sources :

Example 1 with ExtendedParameters

use of com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters in project blaze-persistence by Blazebit.

the class CountQueryPostProcessor method postProcess.

@Override
public Query postProcess(EntityViewCdiQueryInvocationContext context, Query query) {
    String queryString = getQueryString(context, query);
    CountQueryPostProcessor.QueryExtraction extract = new CountQueryPostProcessor.QueryExtraction(queryString);
    String count = extract.rewriteToCount();
    LOG.log(Level.FINER, "Rewrote query {0} to {1}", new Object[] { queryString, count });
    Query result = context.getEntityManager().createQuery(count);
    ExtendedParameters params = context.getParams();
    params.applyTo(result);
    return result;
}
Also used : Query(javax.persistence.Query) ExtendedParameters(com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters)

Example 2 with ExtendedParameters

use of com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters in project blaze-persistence by Blazebit.

the class EntityViewAnnotatedQueryBuilder method createJpaQuery.

private javax.persistence.Query createJpaQuery(Query query, EntityViewCdiQueryInvocationContext context) {
    EntityManager entityManager = context.getEntityManager();
    ExtendedParameters params = context.getParams();
    javax.persistence.Query result = null;
    if (isNotEmpty(query.named())) {
        if (context.hasQueryStringPostProcessors()) {
            javax.persistence.Query namedQuery = entityManager.createNamedQuery(query.named());
            String named = factory.extract(namedQuery);
            String jpqlQuery = context.applyQueryStringPostProcessors(named);
            result = params.applyTo(entityManager.createQuery(jpqlQuery));
        } else {
            result = params.applyTo(entityManager.createNamedQuery(query.named()));
        }
    } else if (query.isNative()) {
        String jpqlQuery = context.applyQueryStringPostProcessors(query.value());
        Class<?> resultType = getQueryResultType(context.getMethod());
        if (isEntityType(resultType)) {
            result = params.applyTo(entityManager.createNativeQuery(jpqlQuery, resultType));
        } else {
            result = params.applyTo(entityManager.createNativeQuery(jpqlQuery));
        }
    } else {
        String jpqlQuery = context.applyQueryStringPostProcessors(query.value());
        context.setQueryString(jpqlQuery);
        result = params.applyTo(entityManager.createQuery(jpqlQuery));
    }
    return context.applyRestrictions(result);
}
Also used : EntityManager(javax.persistence.EntityManager) ExtendedParameters(com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters)

Example 3 with ExtendedParameters

use of com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters in project blaze-persistence by Blazebit.

the class EntityViewMethodQueryBuilder method createJpaQuery.

private <V> Query createJpaQuery(EntityViewCdiQueryInvocationContext context) {
    ExtendedParameters params = context.getExtendedParams();
    EntityViewQueryRoot root = context.getRepositoryMethodMetadata().getEntityViewQueryRoot();
    Pageable pageable = params.getPageable();
    CriteriaBuilder<?> cb;
    Specification<?> specification = params.getSpecification();
    if (specification == null) {
        cb = context.getCriteriaBuilderFactory().create(context.getEntityManager(), context.getEntityClass());
        root.apply(cb);
    } else {
        BlazeCriteriaBuilder blazeCriteriaBuilder = BlazeCriteria.get(context.getCriteriaBuilderFactory());
        BlazeCriteriaQuery<?> query = blazeCriteriaBuilder.createQuery(context.getEntityClass());
        Root queryRoot = query.from(context.getEntityClass());
        root.apply(queryRoot, query, blazeCriteriaBuilder);
        Predicate predicate = specification.toPredicate(queryRoot, query, blazeCriteriaBuilder);
        if (predicate != null) {
            if (query.getRestriction() == null) {
                query.where(predicate);
            } else {
                query.where(query.getRestriction(), predicate);
            }
        }
        cb = query.createCriteriaBuilder(context.getEntityManager());
    }
    Class<V> entityViewClass = (Class<V>) context.getEntityViewClass();
    // TODO: depending on return type we might need to do keyset extraction
    boolean keysetExtraction = false;
    FullQueryBuilder<?, ?> fullCb = QueryBuilderUtils.getFullQueryBuilder(cb, pageable, context.getEntityViewManager(), entityViewClass, keysetExtraction);
    fullCb = context.applyCriteriaBuilderPostProcessors(fullCb);
    Query q = params.applyTo(context.createQuery(fullCb));
    return context.applyRestrictions(q);
}
Also used : EntityViewQueryRoot(com.blazebit.persistence.deltaspike.data.impl.builder.part.EntityViewQueryRoot) EntityViewQueryRoot(com.blazebit.persistence.deltaspike.data.impl.builder.part.EntityViewQueryRoot) Root(javax.persistence.criteria.Root) Query(javax.persistence.Query) BlazeCriteriaQuery(com.blazebit.persistence.criteria.BlazeCriteriaQuery) BlazeCriteriaBuilder(com.blazebit.persistence.criteria.BlazeCriteriaBuilder) ExtendedParameters(com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters) Predicate(javax.persistence.criteria.Predicate) Pageable(com.blazebit.persistence.deltaspike.data.Pageable)

Aggregations

ExtendedParameters (com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters)3 Query (javax.persistence.Query)2 BlazeCriteriaBuilder (com.blazebit.persistence.criteria.BlazeCriteriaBuilder)1 BlazeCriteriaQuery (com.blazebit.persistence.criteria.BlazeCriteriaQuery)1 Pageable (com.blazebit.persistence.deltaspike.data.Pageable)1 EntityViewQueryRoot (com.blazebit.persistence.deltaspike.data.impl.builder.part.EntityViewQueryRoot)1 EntityManager (javax.persistence.EntityManager)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1