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:net.groupbuy.dao.impl.ProductCategoryDaoImpl.java

public List<ProductCategory> findParents(ProductCategory productCategory, Integer count) {
    if (productCategory == null || productCategory.getParent() == null) {
        return Collections.<ProductCategory>emptyList();
    }/*from ww  w.j a va  2  s .c  o m*/
    String jpql = "select productCategory from ProductCategory productCategory where productCategory.id in (:ids) order by productCategory.grade asc";
    TypedQuery<ProductCategory> query = entityManager.createQuery(jpql, ProductCategory.class)
            .setFlushMode(FlushModeType.COMMIT).setParameter("ids", productCategory.getTreePaths());
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute/*from  w w w  . ja  v  a 2 s  .  c om*/
public List<T> findByAttribute(String attributeName, Object attributeValue, long first, long count,
        String... fg) {
    LOGGER.debug("Searching {} bu attribute {} : {}, fg {}",
            new Object[] { persistentClass, attributeName, attributeValue, fg });

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(cb.equal(p.get(attributeName), attributeValue));

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;

}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute/*from   w ww  . jav a  2 s. co  m*/
public List<T> findByExample(final T example, final T example2, final OrderBy<T> sortParam, final long first,
        final long count, final String... fg) {
    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    OpenJPACriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> from = q.from(persistentClass);
    q.where(newQbePredicates(cb, from, example, example2));
    if (sortParam != null) {
        List<Order> orderList = sortParam.orders(cb, from);
        if (orderList != null && !orderList.isEmpty()) {
            q.orderBy(orderList);
        }
    }

    TypedQuery<T> query = entityManager.createQuery(q);
    OrmUtils.firstCount(query, first, count);

    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }
    return resultList;
}

From source file:org.openmeetings.app.data.flvrecord.FlvRecordingMetaDataDaoImpl.java

