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();
}
}
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);
}
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);
}
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);
}
Aggregations