Example usage for javax.persistence TypedQuery getResultList

List of usage examples for javax.persistence TypedQuery getResultList

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getResultList.

Prototype

List<X> getResultList();

Source Link

Document

Execute a SELECT query and return the query results as a typed List.

Usage

From source file:org.mmonti.entitygraph.repository.CustomGenericJpaRepository.java

@Override
protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, Specification<T> spec) {
    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

    Long total = executeCountQuery(getCountQuery(spec));
    List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T>emptyList();

    return new PageImpl<T>(content, pageable, total);
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployeeRepositoryImplementation.java

@Override
public List<Vacancy> getAvailableActiveVacancies(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Integer> list = null;
    List<Vacancy> list1 = null;

    try {/*from w  w  w  .ja  v a2  s .c o  m*/
        em.getTransaction().begin();

        Query query = em.createNativeQuery(
                "select \"ide\" from (select \"UserSkill\".\"employeeId\", \"UserSkill\".\"experience\", \"VacancySkill\".\"vacancyId\" as \"ide\", \"VacancySkill\".\"experience\",  \"UserSkill\".\"allSkillsId\" from \"UserSkill\" join \"VacancySkill\" on \"UserSkill\".\"allSkillsId\" = \"VacancySkill\".\"allSkillsId\" where \"UserSkill\".\"employeeId\" = ? and \"UserSkill\".\"experience\" >= \"VacancySkill\".\"experience\") group by \"employeeId\", \"ide\" having count(\"employeeId\") >= (select count(*) from \"VacancySkill\" where \"VacancySkill\".\"vacancyId\" = \"ide\")");
        query.setParameter(1, employee.getEmployeeUserId());
        list = query.getResultList();
        if (list.isEmpty()) {
            list.add(0);
        }

        TypedQuery<Vacancy> query1 = em.createNamedQuery("Vacancy.findActiveVacanciesByIds", Vacancy.class);
        query1.setParameter("vacancyIdList", list);
        list1 = query1.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list1;
}

From source file:br.ufrgs.inf.dsmoura.repository.model.dao.TypesDAO.java

public List<FunctionalRequirementTypeDTO> getFunctionalRequirementTypeDTOList() {
    TypedQuery<FunctionalRequirementTypeDTO> query = createEntityManager()
            .createQuery("SELECT t FROM FunctionalRequirementTypeDTO t", FunctionalRequirementTypeDTO.class);
    return 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 ww  . j a  v a 2  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:org.openmeetings.app.data.user.dao.UserContactsDaoImpl.java

public List<UserContacts> getUserContacts() {
    try {//from w ww. j a  v  a  2s  .  c o m

        String hql = "select c from UserContacts c ";

        TypedQuery<UserContacts> query = em.createQuery(hql, UserContacts.class);
        List<UserContacts> userContacts = query.getResultList();

        return userContacts;

    } catch (Exception e) {
        log.error("[getUserContacts]", e);
    }
    return null;
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

@Transactional(propagation = Propagation.SUPPORTS)
protected <U> List<U> findList(TypedQuery<U> query) {
    List<U> result = query.getResultList();
    if (result == null) {
        result = new ArrayList<U>();
    }//  ww  w.ja va 2s.  c o m
    return result;
}

From source file:net.triptech.metahive.model.Record.java

/**
 * Find record entries.//from   w  w  w .  j  av  a2 s . co  m
 *
 * @param filter the record filter
 * @param firstResult the first result
 * @param maxResults the max results
 * @return the list
 */
public static List<Record> findRecordEntries(final RecordFilter filter, final List<Definition> definitions,
        final UserRole userRole, final ApplicationContext context, final int firstResult,
        final int maxResults) {

    List<Record> records = new ArrayList<Record>();

    Map<String, Object> variables = new HashMap<String, Object>();

    Definition orderDefinition = null;
    if (filter.getOrderId() != null && filter.getOrderId() > 0) {
        orderDefinition = Definition.findDefinition(filter.getOrderId());
    }

    StringBuilder sql = new StringBuilder("SELECT DISTINCT r FROM Record r");

    if (orderDefinition != null && orderDefinition.getId() != null) {
        sql.append(" LEFT OUTER JOIN r.keyValues as o");
        sql.append(" WITH o.definition.id = :orderDefinitionId");

        variables.put("orderDefinitionId", orderDefinition.getId());
    }

    Map<String, Map<String, Object>> whereParameters = buildWhere(filter);

    if (whereParameters.size() > 0) {
        String sqlWhere = whereParameters.keySet().iterator().next();
        Map<String, Object> whereVariables = whereParameters.get(sqlWhere);

        for (String key : whereVariables.keySet()) {
            variables.put(key, whereVariables.get(key));
        }
        sql.append(sqlWhere);
    }

    String orderValueCol = "r.recordId";
    if (orderDefinition != null && orderDefinition.getId() != null) {
        orderValueCol = "o.doubleValue";
        if (orderDefinition.getDataType() == DataType.TYPE_STRING) {
            orderValueCol = "o.stringValue";
        }
        if (orderDefinition.getDataType() == DataType.TYPE_BOOLEAN) {
            orderValueCol = "o.booleanValue";
        }
    }

    sql.append(" ORDER BY " + orderValueCol);

    if (filter.isOrderDescending()) {
        sql.append(" DESC");
    } else {
        sql.append(" ASC");
    }

    if (orderDefinition != null && orderDefinition.getId() != null) {
        sql.append(", r.recordId ASC");
    }

    logger.info("SQL: " + sql.toString());

    TypedQuery<Record> q = entityManager().createQuery(sql.toString(), Record.class);

    if (maxResults > 0) {
        q.setFirstResult(firstResult).setMaxResults(maxResults);
    }

    for (String variable : variables.keySet()) {
        q.setParameter(variable, variables.get(variable));
    }

    for (Record record : q.getResultList()) {
        record.setKeyValues(KeyValue.findKeyValues(record, definitions), definitions, userRole, context);
        records.add(record);
    }
    return records;
}

From source file:com.healthcit.cacure.dao.FormDao.java

public Collection<FormLibraryForm> getAllLibraryForms() {
    TypedQuery<FormLibraryForm> q = this.em.createQuery("FROM FormLibraryForm form ORDER BY form.ord ASC",
            FormLibraryForm.class);
    return q.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  w  w  w.ja v  a2  s  . co  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:com.olp.jpa.domain.docu.ut.repo.EmployeeRepositoryImpl.java

@Override
@Transactional(readOnly = true)/*from  w w  w  .  jav  a 2s  .c  o  m*/
public List<EmployeeBean> findByDeptCode(String deptCode) {

    IContext ctx = ContextManager.getContext();
    String tid = ctx.getTenantId();

    TypedQuery<EmployeeBean> query = getEntityManager().createNamedQuery("UtEmployeeBean.findByDeptCode",
            EmployeeBean.class);
    query.setParameter("deptCode", deptCode); // TODO: Sanitize input, although low risk due to binding
    query.setParameter("tenant", tid);

    List<EmployeeBean> list = query.getResultList();

    return (list);

}