public List<FlvRecordingMetaData> getFlvRecordingMetaDataAudioFlvsByRecording(Long flvRecordingId) {
    try {//  w  w w . j ava 2 s .co m

        String hql = "SELECT c FROM FlvRecordingMetaData c "
                + "WHERE c.flvRecording.flvRecordingId = :flvRecordingId " + "AND ("
                + "(c.isScreenData = false) " + " AND "
                + "(c.isAudioOnly = true OR (c.isAudioOnly = false AND c.isVideoOnly = false))" + ")";

        TypedQuery<FlvRecordingMetaData> query = em.createQuery(hql, FlvRecordingMetaData.class);
        query.setParameter("flvRecordingId", flvRecordingId);

        List<FlvRecordingMetaData> flvRecordingMetaDatas = query.getResultList();

        return flvRecordingMetaDatas;

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

From source file:org.openmeetings.app.data.flvrecord.FlvRecordingMetaDataDaoImpl.java

public FlvRecordingMetaData getFlvRecordingMetaDataScreenFlvByRecording(Long flvRecordingId) {
    try {/* ww w  .  ja v a 2 s . c om*/

        String hql = "SELECT c FROM FlvRecordingMetaData c "
                + "WHERE c.flvRecording.flvRecordingId = :flvRecordingId " + "AND c.isScreenData = true";

        TypedQuery<FlvRecordingMetaData> query = em.createQuery(hql, FlvRecordingMetaData.class);
        query.setParameter("flvRecordingId", flvRecordingId);

        List<FlvRecordingMetaData> flvRecordingMetaDatas = query.getResultList();

        if (flvRecordingMetaDatas.size() > 0) {
            return flvRecordingMetaDatas.get(0);
        }

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

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

private void showResult(CriteriaQuery c) {
    TypedQuery query = em.createQuery(c);

    // result//w  w w  .j  a va  2  s .c  o  m
    List result = query.getResultList();

    // sql string
    String sql = query.unwrap(org.hibernate.Query.class).getQueryString();

    ResultViewer.showResult(result, sql);
}

From source file:com.pingdu.dao.licenseDao.LicenseTypeDao.java

public List<LicenseTypeReturn> LicenseTypeSpecList(String searchType, String keyword) {
    try {//from  ww  w  .  j  av a  2  s.  co m
        // ??
        String jpql = LicenseTypeSpecSQL(searchType, keyword);
        TypedQuery<LicenseTypeReturn> query = em().createQuery(jpql, LicenseTypeReturn.class);
        query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
        List<LicenseTypeReturn> LicenseTypeSpecList = query.getResultList();
        System.out.println(" ??sql??");
        return LicenseTypeSpecList;

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(" ??sql?" + e.getMessage());
        return null;
    }
}

From source file:com.silverpeas.notification.delayed.repository.DelayedNotificationRepositoryImpl.java

@Override
public List<DelayedNotificationData> findDelayedNotification(
        final DelayedNotificationData delayedNotification) {

    // Parameters
    final List<TypedParameter<?>> parameters = new ArrayList<TypedParameter<?>>();

    // Query//  w w  w.j  a  v  a2  s . c  om
    final StringBuilder query = new StringBuilder("from DelayedNotificationData where");
    query.append(" userId = :");
    query.append(TypedParameterUtil.addNamedParameter(parameters, "userId", delayedNotification.getUserId()));
    query.append(" and fromUserId = :");
    query.append(TypedParameterUtil.addNamedParameter(parameters, "fromUserId",
            delayedNotification.getFromUserId()));
    query.append(" and channel = :");
    query.append(TypedParameterUtil.addNamedParameter(parameters, "channel",
            delayedNotification.getChannel().getId()));
    query.append(" and action = :");
    query.append(TypedParameterUtil.addNamedParameter(parameters, "action",
            delayedNotification.getAction().getId()));
    query.append(" and language = :");
    query.append(
            TypedParameterUtil.addNamedParameter(parameters, "language", delayedNotification.getLanguage()));
    Date date = delayedNotification.getCreationDate();
    if (date == null) {
        date = new Date();
    }
    query.append(" and creationDate between :");
    query.append(TypedParameterUtil.addNamedParameter(parameters, "creationDateMin",
            DateUtils.setSeconds(DateUtils.setMilliseconds(date, 0), 0), TemporalType.TIMESTAMP));
    query.append(" and :");
    query.append(TypedParameterUtil.addNamedParameter(parameters, "creationDateMax",
            DateUtils.setSeconds(DateUtils.setMilliseconds(date, 999), 59), TemporalType.TIMESTAMP));
    query.append(" and notificationResourceId = :");
    query.append(
            TypedParameterUtil.addNamedParameter(parameters, "resourceId", delayedNotification.getResource()));

    // resourceDescription parameter
    if (StringUtils.isNotBlank(delayedNotification.getMessage())) {
        query.append(" and message = :");
        query.append(
                TypedParameterUtil.addNamedParameter(parameters, "message", delayedNotification.getMessage()));
    } else {
        query.append(" and message is null");
    }

    // Typed query
    final TypedQuery<DelayedNotificationData> typedQuery = em.createQuery(query.toString(),
            DelayedNotificationData.class);

    // Parameters
    TypedParameterUtil.computeNamedParameters(typedQuery, parameters);

    // Result
    return typedQuery.getResultList();
}

From source file:org.bigtester.ate.model.data.dao.ElementInputDataDaoImpl.java

/**
 * Gets the value./*from   w ww  . j av a2 s  .co  m*/
 *
 * @param inputDataID
 *            the input data id
 * @return the value
 */
public String getValue(String inputDataID, String repeatStepName, int iteration)
        throws RepeatTestDataException {
    List<ElementInputData> retVal;
    String sql = "select p from ElementInputData p where repeatStepExternalLoopPath is null and FirstTimeExecution= 'No' and p.stepEIDsetID = :stepEIDsetID and p.repeatStepName=:repeatStepName and p.iteration=:iteration";
    TypedQuery<ElementInputData> query = getDbEM().createQuery(sql, ElementInputData.class);
    query.setParameter("stepEIDsetID", inputDataID);
    query.setParameter("repeatStepName", repeatStepName);
    query.setParameter("iteration", iteration);
    retVal = (List<ElementInputData>) query.getResultList();
    if (retVal.isEmpty()) {
        throw new RepeatTestDataException(ExceptionMessage.MSG_TESTDATA_NOTFOUND,
                ExceptionErrorCode.REPEATTESTDATA_NOTFOUND, repeatStepName, "", iteration);
    } else if (retVal.size() > 1) { // NOPMD
        throw new RepeatTestDataException(ExceptionMessage.MSG_TESTDATA_DUPLICATED,
                ExceptionErrorCode.REPEATTESTDATA_DUPLICATED, repeatStepName, "", iteration);
    } else {
        return retVal.get(0).getDataValue();
    }

}

From source file:ca.uhn.fhir.jpa.dao.BaseHapiFhirSystemDao.java

@Override
public Map<String, Long> getResourceCounts() {
    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = builder.createTupleQuery();
    Root<?> from = cq.from(ResourceTable.class);
    cq.multiselect(from.get("myResourceType").as(String.class),
            builder.count(from.get("myResourceType")).as(Long.class));
    cq.groupBy(from.get("myResourceType"));

    TypedQuery<Tuple> q = myEntityManager.createQuery(cq);

    Map<String, Long> retVal = new HashMap<String, Long>();
    for (Tuple next : q.getResultList()) {
        String resourceName = next.get(0, String.class);
        Long count = next.get(1, Long.class);
        retVal.put(resourceName, count);
    }//  w w  w.j a va  2  s  .  c  om
    return retVal;
}