Example usage for javax.persistence TypedQuery setFirstResult

List of usage examples for javax.persistence TypedQuery setFirstResult

Introduction

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

Prototype

TypedQuery<X> setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * //  w w w . j  a va  2 s  .  co  m
 * list entity by criteriaInfo
 * 
 * @param criteriaInfo
 * @return 
 */
public PagedResult<T> findAll(CriteriaInfo criteriaInfo) {
    EntityManager em = getEntityManager();

    ParamInfo paramInfo = criteriaInfo.getParamInfo();
    PageInfo pageInfo = criteriaInfo.getPageInfo();
    SortInfo sortInfo = criteriaInfo.getSortInfo();

    StringBuilder from = new StringBuilder();
    from.append(String.format("%s t ", entityClass.getSimpleName()));

    StringBuilder where = new StringBuilder();
    Map<String, Object> paramMap = new HashMap<>();

    where = prepareWhereCondition(paramInfo, paramMap, where);

    StringBuilder orderBy = new StringBuilder(" ");
    if (sortInfo != null) {
        Iterator<SortItem> iterator = sortInfo.getSortItems().iterator();
        while (iterator.hasNext()) {
            SortItem item = iterator.next();
            orderBy.append("t.").append(item.getFieldName()).append(" ")
                    .append(item.isDescending() ? "desc" : "asc").append(",");
        }
        orderBy = new StringBuilder(orderBy.substring(0, orderBy.length() - 1));
        ;
    }

    String query = toSql(where, from, orderBy, false);
    String countquery = toSql(where, from, orderBy, true);

    TypedQuery<T> cq = em.createQuery(query, entityClass);
    //??
    TypedQuery<Long> countq = em.createQuery(countquery, Long.class);

    for (String key : paramMap.keySet()) {
        Object value = paramMap.get(key);
        cq.setParameter(key, value);
        countq.setParameter(key, value);
    }

    Long countResult = countq.getSingleResult();

    if (pageInfo != null) {
        cq.setFirstResult(pageInfo.getOffset());
        cq.setMaxResults(pageInfo.getSize());
    }

    return new PagedResult(cq.getResultList(), countResult == null ? 0 : countResult.intValue());
}

From source file:com.scipub.dao.jpa.Dao.java

protected <T> List<T> findByQuery(QueryDetails<T> details) {
    if (details.getResultClass() == null) {
        throw new IllegalArgumentException("You must specify a result class");
    }//from  w ww .j a va 2  s.  c  om
    TypedQuery<T> q = null;
    if (details.getQueryName() != null) {
        q = entityManager.createNamedQuery(details.getQueryName(), details.getResultClass());
    } else if (details.getQuery() != null) {
        q = entityManager.createQuery(details.getQuery(), details.getResultClass());
    } else {
        throw new IllegalArgumentException("Either query or query name must be set");
    }

    for (int i = 0; i < details.getParamNames().length; i++) {
        q.setParameter(details.getParamNames()[i], details.getParamValues()[i]);
    }
    if (details.getStart() > -1) {
        q.setFirstResult(details.getStart());
    }
    if (details.getCount() > -1) {
        q.setMaxResults(details.getCount());
    }
    if (details.isCacheable()) {
        setCacheable(q);
    }
    return q.getResultList();
}

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

