use of io.micronaut.data.jpa.repository.criteria.Specification in project micronaut-data by micronaut-projects.
the class FindAllSpecificationInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
final Specification specification = getSpecification(context);
final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Object> query = criteriaBuilder.createQuery((Class<Object>) getRequiredRootEntity(context));
final Root<Object> root = query.from((Class<Object>) getRequiredRootEntity(context));
final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
if (predicate != null) {
query.where(predicate);
}
query.select(root);
if (context.getParameterValues().length > 1) {
addSort(context.getParameterValues()[1], query, root, criteriaBuilder);
}
return entityManager.createQuery(query).getResultList();
}
use of io.micronaut.data.jpa.repository.criteria.Specification in project micronaut-data by micronaut-projects.
the class CountSpecificationInterceptor method intercept.
@Override
public Number intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Number> context) {
Specification specification = getSpecification(context);
final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
final Root<?> root = query.from(getRequiredRootEntity(context));
final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
if (predicate != null) {
query.where(predicate);
}
if (query.isDistinct()) {
query.select(criteriaBuilder.countDistinct(root));
} else {
query.select(criteriaBuilder.count(root));
}
query.orderBy(Collections.emptyList());
final TypedQuery<Long> typedQuery = entityManager.createQuery(query);
final Long result = typedQuery.getSingleResult();
final ReturnType<Number> rt = context.getReturnType();
final Class<Number> returnType = rt.getType();
if (returnType.isInstance(result)) {
return result;
} else {
return operations.getConversionService().convertRequired(result, rt.asArgument());
}
}
use of io.micronaut.data.jpa.repository.criteria.Specification in project micronaut-data by micronaut-projects.
the class FindPageSpecificationInterceptor method intercept.
@Override
public Page intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
if (context.getParameterValues().length != 2) {
throw new IllegalStateException("Expected exactly 2 arguments to method");
}
Specification specification = getSpecification(context);
final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
Class<Object> rootEntity = getRequiredRootEntity(context);
final CriteriaQuery<Object> query = criteriaBuilder.createQuery(rootEntity);
final Root<Object> root = query.from(rootEntity);
final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
if (predicate != null) {
query.where(predicate);
}
query.select(root);
Pageable pageable = getPageable(context);
final Sort sort = pageable.getSort();
if (sort.isSorted()) {
query.orderBy(getOrders(sort, root, criteriaBuilder));
}
final TypedQuery<Object> typedQuery = entityManager.createQuery(query);
if (pageable.isUnpaged()) {
List<Object> resultList = typedQuery.getResultList();
return Page.of(resultList, pageable, resultList.size());
} else {
typedQuery.setFirstResult((int) pageable.getOffset());
typedQuery.setMaxResults(pageable.getSize());
final List<Object> results = typedQuery.getResultList();
final CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
final Root<?> countRoot = countQuery.from(rootEntity);
final Predicate countPredicate = specification.toPredicate(countRoot, countQuery, criteriaBuilder);
if (countPredicate != null) {
countQuery.where(countPredicate);
}
countQuery.select(criteriaBuilder.count(countRoot));
Long singleResult = entityManager.createQuery(countQuery).getSingleResult();
return Page.of(results, pageable, singleResult);
}
}
use of io.micronaut.data.jpa.repository.criteria.Specification in project micronaut-data by micronaut-projects.
the class FindOneSpecificationInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
Specification specification = getSpecification(context);
final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Object> query = criteriaBuilder.createQuery((Class<Object>) getRequiredRootEntity(context));
final Root<Object> root = query.from((Class<Object>) getRequiredRootEntity(context));
final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
if (predicate != null) {
query.where(predicate);
}
query.select(root);
final TypedQuery<?> typedQuery = entityManager.createQuery(query);
try {
final Object result = typedQuery.getSingleResult();
final ReturnType<?> rt = context.getReturnType();
final Class<?> returnType = rt.getType();
if (returnType.isInstance(result)) {
return result;
} else {
return operations.getConversionService().convertRequired(result, rt.asArgument());
}
} catch (NoResultException e) {
if (context.isNullable()) {
return null;
} else {
throw new EmptyResultException();
}
}
}
Aggregations