List of usage examples for javax.persistence Query setFirstResult
Query setFirstResult(int startPosition);
From source file:it.webappcommon.lib.jpa.ControllerStandard.java
/** * //www. j a va 2 s . co m * Metodo che restituisce una collezione di oggetti specificati come * parametro, tramite la query da lanciare, la mappa dei parametri, * l'elemento di inizio e il numero di elementi desiderati (mettendo a 0 * questo parametro li restituisce tutti) * * @param classObj * @param query * @param map * @param firstItem * @param batchSize * @return * @throws java.lang.Exception */ public <T extends EntityBaseStandard> List<T> findList2(Class<T> classObj, String query, Map<String, Object> map, int firstItem, int batchSize) throws Exception { List<T> returnValue = null; EntityManagerFactory emf = null; EntityManager em = null; Map.Entry entry = null; Iterator i = null; Query q = null; try { /* Istanzio l'entity manager */ emf = getEntityManagerFactory(); em = emf.createEntityManager(); /* Genero la query */ q = em.createNamedQuery(query); /* * Se il numero di elementi diverso da 0 specifico quanti e da * dove cominciare */ if (batchSize > 0) { q.setFirstResult(firstItem); q.setMaxResults(batchSize); } /* Verifico la validit della mappa */ if (map != null) { /* Per ogni elemento della mappa setto il parametro */ for (i = map.entrySet().iterator(); i.hasNext();) { entry = (Map.Entry) i.next(); q.setParameter((String) entry.getKey(), entry.getValue()); } } /* Calcolo la collezione di elementi desiderati */ returnValue = (List<T>) q.getResultList(); } catch (Exception e) { throw e; } finally { if (!passedEm) { PersistenceManagerUtil.close(em); } entry = null; em = null; q = null; i = null; } return returnValue; }
From source file:it.webappcommon.lib.jpa.ControllerStandard.java
/** * // ww w . j a va 2s . com * Metodo che restituisce tutta la collezione di oggetti specificati come * parametro, tramite l'elemento di inizio e il numero di elementi * desiderati (mettendo a 0 questo parametro li restituisce tutti) * * @param classObj * @param query * @param map * @param firstItem * @param batchSize * @return * @throws java.lang.Exception */ public <T extends EntityBaseStandard> List<T> findAll(Class<T> classObj, int firstItem, int batchSize) throws Exception { List<T> returnValue = new ArrayList<T>(); // Non piu' null cos posso // semplificare il codice // del chiamante EntityManagerFactory emf = null; EntityManager em = null; Iterator i = null; Query q = null; try { /* Istanzia l'entity manager */ emf = getEntityManagerFactory(); em = emf.createEntityManager(); /* Crea la query */ q = em.createQuery("from " + classObj.getName()); /* * Se il numero di elementi diverso da 0 specifico quanti e da * dove cominciare */ if (batchSize > 0) { q.setFirstResult(firstItem); q.setMaxResults(batchSize); } /* Calcolo la collezione di elementi desiderati */ returnValue = q.getResultList(); } catch (Exception e) { throw e; } finally { if (!passedEm) { PersistenceManagerUtil.close(em); } em = null; q = null; i = null; } return returnValue; }
From source file:org.apache.roller.weblogger.business.jpa.JPAWeblogEntryManagerImpl.java
/** * @inheritDoc//from w ww .jav a 2 s. c om */ public List getHotWeblogs(int sinceDays, int offset, int length) throws WebloggerException { // figure out start date Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.DATE, -1 * sinceDays); Date startDate = cal.getTime(); Query query = strategy.getNamedQuery( "WeblogHitCount.getByWeblogEnabledTrueAndActiveTrue&DailyHitsGreaterThenZero&WeblogLastModifiedGreaterOrderByDailyHitsDesc"); query.setParameter(1, startDate); // Was commented out due to https://glassfish.dev.java.net/issues/show_bug.cgi?id=2084 // TODO: determine if this is still an issue. Is it a problem with other JPA implementations? if (offset != 0) { query.setFirstResult(offset); } if (length != -1) { query.setMaxResults(length); } return query.getResultList(); }
From source file:org.medici.bia.dao.image.ImageDAOJpaImpl.java
/** * {@inheritDoc}/*www . j a v a2 s . c o m*/ */ @Override public Page searchImagesCreatedBefore(Date timeStamp, List<ImageType> types, PaginationFilter paginationFilter) throws PersistenceException { Page page = new Page(paginationFilter); String jpql = "FROM Image WHERE dateCreated <= :timeStamp"; if (types != null && types.size() > 0) { jpql += " AND imageType IN (:types)"; } Query query = null; if (paginationFilter.getTotal() == null) { String countQuery = "SELECT COUNT(*) " + jpql; query = getEntityManager().createQuery(countQuery); query.setParameter("timeStamp", timeStamp); if (types != null && types.size() > 0) { query.setParameter("types", types); } page.setTotal(new Long((Long) query.getSingleResult())); } query = getEntityManager().createQuery(jpql + getOrderByQuery(paginationFilter.getSortingCriterias())); query.setParameter("timeStamp", timeStamp); if (types != null && types.size() > 0) { query.setParameter("types", types); } query.setFirstResult(paginationFilter.getFirstRecord()); query.setMaxResults(paginationFilter.getLength()); page.setList(query.getResultList()); return page; }
From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java
/** * ??????/*w w w . j a v a2 s . c o m*/ * @param paramVO * @return * @throws DaoException */ @Override public List<Member> getMemberInfoList(TzQueryRequestParamVO paramVO) throws DaoException { try { List<Member> list = new ArrayList<Member>(); StringBuffer sql = new StringBuffer(); sql.append("SELECT m.* FROM member m LEFT JOIN business_unit b ON m.orgId=b.id "); sql.append("WHERE b.`name`=?1 "); if (StringUtils.isNotBlank(paramVO.getProName())) { sql.append(" AND ( m.name LIKE ?2 OR m.position LIKE ?3 OR m.identificationNo LIKE ?4 )"); } sql.append(" ORDER BY m.id desc "); Query query = entityManager.createNativeQuery(sql.toString(), Member.class); query.setParameter(1, paramVO.getBusName()); if (StringUtils.isNotBlank(paramVO.getProName())) { query.setParameter(2, "%" + paramVO.getProName() + "%"); query.setParameter(3, "%" + paramVO.getProName() + "%"); query.setParameter(4, "%" + paramVO.getProName() + "%"); } if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) { query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize()); query.setMaxResults(paramVO.getPageSize()); } list = query.getResultList(); return list; } catch (Exception e) { throw new DaoException( "TzQueryDaoImpl-->getMemberInfoList()???????", e); } }
From source file:org.mingle.pear.persistence.dao.impl.GenericDaoImpl.java
/** * ??//from ww w . ja va 2 s . co m * * @param qt * @param clazz * @return */ protected <R> Query createQuery(QueryTemplate qt, Class<R> clazz) { Query query = null; if (qt instanceof JqlQueryTemplate) { if (clazz == null) { query = entityManager().createQuery(qt.getQuery()); } else { query = entityManager().createQuery(qt.getQuery(), clazz); } } if (qt instanceof NamedQueryTemplate) { if (clazz == null) { query = entityManager().createNamedQuery(qt.getQuery()); } else { query = entityManager().createNamedQuery(qt.getQuery(), clazz); } } if (qt instanceof SqlQueryTemplate) { query = entityManager().createNativeQuery(qt.getQuery()); } if (qt instanceof SqlResultSetMappingQueryTemplate) { SqlResultSetMappingQueryTemplate sqt = (SqlResultSetMappingQueryTemplate) qt; query = entityManager().createNativeQuery(qt.getQuery(), sqt.getSqlResultMapping()); } // ? if (qt.getParameters() != null) { for (Entry<String, ?> paramEntry : qt.getParameters().entrySet()) { query.setParameter(paramEntry.getKey(), paramEntry.getValue()); } } // if (qt.isCachable()) { query.setHint("org.hibernate.cacheable", true); if (StringUtils.isNotBlank(qt.getCacheRegion())) { query.setHint("org.hibernate.cacheRegion", qt.getCacheRegion()); } else { query.setHint("org.hibernate.cacheRegion", clazz.getName()); } } // query.setFirstResult(qt.getFirstResult()); query.setMaxResults(qt.getMaxResults()); return query; }
From source file:org.j2free.jpa.Controller.java
/** * * @param namedQuery/* w w w. j a v a2s . co m*/ * @param start * @param limit * @param parameters * @return */ public List<Object[]> namedList(String namedQuery, int start, int limit, KeyValuePair<String, ? extends Object>... parameters) { Query query = em.createNamedQuery(namedQuery); if (parameters != null) { for (KeyValuePair<String, ? extends Object> parameter : parameters) { query.setParameter(parameter.key, parameter.value); } } if (start > 0) { query.setFirstResult(start); } if (limit > 0) { query.setMaxResults(limit); } try { return (List<Object[]>) query.getResultList(); } catch (NoResultException nre) { return null; } }
From source file:org.j2free.jpa.Controller.java
/** * * @param namedQuery//from w ww.jav a 2 s. c o m * @param start * @param limit * @param parameters * @return */ public List<Object[]> namedNativeList(String namedQuery, int start, int limit, KeyValuePair<String, ? extends Object>... parameters) { Query query = em.createNativeQuery(namedQuery); if (parameters != null) { for (KeyValuePair<String, ? extends Object> parameter : parameters) { query.setParameter(parameter.key, parameter.value); } } if (start > 0) { query.setFirstResult(start); } if (limit > 0) { query.setMaxResults(limit); } try { return (List<Object[]>) query.getResultList(); } catch (NoResultException nre) { return null; } }
From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java
/** * {@inheritDoc} //from w w w .java 2 s .com */ @Override public Page findHistory(User user, PaginationFilter paginationFilter) throws PersistenceException { Page page = new Page(paginationFilter); if (paginationFilter.getTotal() == null) { String queryString = "SELECT count(user) FROM UserHistory WHERE user=:user and logicalDelete=false "; Query query = getEntityManager().createQuery(queryString); query.setParameter("user", user); page.setTotal(new Long((Long) query.getSingleResult())); } String objectsQuery = "FROM UserHistory WHERE user=:user and logicalDelete=false "; paginationFilter = generatePaginationFilterMYSQL(paginationFilter); String jpql = objectsQuery + getOrderByQuery(paginationFilter.getSortingCriterias()); logger.debug("JPQL Query : " + jpql); Query query = getEntityManager().createQuery(jpql); query.setParameter("user", user); query.setFirstResult(paginationFilter.getFirstRecord()); query.setMaxResults(paginationFilter.getLength()); page.setList(query.getResultList()); return page; }
From source file:org.apache.openejb.util.proxy.QueryProxy.java
/** * @param method the method//w ww .ja va2 s . c om * @param args queryName (String) -> first parameter, parameters (Map<String, ?>) or (Object[]), first and max (int) -> max follows first * @param type the query type * @return the expected result */ private Object query(final Method method, final Object[] args, final QueryType type) { if (args.length < 1) { throw new IllegalArgumentException("query() needs at least the query name"); } int matched = 0; Query query; if (String.class.isAssignableFrom(args[0].getClass())) { switch (type) { case NAMED: query = em.createNamedQuery((String) args[0]); break; case NATIVE: query = em.createNativeQuery((String) args[0]); break; default: query = em.createQuery((String) args[0]); } matched++; for (int i = 1; i < args.length; i++) { if (args[i] == null) { continue; } if (Map.class.isAssignableFrom(args[i].getClass())) { for (final Map.Entry<String, ?> entry : ((Map<String, ?>) args[i]).entrySet()) { query = query.setParameter(entry.getKey(), entry.getValue()); } matched++; } else if (args[i].getClass().isArray()) { final Object[] array = (Object[]) args[i]; for (int j = 0; j < array.length; j++) { query = query.setParameter(j, array[j]); } matched++; } else if (isInt(args[i].getClass())) { final int next = i + 1; if (args.length == next || !isInt(args[next].getClass())) { throw new IllegalArgumentException("if you provide a firstResult (first int parameter)" + "you should provide a maxResult too"); } final int first = (Integer) args[i]; final int max = (Integer) args[next]; query = query.setFirstResult(first); query = query.setMaxResults(max); matched += 2; i++; } else { throw new IllegalArgumentException( "not managed parameter " + args[i] + " of type " + args[i].getClass()); } } if (matched != args.length) { throw new IllegalArgumentException( "all argument was not used, please check you signature looks like:" + " <ReturnType> query(String name, Map<String, ?> parameters, int firstResult, int maxResult)"); } } else { throw new IllegalArgumentException("query() needs at least the query name of type String"); } return getQueryResult(method, query); }