Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public List<Order> findList(Member member, Integer count, List<Filter> filters,
        List<net.groupbuy.Order> orders) {
    if (member == null) {
        return Collections.<Order>emptyList();
    }//from w  ww .  j  a v  a 2s.c o  m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:ru.portal.services.TableServiceImpl.java

@Override
public Page<HashMap<String, String>> findAll(String tableOrViewName, Pageable pageable) {

    List<HashMap<String, String>> result = new ArrayList<>();

    EntityType<?> type = null;/*  w w w  . ja  va 2 s  .c o m*/
    Set<EntityType<?>> set = em.getEntityManagerFactory().getMetamodel().getEntities();
    for (EntityType<?> entityType : set) {
        if (entityType.getBindableJavaType().getAnnotation(PortalTable.class) != null) {
            if (entityType.getBindableJavaType().getName().equals(tableOrViewName)) {
                type = entityType;
                break;
            }
        }
    }

    Long totalRows = 0L;

    if (type != null) {
        Class<?> bindableJavaType = type.getBindableJavaType();

        //count
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
        countQuery.select(criteriaBuilder.count(countQuery.from(bindableJavaType)));
        totalRows = em.createQuery(countQuery).getSingleResult();

        //select
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<?> cq = cb.createQuery(bindableJavaType);
        Root<?> root = cq.from(bindableJavaType);
        //          cq.select(root);
        if (pageable == null) {
            pageable = new PageRequest(0, 50);
        }

        TypedQuery<?> query = em.createQuery(cq);

        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<?> all = query.getResultList();

        List<String> columns = getTableOrViewMetaData(tableOrViewName);

        for (Object object : all) {

            HashMap<String, String> res = new HashMap<>(columns.size());
            Class<? extends Object> clazz = object.getClass();
            for (String fieldName : columns) {
                try {
                    Field field = clazz.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    Object fieldValue = field.get(object);
                    res.put(fieldName, fieldValue.toString());
                    //TODO cast data integer long etc
                } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                        | IllegalAccessException ex) {
                    Logger.getLogger(TableServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            result.add(res);
        }
    }

    PageImpl<HashMap<String, String>> list = new PageImpl<>(result, pageable, totalRows);
    return list;
}

From source file:br.ufba.dcc.mestrado.computacao.repository.impl.ProjectRepositoryImpl.java

@Override
public OpenHubProjectEntity findByName(String name) {
    CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<OpenHubProjectEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());

    Root<OpenHubProjectEntity> root = criteriaQuery.from(getEntityClass());
    criteriaQuery.select(root);/*from   w ww  .ja v a  2 s .  c o  m*/

    Predicate predicate = criteriaBuilder.equal(root.get("name"), name);
    criteriaQuery.where(predicate);

    TypedQuery<OpenHubProjectEntity> query = getEntityManager().createQuery(criteriaQuery);

    OpenHubProjectEntity projectEntity = query.getSingleResult();

    return projectEntity;
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Long waitingShippingCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);/*www  .  ja  va 2s.  c o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled),
            criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.paid),
            criteriaBuilder.equal(root.get("shippingStatus"), ShippingStatus.unshipped));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Long waitingPaymentCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);//from w  ww. j a  va  2  s. com
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled));
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.or(criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.unpaid),
                    criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.partialPayment)));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Integer getSalesVolume(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer>get("shippedQuantity")));
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed));
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }//from  w  ww  . j  a  v a  2  s  . com
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public BigDecimal getSalesAmount(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amountPaid")));
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed));
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }//from ww w .  jav  a  2 s.  c  om
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Page<Order> findPage(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus,
        Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);//from   w  w  w .  ja  v a 2 s.  c om
    Predicate restrictions = criteriaBuilder.conjunction();
    if (orderStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("orderStatus"), orderStatus));
    }
    if (paymentStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus));
    }
    if (shippingStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Long count(OrderStatus orderStatus, PaymentStatus paymentStatus, ShippingStatus shippingStatus,
        Boolean hasExpired) {//from  ww w .  ja v a2  s.  c  o m
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    if (orderStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("orderStatus"), orderStatus));
    }
    if (paymentStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("paymentStatus"), paymentStatus));
    }
    if (shippingStatus != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("shippingStatus"), shippingStatus));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("expire"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:com.carser.viamais.vo.TransactionFilter.java

public Long count(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.select(cb.countDistinct((transaction)));
    cq.where(getPredicates(cb, transaction));
    return em.createQuery(cq).getSingleResult();
}