Example usage for javax.persistence.criteria Root get

List of usage examples for javax.persistence.criteria Root get

Introduction

In this page you can find the example usage for javax.persistence.criteria Root get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

From source file:com.wms.studio.service.LoginIpServiceImpl.java

@Override
@org.springframework.transaction.annotation.Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public PageDto<LoginIpInfoDto> findBy(final String userId, final Date start, final Date end,
        PageSize pageSize) {/*from   ww w.  j  a  va 2  s  .  co  m*/

    if (pageSize == null) {
        pageSize = new PageSize();
    }

    return loginIpConvert.covertToDto(loginIpRepository.findAll(new Specification<LoginIp>() {

        @Override
        public Predicate toPredicate(Root<LoginIp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> pres = new ArrayList<>(3);

            if (start != null) {
                pres.add(cb.greaterThanOrEqualTo(root.get("loginTime").as(Date.class), start));
            }

            if (end != null) {
                pres.add(cb.lessThanOrEqualTo(root.get("loginTime").as(Date.class), end));
            }

            if (!StringUtils.isBlank(userId)) {
                pres.add(cb.equal(root.get("user").as(User.class), new User(userId)));
            }

            Predicate[] p = new Predicate[pres.size()];
            return cb.and(pres.toArray(p));
        }
    }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit())));
}

From source file:org.oncoblocks.centromere.jpa.CentromereJpaRepository.java

/**
 * Returns a unsorted list of distinct values of the requested field, filtered using a {@link QueryCriteria}
 * based query./*  ww w  .j  ava 2 s.c o m*/
 *
 * @param field          Model field name.
 * @param queryCriterias Query criteria to filter the field values by.
 * @return Sorted list of distinct values of {@code field}.
 */
public Iterable<Object> distinct(String field, Iterable<QueryCriteria> queryCriterias) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(metadata.getJavaType());
    Root<T> root = query.from(metadata.getJavaType());
    query.where(queryBuilder.createSpecification(queryCriterias).toPredicate(root, query, builder));
    query.select(root.get(field)).distinct(true);
    return (List) entityManager.createQuery(query).getResultList();
}

From source file:org.osiam.resource_server.storage.dao.ResourceDao.java

private <T extends ResourceEntity> long getTotalResults(Class<T> clazz, Subquery<Long> internalIdQuery) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> resourceQuery = cb.createQuery(Long.class);
    Root<T> resourceRoot = resourceQuery.from(clazz);

    resourceQuery.select(cb.count(resourceRoot))
            .where(cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery));

    Long total = em.createQuery(resourceQuery).getSingleResult();

    return total;
}

From source file:com.sapito.db.dao.AbstractDao.java

public List<T> findBySpecificField(String field, Object fieldContent, String predicates,
        LinkedHashMap<String, String> ordering, LinkedList<String> grouping) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery();
    Root<T> root = cq.from(entityClass);
    Predicate predicate = null;// www .  ja v a 2 s. c  o  m

    if (predicates.equals("equal")) {
        predicate = cb.equal(root.get(field), fieldContent);
    } else if (predicates.equals("likelower")) {
        predicate = cb.like(cb.lower(root.<String>get(field)), fieldContent.toString());
    } else if (predicates.equals("like")) {
        predicate = cb.like(root.<String>get(field), "%" + fieldContent.toString() + "%");
    }

    cq.select(root);
    cq.where(predicate);

    if (ordering != null) {
        Set<String> set = ordering.keySet();
        List<Order> orders = new ArrayList<>();
        for (String orderingField : set) {
            Order order;
            if (ordering.get(orderingField).equals("ASC")) {
                order = cb.asc(root.get(orderingField));
            } else {
                order = cb.desc(root.get(orderingField));
            }
            orders.add(order);
        }
        cq.orderBy(orders);
    }

    if (grouping != null) {
        Iterator iterator = grouping.iterator();
        List<Expression> groups = new LinkedList<>();
        while (iterator.hasNext()) {
            groups.add(root.get(iterator.next().toString()));
        }
        cq.groupBy(groups);
    }

    Query query = entityManager.createQuery(cq);
    query.setMaxResults(MAX_RECORDS_RETURNED);

    return query.getResultList();
}

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

public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class);
    Root<CouponCode> root = criteriaQuery.from(CouponCode.class);
    criteriaQuery.select(root);/*from ww w.  j  a  va 2s.c  o m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    Path<Coupon> couponPath = root.get("coupon");
    if (coupon != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon));
    }
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    if (hasBegun != null) {
        if (hasBegun) {
            restrictions = criteriaBuilder.and(restrictions,
                    criteriaBuilder.or(couponPath.get("beginDate").isNull(),
                            criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date())));
        } else {
            restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(),
                    criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date()));
        }
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(couponPath.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions,
                    criteriaBuilder.or(couponPath.get("endDate").isNull(),
                            criteriaBuilder.greaterThanOrEqualTo(couponPath.<Date>get("endDate"), new Date())));
        }
    }
    if (isUsed != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:se.kth.csc.persist.JPAStore.java

@Override
public Account fetchNewestAccount() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Account> q = cb.createQuery(Account.class);

    Root<Account> account = q.from(Account.class);

    List<Account> candidates = entityManager
            .createQuery(q.select(account).orderBy(cb.desc(account.get(Account_.id)))).setMaxResults(1)
            .getResultList();// www .  j a va2s  . c  o  m

    if (candidates.isEmpty()) {
        return null;
    } else {
        return candidates.get(0);
    }
}

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

public Page<Article> findPage(ArticleCategory articleCategory, List<Tag> tags, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);//from   ww w.  jav a2  s  .  c  o m
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true));
    if (articleCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory),
                        criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"),
                                "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                        + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (tags != null && !tags.isEmpty()) {
        Subquery<Article> subquery = criteriaQuery.subquery(Article.class);
        Root<Article> subqueryRoot = subquery.from(Article.class);
        subquery.select(subqueryRoot);
        subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findPage(criteriaQuery, pageable);
}

From source file:com.expressui.core.dao.security.query.RelatedPermissionsQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Permission> query,
        Root<Permission> permission) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(role)) {
        ParameterExpression<Role> roleExp = builder.parameter(Role.class, "role");
        predicates.add(builder.equal(permission.get("role"), roleExp));
    }/*  w  ww .  j  a v a  2 s  . com*/

    return predicates;
}

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

public List<Article> findList(ArticleCategory articleCategory, List<Tag> tags, Integer count,
        List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);// w  w w . j  a  v a 2  s. co  m
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true));
    if (articleCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory),
                        criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"),
                                "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                        + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (tags != null && !tags.isEmpty()) {
        Subquery<Article> subquery = criteriaQuery.subquery(Article.class);
        Root<Article> subqueryRoot = subquery.from(Article.class);
        subquery.select(subqueryRoot);
        subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, null, count, filters, orders);
}

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

public List<Article> findList(ArticleCategory articleCategory, Date beginDate, Date endDate, Integer first,
        Integer count) {/*from www .  j  ava 2s.c  o m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class);
    Root<Article> root = criteriaQuery.from(Article.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true));
    if (articleCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory),
                        criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"),
                                "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                                        + ArticleCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, first, count, null, null);
}