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.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);/*w  w w  .  j  a  va  2s.  c o m*/

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

    return typedQuery.getResultList();
}

From source file:org.openmeetings.app.data.user.dao.UsersDaoImpl.java

public List<Users> getAllUsers() {
    try {/*w  w w .  j a  v a2s.  c  om*/
        // get all non-deleted users
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Users> cq = cb.createQuery(Users.class);
        Root<Users> c = cq.from(Users.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        TypedQuery<Users> q = em.createQuery(cq);
        List<Users> ll = q.getResultList();

        return ll;
    } catch (Exception ex2) {
        log.error("[getAllUsers] ", ex2);
    }
    return null;
}

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

@Override
public List<T> findByAttributes(Map<SingularAttribute<T, ?>, Object> theAttributes) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

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

    List<Predicate> aPredicateList = new ArrayList<Predicate>();
    Iterator<SingularAttribute<T, ?>> anIterator = theAttributes.keySet().iterator();
    while (anIterator.hasNext()) {
        SingularAttribute<T, ?> aKey = anIterator.next();
        Object aValue = theAttributes.get(aKey);
        Predicate aPredicate = queryBuilder.equal(rootQuery.get(aKey), aValue);
        aPredicateList.add(aPredicate);//from ww  w.  j a  v  a2  s  . co  m
    }

    select.where(aPredicateList.toArray(new Predicate[0]));
    TypedQuery<T> typedQuery = entityManager.createQuery(select);

    return typedQuery.getResultList();
}

From source file:com.sfs.captor.controller.ProjectPackageAction.java

/**
 * Load project packages/*  w ww.  j  a  v  a  2  s. co m*/
 */
private void loadList() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ProjectPackage> c = cb.createQuery(ProjectPackage.class);
    Root<ProjectPackage> obj = c.from(ProjectPackage.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.projectPackages = em.createQuery(c).getResultList();
}

From source file:com.eu.evaluation.server.dao.AbstractDAO.java

public boolean isUnique(T entity, String... propertys) {
    if (propertys == null || propertys.length == 0) {
        return true;
    }//from   ww  w.  j a v a2 s . c  o  m
    try {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass);
        Root<T> root = criteriaQuery.from(entityClass);
        Predicate predicate = null;
        for (String property : propertys) {
            if (predicate == null) {
                predicate = cb.equal(root.get(property), PropertyUtils.getProperty(entity, property));
            } else {
                predicate = cb.and(predicate,
                        cb.equal(root.get(property), PropertyUtils.getProperty(entity, property)));
            }
        }

        if (!StringUtils.isBlank(entity.getId())) {
            predicate = cb.and(predicate, cb.notEqual(root.get("id"), entity.getId()));
        }
        criteriaQuery.where(predicate);

        TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);
        List<T> result = typedQuery.getResultList();
        return result.isEmpty();
    } catch (Exception e) {
        e.printStackTrace();
        ReflectionUtils.handleReflectionException(e);
    }
    return false;
}

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;/*from w w  w  .  j ava 2s  . c  o  m*/
    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:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#unlockOperationOnServerStart()
 *//*w  w w  .  ja v a2 s  .co  m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public void unlockOperationOnServerStart() {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("serverName"), this.hostName));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        List<OperationLockControllType> locks = typedQuery.getResultList();
        for (OperationLockControllType lock : locks) {
            this.logger.debug(" remove lock on server start (this {}): {} {}", this.hostName,
                    lock.getServerName(), lock.getLockDate());
            this.em.remove(this.em.merge(lock));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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));// ww w.ja  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);
}