Search in sources :

Example 1 with BlazeCriteriaQuery

use of com.blazebit.persistence.criteria.BlazeCriteriaQuery 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

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 ExtendedParameters (com.blazebit.persistence.deltaspike.data.impl.param.ExtendedParameters)1 Query (javax.persistence.Query)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1