Search in sources :

Example 1 with BlazeCriteriaBuilder

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

the class QueryCriteria method createQuery.

@Override
public TypedQuery<R> createQuery() {
    if (entityViewCriteria) {
        try {
            BlazeCriteriaBuilder builder = BlazeCriteria.get(criteriaBuilderFactory);
            BlazeCriteriaQuery<C> query = builder.createQuery(entityClass);
            From<C, C> root = query.from(entityClass);
            List<Predicate> predicates = predicates(builder, root);
            query.distinct(isDistinct());
            if (!predicates.isEmpty()) {
                query.where(predicates.toArray(new Predicate[predicates.size()]));
            }
            applyProcessors(query, builder, root);
            return entityViewManager.applySetting(EntityViewSetting.create(resultClass), query.createCriteriaBuilder(entityManager)).getQuery();
        } catch (RuntimeException e) {
            LOG.log(Level.SEVERE, "Exception while creating JPA query", e);
            throw e;
        }
    } else {
        return super.createQuery();
    }
}
Also used : BlazeCriteriaBuilder(com.blazebit.persistence.criteria.BlazeCriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 2 with BlazeCriteriaBuilder

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

the class AbstractEntityViewAwareRepositoryHandler method findAll.

@Override
public Page<V> findAll(Specification<E> specification, Pageable pageable) {
    CriteriaBuilder<E> cb;
    if (specification == null) {
        cb = createCriteriaBuilder();
    } else {
        BlazeCriteriaBuilder blazeCriteriaBuilder = BlazeCriteria.get(criteriaBuilderFactory());
        BlazeCriteriaQuery<?> query = blazeCriteriaBuilder.createQuery(entityClass());
        Root queryRoot = query.from(entityClass());
        Predicate predicate = specification.toPredicate(queryRoot, query, blazeCriteriaBuilder);
        if (predicate != null) {
            if (query.getRestriction() == null) {
                query.where(predicate);
            } else {
                query.where(query.getRestriction(), predicate);
            }
        }
        cb = (CriteriaBuilder<E>) query.createCriteriaBuilder(entityManager());
    }
    String[] fetches = getFetches();
    if (fetches.length != 0) {
        cb.fetch(fetches);
    }
    boolean withCountQuery = true;
    boolean withKeysetExtraction = false;
    boolean withExtractAllKeysets = false;
    TypedQuery<V> query;
    if (viewClass() == null) {
        PaginatedCriteriaBuilder<V> pcb;
        if (pageable instanceof KeysetPageable) {
            KeysetPageable keysetPageable = (KeysetPageable) pageable;
            pcb = (PaginatedCriteriaBuilder<V>) cb.page(keysetPageable.getKeysetPage(), pageable.getOffset(), pageable.getPageSize());
            withCountQuery = keysetPageable.isWithCountQuery();
            withKeysetExtraction = true;
            withExtractAllKeysets = keysetPageable.isWithExtractAllKeysets();
        } else {
            pcb = (PaginatedCriteriaBuilder<V>) cb.page(pageable.getOffset(), pageable.getPageSize());
        }
        QueryBuilderUtils.applySort(pageable.getSort(), pcb);
        pcb.withCountQuery(withCountQuery);
        pcb.withKeysetExtraction(withKeysetExtraction);
        pcb.withExtractAllKeysets(withExtractAllKeysets);
        query = pcb.getQuery();
    } else {
        EntityViewSetting<V, PaginatedCriteriaBuilder<V>> setting = EntityViewSetting.create(viewClass(), pageable.getOffset(), pageable.getPageSize());
        if (pageable instanceof KeysetPageable) {
            KeysetPageable keysetPageable = (KeysetPageable) pageable;
            setting.withKeysetPage(keysetPageable.getKeysetPage());
            withCountQuery = keysetPageable.isWithCountQuery();
            withKeysetExtraction = true;
            withExtractAllKeysets = keysetPageable.isWithExtractAllKeysets();
        }
        QueryBuilderUtils.applySort(pageable.getSort(), setting);
        PaginatedCriteriaBuilder<V> pcb = applySetting(setting, cb);
        pcb.withCountQuery(withCountQuery);
        pcb.withKeysetExtraction(withKeysetExtraction);
        pcb.withExtractAllKeysets(withExtractAllKeysets);
        query = pcb.getQuery();
    }
    applyQueryHints(query, fetches.length == 0);
    PagedList<V> resultList = (PagedList<V>) query.getResultList();
    return new KeysetAwarePageImpl<>(resultList, pageable);
}
Also used : Root(javax.persistence.criteria.Root) PagedList(com.blazebit.persistence.PagedList) BlazeCriteriaBuilder(com.blazebit.persistence.criteria.BlazeCriteriaBuilder) PaginatedCriteriaBuilder(com.blazebit.persistence.PaginatedCriteriaBuilder) Predicate(javax.persistence.criteria.Predicate) KeysetPageable(com.blazebit.persistence.deltaspike.data.KeysetPageable)

Example 3 with BlazeCriteriaBuilder

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

Example 4 with BlazeCriteriaBuilder

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

the class AbstractEntityViewAwareRepository method getCountQuery.

protected <S extends E> TypedQuery<Long> getCountQuery(Specification<S> spec, Class<S> domainClass) {
    BlazeCriteriaBuilder builder = BlazeCriteria.get(cbf);
    BlazeCriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<S> root = applySpecificationToCriteria(spec, domainClass, query);
    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }
    // Remove all Orders the Specifications might have applied
    query.orderBy(Collections.<Order>emptyList());
    return this.applyRepositoryMethodMetadata(query.createCriteriaBuilder(entityManager).getQuery(), true);
}
Also used : BlazeCriteriaBuilder(com.blazebit.persistence.criteria.BlazeCriteriaBuilder)

Aggregations

BlazeCriteriaBuilder (com.blazebit.persistence.criteria.BlazeCriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)3 Root (javax.persistence.criteria.Root)2 PagedList (com.blazebit.persistence.PagedList)1 PaginatedCriteriaBuilder (com.blazebit.persistence.PaginatedCriteriaBuilder)1 BlazeCriteriaQuery (com.blazebit.persistence.criteria.BlazeCriteriaQuery)1 KeysetPageable (com.blazebit.persistence.deltaspike.data.KeysetPageable)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