Example usage for javax.persistence TypedQuery setMaxResults

List of usage examples for javax.persistence TypedQuery setMaxResults

Introduction

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

Prototype

TypedQuery<X> setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

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

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {//from w w  w. jav  a 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:de.taimos.dao.hibernate.EntityDAOHibernate.java

private <T> List<T> findListByQueryLimit(final String query, Class<T> clazz, final int first, final int max,
        final Object... params) {
    final TypedQuery<T> tq = this.entityManager.createQuery(query, clazz);
    for (int i = 0; i < params.length; i++) {
        tq.setParameter(i + 1, params[i]);
    }/*from  w  ww . j  av  a  2  s  .c o m*/
    if (first >= 0) {
        tq.setFirstResult(first);
    }
    if (max >= 0) {
        tq.setMaxResults(max);
    }
    return tq.getResultList();
}

From source file:com.music.dao.PieceDao.java

public List<Piece> getTopPieces(int page, int pageSize) {
    TypedQuery<Piece> query = getEntityManager().createQuery(
            "SELECT piece FROM Piece piece WHERE piece.likes > 0 ORDER BY likes DESC, generationTime DESC",
            Piece.class);
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);

    return query.getResultList();
}

From source file:org.cleverbus.core.common.dao.ExternalCallDaoJpaImpl.java

@Override
@Nullable/*  w  ww  .j a  v  a 2 s  .c  om*/
@SuppressWarnings("unchecked")
public ExternalCall findConfirmation(int interval) {
    // find confirmation that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT c " + "FROM " + ExternalCall.class.getName() + " c "
            + "WHERE c.operationName = :operationName" + "     AND c.state = :state"
            + "     AND c.lastUpdateTimestamp < :lastUpdateTimestamp" + " ORDER BY c.creationTimestamp";

    TypedQuery<ExternalCall> q = em.createQuery(jSql, ExternalCall.class);
    q.setParameter("operationName", ExternalCall.CONFIRM_OPERATION);
    q.setParameter("state", ExternalCallStateEnum.FAILED);
    q.setParameter("lastUpdateTimestamp", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<ExternalCall> extCalls = q.getResultList();

    if (extCalls.isEmpty()) {
        return null;
    } else {
        return extCalls.get(0);
    }
}

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

/**
 * Adaptation of the algorithm defined in the GraphQL specification.
 * <p>//w  ww  .  j  a  v a 2 s .  c  o  m
 * Please look at the following link for more details:
 * https://facebook.github.io/relay/graphql/connections.htm#sec-Pagination-algorithm
 * <p>
 * The opaque cursor that is returned to the client makes use of the entity ID internally.
 */
protected ExtendedConnection createPaginatedConnection(DataFetchingEnvironment environment,
        Class<E> entityClass, Function<Root<E>, Path<? extends Number>> entityId,
        Comparator<E> entityComparator, BiFunction<CriteriaBuilder, Root<E>, List<Predicate[]>> criteria,
        CursorMapper<T, Integer> cursorMapper) {

    Integer first = environment.getArgument(FIRST.getName());
    Integer last = environment.getArgument(LAST.getName());

    Integer after = cursorMapper.getOffsetFromCursor(environment.getArgument(AFTER.getName()));
    Integer before = cursorMapper.getOffsetFromCursor(environment.getArgument(BEFORE.getName()));

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(entityClass);
    Root<E> entityRoot = criteriaQuery.from(entityClass);
    Path<? extends Number> entityIdPath = entityId.apply(entityRoot);

    Predicate cursorPredicate = createCursorPredicate(criteriaBuilder, entityIdPath, after, before);

    int maxResults = applySlicing(criteriaQuery, criteriaBuilder, entityIdPath, first, last);

    CriteriaQuery<E> select = criteriaQuery.select(entityRoot);

    List<Predicate[]> predicates = criteria.apply(criteriaBuilder, entityRoot);

    Predicate[] wherePredicate = buildWherePredicate(predicates, cursorPredicate, criteriaBuilder);

    if (wherePredicate.length > 0) {
        select.where(wherePredicate);
    }

    TypedQuery<E> query = entityManager.createQuery(select);

    if (maxResults > -1) {
        query.setMaxResults(maxResults);
    }

    Stream<E> dataStream = query.getResultList().stream();

    // if last is provided, reverse the stream
    // in order to get results sorted in ascending order based on entities ID
    if (last != null) {
        dataStream = dataStream.sorted(entityComparator);
    }

    Stream<T> data = dataMapping(dataStream);

    ExtendedConnection connection = createRelayConnection(entityManager, entityClass, criteriaBuilder,
            wherePredicate, cursorMapper, data, first, last);

    return connection;
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public List<Message> findMessagesByContent(String substring) {
    Assert.hasText("the substring must not be empty", substring);

    String jSql = "SELECT m " + "     FROM " + Message.class.getName() + " m "
            + " WHERE (m.payload like :substring) ORDER BY m.msgId DESC";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("substring", "%" + substring + "%");
    q.setMaxResults(MAX_MESSAGES_IN_ONE_QUERY);

    return q.getResultList();
}

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

public List<LicenseTypeReturn> getLicenseTypeList(int page) {
    try {/*  w  w  w. j a va 2 s .  c  o m*/
        String jpql = LicenseTypeSQL(page);
        TypedQuery<LicenseTypeReturn> query = em().createQuery(jpql, LicenseTypeReturn.class);
        query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
        int head = (page - 1) * 15;
        query.setFirstResult(head);
        query.setMaxResults(15);
        List<LicenseTypeReturn> LicenseTypeList = query.getResultList();
        System.out.println(" ??sql??");
        return LicenseTypeList;
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("??sql?" + e.getMessage());
        return null;
    }
}

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

public FormLibraryForm getFormLibraryFormByName(final String formName) {
    TypedQuery<FormLibraryForm> q = this.em.createQuery(
            "SELECT form FROM FormLibraryForm form WHERE form.name = :name ORDER BY form.updateDate DESC",
            FormLibraryForm.class);
    q.setMaxResults(1);
    q.setParameter("name", formName);
    List<FormLibraryForm> resultList = q.getResultList();
    return resultList.size() > 0 ? resultList.get(0) : null;
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public List<Message> findProcessingMessages(int interval) {
    final Date startProcessLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.PROCESSING + "'" + "     AND m.startProcessTimestamp < :startTime";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("startTime", new Timestamp(startProcessLimit.getTime()));
    q.setMaxResults(MAX_MESSAGES_IN_ONE_QUERY);
    return q.getResultList();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable//from   w  ww .j a v a 2 s .c  o m
public Message findPartlyFailedMessage(int interval) {
    // find message that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.PARTLY_FAILED + "'" + "     AND m.lastUpdateTimestamp < :lastTime"
            + " ORDER BY m.msgTimestamp";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("lastTime", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<Message> messages = q.getResultList();

    if (messages.isEmpty()) {
        return null;
    } else {
        return messages.get(0);
    }
}