Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:org.businessmanager.dao.GenericDaoImpl.java

public List<T> findByAssignedEntity(SetAttribute<T, ?> setAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(setAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);//from   w ww.j  a  v  a2s  . c  o  m

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getByCompany(final Company company) {
    logger.infof("loading User with company %s ...", company);
    User entity = null;/*  w  ww .  ja v a  2 s  . com*/
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.equal(root.get(User_.company), company));
    return em.createQuery(query).getResultList();
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

@VisibleForTesting
int getNbEntriesBeforeSlicing(EntityManager entityManager, Class<E> entityClass,
        CriteriaBuilder criteriaBuilder, Predicate[] predicates) {

    CriteriaQuery<Long> counterQuery = criteriaBuilder.createQuery(Long.class);

    CriteriaQuery<Long> select = counterQuery.select(criteriaBuilder.count(counterQuery.from(entityClass)));

    if (predicates.length > 0) {
        select.where(predicates);/*from   w ww .  j a  v  a2s.  com*/
    }

    return entityManager.createQuery(counterQuery).getSingleResult().intValue();
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllByAscendingName(int first, int count) {
    logger.infof("loading all Users ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> root = criteria.from(User.class);
    criteria.orderBy(cb.asc(root.get(User_.lastName)), cb.asc(root.get(User_.firstName)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:eu.uqasar.service.user.UserService.java

public boolean ldapBasedUserExists(final LdapUser user) {
    logger.infof("looking for users with username %s or mail %s", user.getUserName(), user.getMail());
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate userName = cb.equal(root.get(User_.userName), user.getUserName());
    Predicate mail = cb.equal(root.get(User_.mail), user.getMail());
    query.where(cb.or(userName, mail));/*from  www  .j  a  v a  2  s .c o  m*/
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    return !resultList.isEmpty();
}

From source file:eu.uqasar.service.user.UserService.java

public User getLdapBasedUser(final LdapUser user) {
    logger.infof("looking for LDAP users with username %s or mail %s", user.getUserName(), user.getMail());
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate userName = cb.equal(root.get(User_.userName), user.getUserName());
    Predicate mail = cb.equal(root.get(User_.mail), user.getMail());
    query.where(cb.and(cb.or(userName, mail)));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    return resultList.isEmpty() ? null : resultList.get(0);
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllExceptOne(User user) {
    if (user == null) {
        return this.getAll();
    }/*from w  ww .j  a  v a2s  .  c o  m*/
    List<User> users;
    logger.infof("loading all Users except %s ...", user);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.notEqual(root.get(User_.id), user.getId()));
    return em.createQuery(query).getResultList();
}

From source file:org.openmeetings.app.data.basic.Configurationmanagement.java

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {//w w  w. ja  va  2s  .c  om
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
        Root<Configuration> c = cq.from(Configuration.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Configuration> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Configuration> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getConfigurations]", ex2);
    }
    return null;
}

From source file:eu.uqasar.service.user.UserService.java

public User getByUserName(final String userName) {
    logger.infof("loading User with username %s ...", userName);
    User entity = null;//from  w w w.ja va2s .com
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.equal(root.get(User_.userName), userName));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }
    return entity;
}

From source file:eu.uqasar.service.user.UserService.java

public User getByMail(final String mail) {
    logger.infof("loading User with mail %s ...", mail);
    User entity = null;/*from ww  w  . j av  a 2 s .  c om*/
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.equal(root.get(User_.mail), mail));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }
    return entity;
}