Search in sources :

Example 1 with Specification

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Specification(io.micronaut.data.jpa.repository.criteria.Specification) Predicate(javax.persistence.criteria.Predicate)

Example 2 with Specification

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());
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Specification(io.micronaut.data.jpa.repository.criteria.Specification) Predicate(javax.persistence.criteria.Predicate)

Example 3 with Specification

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);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Specification(io.micronaut.data.jpa.repository.criteria.Specification) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Pageable(io.micronaut.data.model.Pageable) Sort(io.micronaut.data.model.Sort)

Example 4 with Specification

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();
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) EmptyResultException(io.micronaut.data.exceptions.EmptyResultException) Specification(io.micronaut.data.jpa.repository.criteria.Specification) NoResultException(javax.persistence.NoResultException) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Specification (io.micronaut.data.jpa.repository.criteria.Specification)4 EntityManager (javax.persistence.EntityManager)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 EmptyResultException (io.micronaut.data.exceptions.EmptyResultException)1 Pageable (io.micronaut.data.model.Pageable)1 Sort (io.micronaut.data.model.Sort)1 NoResultException (javax.persistence.NoResultException)1