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.ttech.cordovabuild.domain.application.ApplicationRepositoryImpl.java

@Override
public List<ApplicationBuilt> getApplications(User owner) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ApplicationBuilt> cq = cb.createQuery(ApplicationBuilt.class);
    Root<ApplicationBuilt> from = cq.from(ApplicationBuilt.class);
    return em//from  www.  ja  va2s. co m
            .createQuery(cq.select(from)
                    .where(cb.equal(from.get(ApplicationBuilt_.application).get(Application_.owner), owner)))
            .getResultList();
}

From source file:dtu.ds.warnme.dao.impl.EventsDaoImpl.java

@Override
public List<EventEntity> getNearestEvents(Float nLat, Float sLat, Float eLng, Float wLng,
        EventType... eventTypes) {//from w  w w. ja v  a 2  s. co  m
    CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
    CriteriaQuery<EventEntity> criteriaQuery = criteriaBuilder.createQuery(EventEntity.class);
    Root<EventEntity> root = criteriaQuery.from(EventEntity.class);

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.latitude), nLat));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.latitude), sLat));
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.longitude), eLng));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.longitude), wLng));

    if (ArrayUtils.isNotEmpty(eventTypes)) {
        List<Predicate> eventTypePredicates = new ArrayList<Predicate>();
        for (EventType et : eventTypes) {
            eventTypePredicates.add(criteriaBuilder.equal(root.get(EventEntity_.eventType), et));
        }
        predicates.add(criteriaBuilder.and(
                criteriaBuilder.or(eventTypePredicates.toArray(new Predicate[eventTypePredicates.size()]))));
    }

    criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
    return getAllByCriteria(criteriaQuery);
}

From source file:com.aimdek.ccm.dao.impl.test.BasicAbstractGenericDaoImplTest.java

/**
 * Find by id.//  w  ww.  j a va2  s. co  m
 *
 * @param id
 *            the id
 * @param entityClass
 *            the entity class
 * @return the entity type
 */
public EntityType findById(IDType id, Class<EntityType> entityClass) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityType> query = builder.createQuery(entityClass);
    Root<EntityType> root = query.from(entityClass);
    query.select(root);
    query.where(builder.equal(root.get("id"), id));

    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public boolean isValidUser(String userName, String password) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);

    criteria.select(member).where(builder.equal(member.get("name"), userName))
            .where(builder.equal(member.get("password"), password));
    try {/*from  www. j a  v  a  2 s  .  c  o m*/

        Member validMember = em.createQuery(criteria).setMaxResults(1).getSingleResult();
        return ObjectUtil.verifyMemberExists(validMember);

    } catch (NoResultException e) {
        return false;
    }

}

From source file:org.sloth.persistence.impl.CategorieDaoImpl.java

@Override
public Categorie getByTitle(String title) {
    if (title == null) {
        throw new NullPointerException();
    }/*  w w w.jav  a 2s.co m*/
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Categorie> cq = cb.createQuery(Categorie.class);
    Root<Categorie> categorie = cq.from(Categorie.class);
    cq.select(categorie);
    cq.where(cb.equal(categorie.get(Categorie_.title), title));
    Categorie result = null;
    try {
        result = getEntityManager().createQuery(cq).getSingleResult();
    } catch (NoResultException e) {
        logger.info("Categorie with Title {} not found", title);
    } catch (NonUniqueResultException e) {
        logger.warn("Corrupt Database", e);
    }
    return result;
}

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

/**
 * {@inheritDoc}//from   ww  w . j  a v a  2s . com
 */
public List<User> getCustomers(int start, int limit, String sortField, String sortOrder,
        Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate isCustomerRole = builder.equal(root.get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER);
    Predicate hasCreditCard = builder.equal(root.get(FIELD_CONSTANT_HAS_CREDIT_CARD), TRUE);
    query.where(builder.and(isCustomerRole, hasCreditCard));

    query.select(root);

    addSorting(sortField, sortOrder, query, builder, root);
    addFilterCriteria(filters, builder, root, query);
    return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public Member findMemberByUserName(String userName) {
    try {/*from  w  w  w  .  ja  v a  2  s.c om*/

        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
        Root<Member> member = criteria.from(Member.class);

        criteria.select(member).where(builder.equal(member.get("name"), userName));
        return em.createQuery(criteria).getSingleResult();

    } catch (NoResultException e) {

        return new Member();
    }

}

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

public Page<Receiver> findPage(Member member, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Receiver> criteriaQuery = criteriaBuilder.createQuery(Receiver.class);
    Root<Receiver> root = criteriaQuery.from(Receiver.class);
    criteriaQuery.select(root);//from  w w w  . j  av  a2 s .c  o  m
    if (member != null) {
        criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    }
    return super.findPage(criteriaQuery, pageable);
}

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

/**
 * {@inheritDoc}/*w  w w. j  a  v a  2s.  c  o m*/
 */
public BulkUpload findLastBulkUpload() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BulkUpload> query = builder.createQuery(BulkUpload.class);
    Root<BulkUpload> root = query.from(BulkUpload.class);
    query.select(root);
    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 bulkupload", e);
    }
    return null;
}

From source file:net.dontdrinkandroot.persistence.dao.ExampleGeneratedIdEntityDaoImpl.java

@Override
@Transactional(readOnly = true)//from  w ww .  j av  a  2 s.co  m
public ExampleGeneratedIdEntity findWithOthersFetchJoin(final Long id) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder
            .createQuery(ExampleGeneratedIdEntity.class);
    final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass);
    root.fetch(ExampleGeneratedIdEntity_.otherEntities);

    criteriaQuery.where(builder.equal(root.get(ExampleGeneratedIdEntity_.id), id));

    return this.findSingle(criteriaQuery);
}