public List<PrivateMessages> getTrashPrivateMessagesByUser(Long user_id, String search, String orderBy,
        int start, Boolean asc, int max) {
    try {//from   ww  w .  java2 s. c  om

        String hql = "select c from PrivateMessages c " + "where c.isTrash = true "
                + "AND c.owner.user_id = :user_id ";

        if (search.length() != 0) {
            hql += "AND ( ";
            hql += "lower(c.subject) LIKE :search ";
            hql += "OR lower(c.message) LIKE :search ";
            hql += "OR lower(c.from.firstname) LIKE :search ";
            hql += "OR lower(c.from.lastname) LIKE :search ";
            hql += "OR lower(c.from.login) LIKE :search ";
            hql += "OR lower(c.from.adresses.email) LIKE :search ";
            hql += " ) ";
        }

        hql += "ORDER BY " + orderBy;

        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        TypedQuery<PrivateMessages> query = em.createQuery(hql, PrivateMessages.class);
        if (search.length() != 0) {
            query.setParameter("search", StringUtils.lowerCase("%" + search + "%"));
        }
        query.setParameter("user_id", user_id);
        query.setFirstResult(start);
        query.setMaxResults(max);
        List<PrivateMessages> ll = query.getResultList();

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

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * /* w w w . java  2s  .  c o m*/
 * @param user_level
 * @param user_id
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
public List<Organisation> getRestOrganisationsByUserId(long user_level, long user_id, int start, int max,
        String orderby, boolean asc) {
    try {
        if (authLevelManagement.checkAdminLevel(user_level)) {
            String qSQL = "SELECT o FROM Organisation AS o " + "WHERE o.organisation_id NOT IN ("
                    + "   SELECT ou.organisation.organisation_id "
                    + "   FROM Users u, IN(u.organisation_users) ou WHERE u.deleted = 'false' AND u.user_id = :user_id)";
            TypedQuery<Organisation> q = em.createQuery(qSQL, Organisation.class);
            q.setParameter("user_id", user_id);
            q.setFirstResult(start);
            q.setMaxResults(max);
            return q.getResultList();
        }
    } catch (Exception ex2) {
        log.error("[getRestOrganisationsByUserId]", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * get a list of all users of an organisation
 * /*from   ww w.  ja va  2  s . c o m*/
 * @param user_level
 * @param organisation_id
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
public List<Users> getUsersByOrganisationId(long organisation_id, int start, int max, String orderby,
        boolean asc) {
    try {
        String hql = "SELECT c FROM " + "Users c " + ", IN(c.organisation_users) ou "
                + "WHERE c.deleted = 'false' AND ou.organisation.organisation_id = :organisation_id ";
        if (orderby.startsWith("c.")) {
            hql += "ORDER BY " + orderby;
        } else {
            hql += "ORDER BY " + "c." + orderby;
        }
        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        TypedQuery<Users> q = em.createQuery(hql, Users.class);
        q.setParameter("organisation_id", organisation_id);
        q.setFirstResult(start);
        q.setMaxResults(max);

        List<Users> userL = q.getResultList();
        return userL;
    } catch (Exception ex2) {
        log.error("[getUsersByOrganisationId]", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * Filter all Organisations by user TODO: Add sorting
 * /*from ww  w.  ja  v  a2  s . c o  m*/
 * @param user_level
 * @param users_id
 * @param start
 * @param max
 * @param orderby
 * @return
 */
public List<Organisation> getOrganisationsByUserId(long user_level, long user_id, int start, int max,
        String orderby, boolean asc) {
    try {
        if (authLevelManagement.checkAdminLevel(user_level)) {
            TypedQuery<Organisation> q = em.createNamedQuery("getOrganisationsByUserId", Organisation.class);
            q.setParameter("user_id", user_id);
            q.setFirstResult(start);
            q.setMaxResults(max);

            List<Organisation> userOrg = q.getResultList();

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

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

public List<PrivateMessages> getSendPrivateMessagesByUser(Long toUserId, String search, String orderBy,
        int start, Boolean asc, Long privateMessageFolderId, int max) {
    try {//from  w  w w  . jav a 2 s .  c o m

        String hql = "select c from PrivateMessages c " + "where c.from.user_id = :toUserId "
                + "AND c.isTrash = false " + "AND c.owner.user_id = :toUserId "
                + "AND c.privateMessageFolderId = :privateMessageFolderId ";

        if (search.length() != 0) {
            hql += "AND ( ";
            hql += "lower(c.subject) LIKE :search ";
            hql += "OR lower(c.message) LIKE :search ";
            hql += "OR lower(c.from.firstname) LIKE :search ";
            hql += "OR lower(c.from.lastname) LIKE :search ";
            hql += "OR lower(c.from.login) LIKE :search ";
            hql += "OR lower(c.from.adresses.email) LIKE :search ";
            hql += " ) ";
        }

        hql += "ORDER BY " + orderBy;

        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        TypedQuery<PrivateMessages> query = em.createQuery(hql, PrivateMessages.class);
        query.setParameter("toUserId", toUserId);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);
        if (search.length() != 0) {
            query.setParameter("search", StringUtils.lowerCase("%" + search + "%"));
        }
        query.setFirstResult(start);
        query.setMaxResults(max);
        List<PrivateMessages> ll = query.getResultList();

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

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

private List<Fieldvalues> getFieldsValues(int start, int max, String orderby, boolean asc, String search)
        throws Exception {
    String queryLanguage = "select c from Fieldvalues c where c.deleted = 'false'";
    if (search.length() > 0) {
        queryLanguage += " AND (c.name LIKE :searchStr " + "OR c.fieldvalues_id = :fieldvalues_id)";
    }//from   www. j  a  va2  s . c o m
    if (asc) {
        queryLanguage += " ORDER BY " + orderby + " ASC";
    } else {
        queryLanguage += " ORDER BY " + orderby + "DESC";
    }
    TypedQuery<Fieldvalues> q = em.createQuery(queryLanguage, Fieldvalues.class);
    if (search.length() > 0) {
        q.setParameter("searchStr", "%" + search + "%");
        long idNumber = 0;
        try {
            idNumber = Long.parseLong(search);
        } catch (NumberFormatException nfe) {
        }
        q.setParameter("fieldvalues_id", idNumber);
    }
    q.setFirstResult(start);
    q.setMaxResults(max);
    return q.getResultList();
}

From source file:edu.kit.dama.mdm.core.jpa.MetaDataManagerJpa.java

@Override
public final <T> List<T> findResultList(String queryString, Object[] pParameters, Class<T> entityClass,
        int pFirstIdx, int pMaxResults) throws UnauthorizedAccessAttemptException {
    List<T> result = new ArrayList();
    try {/*  w  ww .  java 2  s  .  c  om*/
        TypedQuery<T> q = entityManager.createQuery(queryString, entityClass);
        applyProperties(q);
        if (pParameters != null && pParameters.length != 0) {
            for (int i = 0; i < pParameters.length; i++) {
                q.setParameter(i + 1, pParameters[i]);
            }
        }
        q.setFirstResult((pFirstIdx >= 0) ? pFirstIdx : 0);
        if (pMaxResults > 0) {
            q.setMaxResults(pMaxResults);
        }
        result = q.getResultList();
    } catch (RuntimeException re) {
        LOGGER.warn("Failed to obtain typed query result list", re);
    } finally {
        finalizeEntityManagerAccess("find result list with plain SQL '" + queryString + "'", null, entityClass);
    }
    return result;
}