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:ru.codeinside.adm.AdminServiceImpl.java

private List<Group> getControlledGroups(Employee employee, int startIndex, int count, String[] order,
        boolean[] asc, AdvancedFilterableSupport newSender, String target) {
    StringBuilder q = new StringBuilder("select e." + target + " from Employee e where e = :employee");

    if (newSender != null) {
        for (Container.Filter f : newSender.getFilters()) {
            String field = ((SimpleStringFilter) f).getPropertyId().toString();
            q.append(" and lower(e.").append(target).append(".").append(field).append(") LIKE lower(:")
                    .append(field).append(")");
        }// ww  w  .j  a va 2 s . c om
    }
    for (int i = 0; i < order.length; i++) {
        if (i == 0) {
            q.append(" order by ");
        } else {
            q.append(", ");
        }
        q.append("e.").append(target).append(".").append(order[i]).append(asc[i] ? " asc" : " desc");
    }
    TypedQuery<Group> query = em.createQuery(q.toString(), Group.class).setParameter("employee", employee);
    if (newSender != null) {
        for (Container.Filter f : newSender.getFilters()) {
            String field = ((SimpleStringFilter) f).getPropertyId().toString();
            String value = ((SimpleStringFilter) f).getFilterString();
            query.setParameter(field, "%" + value + "%");
        }
    }
    return query.setFirstResult(startIndex).setMaxResults(count).getResultList();
}

From source file:ru.codeinside.adm.AdminServiceImpl.java

public List<Bid> bidsByLogin(String login, final int start, final int count, String[] order, boolean[] asc,
        AdvancedFilterableSupport newSender) {
    StringBuilder q = new StringBuilder(
            "select distinct(s.bid) from BidWorkers s where s.employee.login=:login ");

    Set<Timestamp> timestamps = null;
    if (newSender != null) {
        timestamps = archiveQueryFilters(newSender, q);
    }//from   ww w  .jav  a 2 s  .c o  m

    for (int i = 0; i < order.length; i++) {
        if (i == 0) {
            q.append(" order by ");
        } else {
            q.append(", ");
        }
        q.append("s.bid.").append(order[i]).append(asc[i] ? " asc" : " desc");
    }

    TypedQuery<Bid> query = em.createQuery(q.toString(), Bid.class).setParameter("login", login);
    if (newSender != null) {
        for (Container.Filter filter : newSender.getFilters()) {
            if (filter instanceof SimpleStringFilter) {
                String field = ((SimpleStringFilter) filter).getPropertyId().toString();
                String value = ((SimpleStringFilter) filter).getFilterString();
                if (field.equals("procedure.name")) {
                    query.setParameter("name", "%" + value + "%");
                } else if (field.equals("id")) {
                    try {
                        query.setParameter(field, Long.parseLong(value));
                    } catch (Exception e) {
                        //
                    }
                }
            }
        }
        Iterator<Timestamp> iterator = timestamps.iterator();
        if (timestamps.size() == 1) {
            return query.setParameter("value", iterator.next()).setFirstResult(start).setMaxResults(count)
                    .getResultList();
        } else if (timestamps.size() == 2) {
            Timestamp next = iterator.next();
            Timestamp next1 = iterator.next();
            return query.setParameter("startValue", next).setParameter("endValue", next1).setFirstResult(start)
                    .setMaxResults(count).getResultList();
        }
    }

    return query.setFirstResult(start).setMaxResults(count).getResultList();
}