Example usage for javax.persistence.criteria CriteriaQuery from

List of usage examples for javax.persistence.criteria CriteriaQuery from

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery from.

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param uuid//from   w ww. j  ava2s .  co m
 *            the id of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookById(final String uuid) {
    BookType m = null;
    if (uuid == null) {
        this.logger.debug("getBookById: UUID is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(EntityType_.UUID), uuid));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for id=" + uuid);
            m = null;
        }
    }
    return m;
}

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

@Override
public Collection<Report> getByUser(User u) throws NullPointerException, IllegalArgumentException {
    if (u == null) {
        throw new NullPointerException();
    }/*from  ww  w .ja v  a 2 s .  c  o m*/
    if (u.isNew()) {
        throw new IllegalArgumentException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.author), u));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("Found {} Reports by {}.", result.size(), u);
    return result;
}

From source file:net.shopxx.dao.impl.MemberDaoImpl.java

public Long registerMemberCount(Date beginDate, Date endDate) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class);
    Root<Member> root = criteriaQuery.from(Member.class);
    criteriaQuery.select(root);//w  ww .  j a v a2s. co  m
    Predicate restrictions = criteriaBuilder.conjunction();
    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);
    return super.count(criteriaQuery, null);
}

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

/**
 * Test find filter statement.//from   ww w . j av  a  2  s . c  o  m
 */
@Test
public void testFindFilterStatement() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statement> query = builder.createQuery(Statement.class);
    Root<Statement> root = query.from(Statement.class);
    query.select(root);
    int limit = 4 - 2;
    List<Statement> statementList = entityManager.createQuery(query).setFirstResult(2).setMaxResults(limit)
            .getResultList();

    assertEquals(2, statementList.size());
}

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

/**
 * Test filtering./*from w w w . j a  v  a 2s .  com*/
 */
@Test
public void testFiltering() {

    String cardNumber = "0008";
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Statement> query = builder.createQuery(Statement.class);
    Root<Statement> root = query.from(Statement.class);
    query.select(root);
    query.where(builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER), "%" + cardNumber));
    List<Statement> statementList = entityManager.createQuery(query).getResultList();

    assertEquals(4, statementList.size());
}

From source file:com.devicehive.dao.rdbms.DeviceClassDaoRdbmsImpl.java

@Override
public List<DeviceClassWithEquipmentVO> list(String name, String namePattern, String sortField,
        Boolean sortOrderAsc, Integer take, Integer skip) {
    final CriteriaBuilder cb = criteriaBuilder();
    final CriteriaQuery<DeviceClass> criteria = cb.createQuery(DeviceClass.class);
    final Root<DeviceClass> from = criteria.from(DeviceClass.class);

    final Predicate[] predicates = CriteriaHelper.deviceClassListPredicates(cb, from, ofNullable(name),
            ofNullable(namePattern));//w ww .j av a  2  s.  c  o  m
    criteria.where(predicates);
    CriteriaHelper.order(cb, criteria, from, ofNullable(sortField), Boolean.TRUE.equals(sortOrderAsc));

    final TypedQuery<DeviceClass> query = createQuery(criteria);
    ofNullable(take).ifPresent(query::setMaxResults);
    ofNullable(skip).ifPresent(query::setFirstResult);

    CacheHelper.cacheable(query);
    List<DeviceClass> resultList = query.getResultList();
    Stream<DeviceClassWithEquipmentVO> objectStream = resultList.stream().map(DeviceClass::convertToVo);
    return objectStream.collect(Collectors.toList());
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public List<T> findRange(int[] range) {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);// ww w .  j  a v  a  2s .  co  m
    return q.getResultList();
}

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

@Override
public Collection<Report> getByObservation(Observation o)
        throws NullPointerException, IllegalArgumentException {
    if (o == null) {
        throw new NullPointerException();
    }//w  ww  .  jav  a  2s  .co m
    if (o.isNew()) {
        throw new IllegalArgumentException();
    }
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Report> cq = cb.createQuery(Report.class);
    Root<Report> r = cq.from(Report.class);
    cq.select(r).where(cb.equal(r.get(Report_.observation), o));
    Collection<Report> result = getEntityManager().createQuery(cq).getResultList();
    logger.info("{} Reports for Observation {}.", result.size(), o);
    return result;
}

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

@Override
public Collection<User> getAll() {
    CriteriaQuery<User> cq = getEntityManager().getCriteriaBuilder().createQuery(User.class);
    cq.select(cq.from(User.class));
    Collection<User> list = getEntityManager().createQuery(cq).getResultList();
    logger.info("Getting all Users; Found: {}", list.size());
    return list;// ww  w  . ja va 2s. co  m
}

From source file:org.verinice.persistence.CnaTreeElementDaoImpl.java

private TypedQuery<CnaTreeElement> createQueryForScopeKeyValue(Integer scopeId, String key, String value) {
    CriteriaQuery<CnaTreeElement> query = getCriteriaBuilder().createQuery(CnaTreeElement.class);
    Root<CnaTreeElement> rootelement = query.from(CnaTreeElement.class);
    query.select(rootelement);//w  ww .j a v a  2s . c om

    Join<CnaTreeElement, Entity> entityJoin = rootelement.join("entity", JoinType.LEFT);
    Join<PropertyList, Entity> propertyListJoin = entityJoin.join("propertyLists", JoinType.LEFT);
    Join<PropertyList, Property> propertyJoin = propertyListJoin.join("properties", JoinType.LEFT);

    List<Predicate> conditions = new ArrayList<>();
    if (key != null) {
        conditions.add(getCriteriaBuilder().like(propertyJoin.get("propertytype"), key));
    }
    if (value != null) {
        conditions.add(getCriteriaBuilder().like(propertyJoin.get("propertyvalue"), value));
    }
    if (scopeId != null) {
        conditions.add(getCriteriaBuilder().equal(rootelement.get("scopeId"), scopeId));
    }
    query.where(conditions.toArray(new Predicate[conditions.size()]));

    query.distinct(true);
    return entityManager.createQuery(query);
}