List of usage examples for javax.persistence Query setFirstResult
Query setFirstResult(int startPosition);
From source file:org.medici.bia.dao.titleoccslist.TitleOccsListDAOJpaImpl.java
/** * {@inheritDoc}//from ww w. j a v a 2s .c o m */ @SuppressWarnings("unchecked") @Override public Page searchTitlesOrOccupations(SimpleSearchTitleOrOccupation simpleSearchTitleOrOccupation, PaginationFilter paginationFilter) throws PersistenceException { // We prepare object of return method. Page page = new Page(paginationFilter); // select a.TitleOccID, a.TitleOcc, count(b.personId) from tblTitleOccsList a left outer join tblPoLink b on b.TitleOccID = a.TitleOccID group by a.titleOccID StringBuilder stringBuilder = new StringBuilder( "select a.titleOccID, a.titleOcc, count(b.personId) from tblTitleOccsList a"); stringBuilder.append(" left join tblPoLink b on b.titleOccID = a.TitleOccID "); if (simpleSearchTitleOrOccupation.getTextSearch() != null) { stringBuilder.append(" where a.titleOcc LIKE '%"); stringBuilder.append(simpleSearchTitleOrOccupation.getTextSearch()); stringBuilder.append("%' "); } else if (simpleSearchTitleOrOccupation.getRoleCatId() != null) { stringBuilder.append(" where a.roleCatMinorId="); stringBuilder.append(simpleSearchTitleOrOccupation.getRoleCatId()); } stringBuilder.append(" group by a.titleOccID "); // We set size of result. if (paginationFilter.getTotal() == null) { //select count(*) from ( select count(*) from tblTitleOccsList a left outer join tblPoLink b on b.TitleOccID = a.TitleOccID group by a.titleOccID ) count StringBuilder queryCountBuilder = new StringBuilder(""); queryCountBuilder.append("select count(*) from tblTitleOccsList a "); if (simpleSearchTitleOrOccupation.getTextSearch() != null) { queryCountBuilder.append(" where a.titleOcc LIKE '%"); queryCountBuilder.append(simpleSearchTitleOrOccupation.getTextSearch()); queryCountBuilder.append("%' "); } else if (simpleSearchTitleOrOccupation.getRoleCatId() != null) { queryCountBuilder.append(" where a.roleCatMinorID="); queryCountBuilder.append(simpleSearchTitleOrOccupation.getRoleCatId()); } // In this case we use Native Query!!! Query query = getEntityManager().createNativeQuery(queryCountBuilder.toString()); // Count(*) in native query is mapped as BigInteger, so we need to convert to Long... BigInteger result = (BigInteger) query.getSingleResult(); page.setTotal(NumberUtils.createLong(result.toString())); } // We invoke native query beacuse we use left outer join with on condition Query query = getEntityManager().createNativeQuery(stringBuilder.toString()); query.setFirstResult(paginationFilter.getFirstRecord()); query.setMaxResults(paginationFilter.getLength()); List<Object> list = (List<Object>) query.getResultList(); List<Object> result = new ArrayList<Object>(list.size()); for (int i = 0; i < list.size(); i++) { Object[] singleRow = (Object[]) list.get(i); List<Object> row = new ArrayList<Object>(0); row.add(new TitleOccsList((Integer) singleRow[0], (String) singleRow[1])); row.add(NumberUtils.createLong(((BigInteger) singleRow[2]).toString())); result.add(row); } // We set search result on return method page.setList(result); return page; }
From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java
@SuppressWarnings("unchecked") public List<BakerUser> readUsers(int firstResult, int maxResults) { EntityManager entityManager = entityManagerFactory.createEntityManager(); Query q = entityManager.createQuery("SELECT m FROM BakerUser m"); q.setFirstResult(firstResult); q.setMaxResults(maxResults);//from ww w .ja v a 2 s . com return q.getResultList(); }
From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java
@SuppressWarnings("unchecked") public List<InstalledBun> readInstalledBuns(int firstResult, int maxResults) { EntityManager entityManager = entityManagerFactory.createEntityManager(); Query q = entityManager.createQuery("SELECT m FROM InstalledBun m"); q.setFirstResult(firstResult); q.setMaxResults(maxResults);//from w w w .j ava 2 s.com return q.getResultList(); }
From source file:org.apache.roller.weblogger.business.jpa.JPAWeblogManagerImpl.java
public List getWeblogsByLetter(char letter, int offset, int length) throws WebloggerException { Query query = strategy.getNamedQuery("Weblog.getByLetterOrderByHandle"); query.setParameter(1, letter + "%"); if (offset != 0) { query.setFirstResult(offset); }//from w w w . j ava 2 s . c o m if (length != -1) { query.setMaxResults(length); } return query.getResultList(); }
From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java
public List<SubscribedResource> readSubscribedResources(int firstResult, int maxResults) { EntityManager entityManager = entityManagerFactory.createEntityManager(); Query q = entityManager.createQuery("SELECT m FROM SubscribedResource m"); q.setFirstResult(firstResult); q.setMaxResults(maxResults);//w ww .jav a 2 s.com return q.getResultList(); }
From source file:org.apache.roller.weblogger.business.jpa.JPAUserManagerImpl.java
public List getUsersByLetter(char letter, int offset, int length) throws WebloggerException { Query query = strategy.getNamedQuery("User.getByUserNameOrderByUserName"); query.setParameter(1, letter + "%"); if (offset != 0) { query.setFirstResult(offset); }/*from w w w.j a v a2s . c o m*/ if (length != -1) { query.setMaxResults(length); } return query.getResultList(); }
From source file:org.apache.roller.weblogger.business.jpa.JPAWeblogManagerImpl.java
/** * Get websites of a user//from w w w . j av a 2s .c om */ public List getWeblogs(Boolean enabled, Boolean active, Date startDate, Date endDate, int offset, int length) throws WebloggerException { //if (endDate == null) endDate = new Date(); List params = new ArrayList(); int size = 0; StringBuffer queryString = new StringBuffer(); StringBuffer whereClause = new StringBuffer(); queryString.append("SELECT w FROM Weblog w WHERE "); if (startDate != null) { Timestamp start = new Timestamp(startDate.getTime()); if (whereClause.length() > 0) whereClause.append(" AND "); params.add(size++, start); whereClause.append(" w.dateCreated > ?" + size); } if (endDate != null) { Timestamp end = new Timestamp(endDate.getTime()); if (whereClause.length() > 0) whereClause.append(" AND "); params.add(size++, end); whereClause.append(" w.dateCreated < ?" + size); } if (enabled != null) { if (whereClause.length() > 0) whereClause.append(" AND "); params.add(size++, enabled); whereClause.append(" w.enabled = ?" + size); } if (active != null) { if (whereClause.length() > 0) whereClause.append(" AND "); params.add(size++, active); whereClause.append(" w.active = ?" + size); } whereClause.append(" ORDER BY w.dateCreated DESC"); Query query = strategy.getDynamicQuery(queryString.toString() + whereClause.toString()); if (offset != 0) { query.setFirstResult(offset); } if (length != -1) { query.setMaxResults(length); } for (int i = 0; i < params.size(); i++) { query.setParameter(i + 1, params.get(i)); } return query.getResultList(); }
From source file:org.apache.syncope.core.persistence.dao.impl.SubjectSearchDAOImpl.java
@SuppressWarnings("unchecked") private <T extends AbstractSubject> List<T> doSearch(final Set<Long> adminRoles, final SearchCond nodeCond, final int page, final int itemsPerPage, final List<OrderByClause> orderBy, final SubjectType type) { List<Object> parameters = Collections.synchronizedList(new ArrayList<Object>()); // 1. get the query string from the search condition SearchSupport svs = new SearchSupport(type); StringBuilder queryString = getQuery(nodeCond, parameters, type, svs); // 2. take into account administrative roles and ordering OrderBySupport orderBySupport = parseOrderBy(type, svs, orderBy); if (queryString.charAt(0) == '(') { queryString.insert(0, buildSelect(orderBySupport)); queryString.append(buildWhere(orderBySupport, type)); } else {/*from w w w . j a v a 2s . c o m*/ queryString.insert(0, buildSelect(orderBySupport).append('(')); queryString.append(')').append(buildWhere(orderBySupport, type)); } queryString.append(getAdminRolesFilter(adminRoles, type)).append(')').append(buildOrderBy(orderBySupport)); // 3. prepare the search query Query query = entityManager.createNativeQuery(queryString.toString()); // 4. page starts from 1, while setFirtResult() starts from 0 query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1)); if (itemsPerPage >= 0) { query.setMaxResults(itemsPerPage); } // 5. populate the search query with parameter values fillWithParameters(query, parameters); LOG.debug("Native query\n{}\nwith parameters\n{}", queryString.toString(), parameters); // 6. Prepare the result (avoiding duplicates) List<T> result = new ArrayList<T>(); for (Object subjectId : query.getResultList()) { long actualId; if (subjectId instanceof Object[]) { actualId = ((Number) ((Object[]) subjectId)[0]).longValue(); } else { actualId = ((Number) subjectId).longValue(); } T subject = type == SubjectType.USER ? (T) userDAO.find(actualId) : (T) roleDAO.find(actualId); if (subject == null) { LOG.error("Could not find {} with id {}, even though returned by the native query", type, subjectId); } else { if (!result.contains(subject)) { result.add(subject); } } } return result; }
From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java
public List<BakerProperty> readProperties(int firstResult, int maxResults) { EntityManager entityManager = entityManagerFactory.createEntityManager(); Query q = entityManager.createQuery("SELECT m FROM BakerProperty m ORDER BY m.id"); q.setFirstResult(firstResult); q.setMaxResults(maxResults);//from w w w.j a va 2s . c o m return q.getResultList(); }
From source file:org.nuxeo.ecm.activity.ActivityStreamServiceImpl.java
@SuppressWarnings("unchecked") protected ActivitiesList queryAll(EntityManager em, long offset, long limit) { Query query = em.createQuery("select activity from Activity activity order by activity.id asc"); if (limit > 0) { query.setMaxResults((int) limit); }/* ww w. j a va 2 s. c o m*/ if (offset > 0) { query.setFirstResult((int) offset); } return new ActivitiesListImpl(query.getResultList()); }