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.expressui.sample.dao.query.RelatedContactsQuery.java

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

    if (hasValue(account)) {
        ParameterExpression<Account> accountExp = builder.parameter(Account.class, "account");
        predicates.add(builder.equal(contact.get("account"), accountExp));
    }//ww  w  .j  av  a2  s  .  com

    return predicates;
}

From source file:cz.muni.expense.data.UserRepository.java

public User findByUsernameAndPassword(String username, String password) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> user = criteria.from(User.class);
    // Swap criteria statements if you would like to try out type-safe criteria queries, a new
    // feature in JPA 2.0
    //criteria.select(member).where(cb.equal(member.get(Member_.name), email));
    criteria.select(user).where(cb.equal(user.get(User_.username), username));
    try {/*from   w  w w .  j  a  v a 2s  .c  o  m*/
        User userToReturn = em.createQuery(criteria).getSingleResult();
        String hash = org.apache.commons.codec.digest.DigestUtils.sha256Hex(password);
        return userToReturn.getPasswdHash().equals(hash) ? userToReturn : null;
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * {@inheritDoc}/*from w ww . j  av a 2  s.  co m*/
 */
public User findLastCustomer() {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.select(root);
    query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER));
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last customer", e);
    }
    return null;
}

From source file:com.aimdek.ccm.dao.impl.StatementRepositoryImpl.java

/**
 * Adds the sorting.//from  w ww .j  ava 2s.c o  m
 *
 * @param sortField
 *            the sort field
 * @param sortOrder
 *            the sort order
 * @param query
 *            the query
 * @param builder
 *            the builder
 * @param root
 *            the root
 */
private void addSorting(String sortField, String sortOrder, CriteriaQuery query, CriteriaBuilder builder,
        Root<Statement> root) {
    if (CommonUtil.isNotNull(sortField)) {
        if (sortOrder.startsWith(SORT_ORDER_ASCENDING)) {
            query.orderBy(builder.asc(root.get(sortField)));
        } else {
            query.orderBy(builder.desc(root.get(sortField)));
        }
    }
}

From source file:com.expressui.sample.dao.query.RelatedOpportunitiesQuery.java

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

    if (hasValue(account)) {
        ParameterExpression<Account> accountExp = builder.parameter(Account.class, "account");
        predicates.add(builder.equal(opportunity.get("account"), accountExp));
    }/*from w ww  . j av  a 2  s .c o m*/

    return predicates;
}

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

public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters,
        List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Promotion> criteriaQuery = criteriaBuilder.createQuery(Promotion.class);
    Root<Promotion> root = criteriaQuery.from(Promotion.class);
    criteriaQuery.select(root);//w w w  .j  ava  2 s . c om
    Predicate restrictions = criteriaBuilder.conjunction();
    if (hasBegun != null) {
        if (hasBegun) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("beginDate").isNull(),
                    criteriaBuilder.lessThanOrEqualTo(root.<Date>get("beginDate"), new Date())));
        } else {
            restrictions = criteriaBuilder.and(restrictions, root.get("beginDate").isNotNull(),
                    criteriaBuilder.greaterThan(root.<Date>get("beginDate"), new Date()));
        }
    }
    if (hasEnded != null) {
        if (hasEnded) {
            restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findList(criteriaQuery, null, count, filters, orders);
}

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

public Page<Order> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Order>(Collections.<Order>emptyList(), 0, pageable);
    }/*from   ww w  .  ja  v  a  2 s  .com*/
    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.findPage(criteriaQuery, pageable);
}

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 ww w . j  a va2 s  .co  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:ca.uhn.fhir.jpa.dao.BaseHapiFhirSystemDao.java

@Override
public Map<String, Long> getResourceCounts() {
    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = builder.createTupleQuery();
    Root<?> from = cq.from(ResourceTable.class);
    cq.multiselect(from.get("myResourceType").as(String.class),
            builder.count(from.get("myResourceType")).as(Long.class));
    cq.groupBy(from.get("myResourceType"));

    TypedQuery<Tuple> q = myEntityManager.createQuery(cq);

    Map<String, Long> retVal = new HashMap<String, Long>();
    for (Tuple next : q.getResultList()) {
        String resourceName = next.get(0, String.class);
        Long count = next.get(1, Long.class);
        retVal.put(resourceName, count);
    }/*from w  w  w.  ja  va 2 s  .  c  o m*/
    return retVal;
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute// w ww  .ja  v a  2 s . co m
public List<T> findByAttribute(String attributeName, Object attributeValue, long first, long count,
        String... fg) {
    LOGGER.debug("Searching {} bu attribute {} : {}, fg {}",
            new Object[] { persistentClass, attributeName, attributeValue, fg });

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(cb.equal(p.get(attributeName), attributeValue));

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;

}