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:org.verinice.persistence.VeriniceAccountDaoImpl.java

private TypedQuery<Entity> buildQueryForProperties(String loginName) {

    CriteriaBuilder builder = getCriteriaBuilder();

    CriteriaQuery<Entity> query = builder.createQuery(Entity.class);
    Root<Entity> entity = query.from(Entity.class);
    Join<PropertyList, Entity> propertyListJoin = entity.join("propertyLists");
    Join<PropertyList, Property> propertiesJoin = propertyListJoin.join("properties");
    List<Predicate> conditions = new ArrayList<>();
    Path<String> propertytypePath = propertiesJoin.get("propertytype");
    conditions.add(builder.like(propertytypePath, "configuration_benutzername"));
    Path<String> propertyvaluePath = propertiesJoin.get("propertyvalue");
    conditions.add(builder.like(propertyvaluePath, loginName));
    query.where(conditions.toArray(new Predicate[conditions.size()]));

    return entityManager.createQuery(query);
}

From source file:br.nom.abdon.gastoso.dal.FatosDao.java

public List<Fato> listar(final EntityManager em, final FiltroFatos filtroFatos) {

    final CriteriaBuilder cb = em.getCriteriaBuilder();

    final CriteriaQuery<Fato> q = cb.createQuery(Fato.class);

    final Root<Fato> root = q.from(Fato.class);

    final List<Predicate> where = new LinkedList<>();
    final Map<String, Object> params = new HashMap<>();

    buildQuery(cb, filtroFatos, root, where, params);

    trataOrdenacao(filtroFatos, root, cb, q);

    return DalUtil.prepareAndRunQuery(em, q, where, params, filtroFatos.getPaginacao());
}

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

/**
 * Find by id.//ww  w. j a v  a 2 s  . c  o  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:org.apereo.lap.dao.RiskConfidenceRepositoryImpl.java

public List<RiskConfidence> findByUserCourseDate(final String user, final String course) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<RiskConfidence> criteria = builder.createQuery(RiskConfidence.class);
    Root<RiskConfidence> root = criteria.from(RiskConfidence.class);
    EntityType<RiskConfidence> type = entityManager.getMetamodel().entity(RiskConfidence.class);
    criteria.orderBy(builder.desc(root.get("dateCreated")));

    List<RiskConfidence> lastRiskConfidences = entityManager.createQuery(criteria).setFirstResult(0)
            .setMaxResults(1).getResultList();

    if (lastRiskConfidences == null || lastRiskConfidences.isEmpty()) {
        logger.warn("No risk confidence records found");
        return new ArrayList<RiskConfidence>();
    }/*from w  ww. j  av  a  2  s.c o m*/

    RiskConfidence lastRickConfidence = lastRiskConfidences.get(0);

    builder = entityManager.getCriteriaBuilder();
    criteria = builder.createQuery(RiskConfidence.class);
    root = criteria.from(RiskConfidence.class);
    type = entityManager.getMetamodel().entity(RiskConfidence.class);

    Predicate groupPredicate = builder.equal(root.get("groupId"), lastRickConfidence.getGroupId());

    if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(course)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(
                                root.get(type.getDeclaredSingularAttribute("alternativeId", String.class))),
                        user.toLowerCase()),
                builder.equal(
                        builder.lower(root.get(type.getDeclaredSingularAttribute("courseId", String.class))),
                        course.toLowerCase()));
    } else if (!StringUtils.isEmpty(user)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(
                                root.get(type.getDeclaredSingularAttribute("alternativeId", String.class))),
                        user.toLowerCase()));
    } else if (!StringUtils.isEmpty(course)) {
        criteria.where(groupPredicate,
                builder.equal(
                        builder.lower(root.get(type.getDeclaredSingularAttribute("courseId", String.class))),
                        course.toLowerCase()));
    } else {
        criteria.where(groupPredicate);
    }

    return entityManager.createQuery(criteria).getResultList();
}

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

@Override
public Categorie getByTitle(String title) {
    if (title == null) {
        throw new NullPointerException();
    }/*  w  ww.ja v  a 2s.  c o 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:eu.uqasar.service.ProductService.java

public List<Product> getAllByDescendingNameFiltered(ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> root = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*ww  w.  j  ava 2  s. c  o  m*/

    criteria.orderBy(cb.desc(root.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:org.jboss.spring3_2.example.MatrixVariables.repo.MemberDaoImpl.java

public List<Member> findByNameAndEmail(String name, String email) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    EntityType<Member> type = em.getMetamodel().entity(Member.class);
    Root<Member> member = criteria.from(Member.class);
    if (name != null && !name.isEmpty() && name != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("name", String.class)),
                "%" + name + "%"));
    if (email != null && !email.isEmpty() && email != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("email", String.class)),
                "%" + email + "%"));
    return em.createQuery(criteria).getResultList();
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> getAllByAscendingNameFiltered(
        eu.uqasar.web.pages.products.panels.ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from  w  w  w .j  av  a2s  .c  o m

    criteria.orderBy(cb.asc(from.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

public Page<Member> findPage(Member.RankingType rankingType, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Member> criteriaQuery = criteriaBuilder.createQuery(Member.class);
    Root<Member> root = criteriaQuery.from(Member.class);
    criteriaQuery.select(root);/*from  www.  ja v  a  2s . c o m*/
    if (rankingType != null) {
        switch (rankingType) {
        case point:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("point")));
            break;
        case balance:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("balance")));
            break;
        case amount:
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("amount")));
            break;
        }
    }
    return super.findPage(criteriaQuery, pageable);
}

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

/**
 * {@inheritDoc}/*from  w w  w .  j av  a  2s . com*/
 */
public List<User> getCustomers() {

    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));
    return entityManager.createQuery(query).getResultList();
}