Example usage for javax.persistence Query setFirstResult

List of usage examples for javax.persistence Query setFirstResult

Introduction

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

Prototype

Query setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves a specified quantity of annotations associated to a specified Resource through its URL, ordered by FirstAuthor
 * @param _url//from   w w  w .j av a2s.c o  m
 * @param asc
 * @param first_indice
 * @param max_results
 * @return
 */
public List<Annotation> retrieveAnnotationsGroupByFirstAuthor(String _url, boolean asc, int first_indice,
        int max_results) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " annotation.id desc";
        if (asc)
            order_by_clause = " annotation.id asc";
        Query _query = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.creator,"
                        + order_by_clause)
                .setParameter(1, _uri);
        _query.setFirstResult(first_indice);
        _query.setMaxResults(max_results);
        List<Annotation> annotations = _query.getResultList();
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ???????//  w  ww.  j av a2  s  .  c  om
 * @author ChenXiaolin 2015-12-04
 * @param paramVO
 * @return
 * @throws DaoException
 */
@SuppressWarnings("unchecked")
@Override
public List<TzQueryResponseBusVO> getAccountInfo(TzQueryRequestParamVO paramVO) throws DaoException {
    try {
        String changeSql = "SELECT DISTINCT outBus.id,outBus.name,outBus.license_no,outBus.type,outBus.address";
        StringBuffer sql = new StringBuffer();
        StringBuffer publicSql = new StringBuffer();
        publicSql.append(" FROM tz_business_account account");
        publicSql.append(" INNER JOIN business_unit inBus ON inBus.id = account.in_business_id");
        publicSql.append(" INNER JOIN business_unit outBus ON outBus.id = account.out_business_id");
        if (paramVO.getSalesType() == 0) {//
            sql.append(changeSql);
            sql.append(publicSql);
            sql.append(" WHERE inBus.id = ?1");
        } else if (paramVO.getSalesType() == 1) {//
            String temporarySql = changeSql.replace("outBus", "inBus");
            sql.append(temporarySql);
            sql.append(publicSql);
            sql.append(" WHERE outBus.id = ?1");
        }
        Query query = entityManager.createNativeQuery(sql.toString());
        query.setParameter(1, paramVO.getFirstBusId());
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        List<Object[]> objects = query.getResultList();
        return setAccounInfo(objects, paramVO);
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getAccountInfo()???????,?",
                e);
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}//from   w w w .  j  a  va  2s  .  c  o m
 */
@Override
public Page searchRecipientDocumentsPerson(String personToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE recipientPeople.personId=" + personToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}/*from  ww  w  . j  a  v a 2  s  . c o  m*/
 */
@Override
public Page searchRecipientDocumentsPlace(String placeToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE recipientPlace.placeAllId=" + placeToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ?id??//from   w w w.j  a v a2  s .c  om
 * @param paramVO
 * @param fId
 * @return
 * @throws DaoException
 */
@Override
public List<FacilityMaintenanceRecord> getFacilityMaintenanceRecordList(TzQueryRequestParamVO paramVO, Long fId)
        throws DaoException {
    try {
        List<FacilityMaintenanceRecord> list = new ArrayList<FacilityMaintenanceRecord>();
        StringBuffer sql = new StringBuffer();
        sql.append(
                "SELECT * FROM facility_maintenance_record WHERE facility_id=?1 ORDER BY maintenance_time desc");
        Query query = entityManager.createNativeQuery(sql.toString(), FacilityMaintenanceRecord.class);
        query.setParameter(1, fId);
        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-->getFacilityMaintenanceRecordList()?id???",
                e);
    }
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ??ID??//  w ww  .ja  v  a2 s .co  m
 * @author ChenXiaolin 2015-12-02
 * @param paramVO
 * @return
 * @throws DaoException
 */
@SuppressWarnings("unchecked")
@Override
public List<TzQueryResponseBusVO> getSaleBusiness(TzQueryRequestParamVO paramVO) throws DaoException {
    try {
        StringBuffer sb_qyery = new StringBuffer(" and bus.other_address LIKE '");
        if (StringUtil.isNotEmpty(paramVO.getProvice())) { //?
            sb_qyery.append(paramVO.getProvice()).append("-");
        }
        if (StringUtil.isNotEmpty(paramVO.getCity())) { //
            sb_qyery.append(paramVO.getCity()).append("-");
        }
        if (StringUtil.isNotEmpty(paramVO.getArea())) { //
            sb_qyery.append(paramVO.getArea());
        }
        sb_qyery.append("%'");

        StringBuffer sb = new StringBuffer();
        sb.append(" select * from (");
        sb.append(
                " SELECT pro.name proName,pro.category cat,pro.barcode bar, bus.id busId,bus.other_address oadd,bus.name busName,");
        sb.append(
                "bus.license_no lic,bus.type type,bus.address addr,IF(bus.id=pro.producer_id,'','??') relation,pro.id");
        sb.append(" FROM tz_stock stock ");
        sb.append(" INNER JOIN business_unit bus ON bus.id = stock.business_id ");
        sb.append(" INNER JOIN product pro ON pro.id = stock.product_id");
        sb.append(" WHERE stock.product_id = ?1");
        sb.append(sb_qyery);
        sb.append(" GROUP BY bus.id ) test");
        Query query = entityManager.createNativeQuery(sb.toString());
        query.setParameter(1, Long.parseLong(paramVO.getProId()));
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        List<Object[]> objects = query.getResultList();
        return setSaleBusiness(objects);
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getSaleBusiness()??ID?? ,?",
                e);
    }
}

From source file:com.gettec.fsnip.fsn.dao.product.impl.ProductDAOImpl.java

/**
 * ??/*from  w  ww  .  j  a v  a2 s  . c o m*/
 * @return
 * @author ZhaWanNeng   2015/04/17
 */
@SuppressWarnings("unchecked")
public List<Product> getproductList(int pageSize, int page) throws DaoException {
    try {
        String sql = " SELECT  e FROM  " + entityClass.getName() + " e ";
        Query query = entityManager.createQuery(sql);
        query.setFirstResult((page - 1) * pageSize);
        query.setMaxResults(pageSize);
        List<Product> result = query.getResultList();
        return result;
    } catch (Exception e) {
        throw new DaoException("ProductDAOImpl.getproductList()-->> ?", e);
    }
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard> List<T> findList(Class<T> classObj, String query, Map<String, Object> map,
        int firstItem, int batchSize) throws Exception {
    List<T> returnValue = null;

    EntityManagerFactory emf = null;// w ww  .  ja  v a 2s .  co m
    EntityManager em = null;
    Query q = null;
    Iterator i = null;
    Map.Entry entry = null;
    try {
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        q = em.createNamedQuery(query);
        if (batchSize > 0) {
            q.setFirstResult(firstItem);
            q.setMaxResults(batchSize);
        }
        if (map != null) {
            for (i = map.entrySet().iterator(); i.hasNext();) {
                entry = (Map.Entry) i.next();
                q.setParameter((String) entry.getKey(), entry.getValue());
            }
        }
        returnValue = (List<T>) q.getResultList();
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
        entry = null;
    }
    return returnValue;
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ???//w w w  . j a va  2 s .c  om
 * @author ChenXiaolin 2015-12-04
 * @param paramVO
 * @return
 * @throws DaoException
 */
@SuppressWarnings("unchecked")
@Override
public List<TzQueryResponseTansDetailVO> getBusQueryTransDetail(TzQueryRequestParamVO paramVO)
        throws DaoException {
    try {
        StringBuffer sb = new StringBuffer();
        sb.append(
                " SELECT pro.barcode,pro.name,pro.format,acInfo.product_batch,acInfo.production_date,account.create_time,acInfo.product_num");
        sb.append(" FROM tz_business_account account");
        sb.append(" INNER JOIN tz_business_account_info acInfo ON account.id = acInfo.business_account_id");
        sb.append(" INNER JOIN product pro ON pro.id = acInfo.product_id");
        sb.append(" INNER JOIN business_unit inBus  ON inBus.id = account.in_business_id");
        sb.append(" INNER JOIN business_unit outBus ON outBus.id = account.out_business_id");
        if (paramVO.getSalesType() == 0) {//?????
            sb.append(" WHERE account.out_business_id = ?1 AND inBus.id = ?2");
        } else if (paramVO.getSalesType() == 1) {//?????
            sb.append(" WHERE account.in_business_id = ?1 AND outBus.id = ?2");
        }
        /* ?  */
        if (StringUtil.isNotEmpty(paramVO.getProBarcode())) {//???
            sb.append(" AND pro.barcode LIKE '%").append(paramVO.getProBarcode()).append("%'");
        }
        if (StringUtil.isNotEmpty(paramVO.getProName())) {//???
            sb.append(" AND pro.name LIKE '%").append(paramVO.getProName()).append("%'");
        }
        if (StringUtil.isNotEmpty(paramVO.getTransSDate())) {
            sb.append(" AND account.create_time >= '").append(paramVO.getTransSDate()).append(" 00:00:00")
                    .append("'");
        }
        if (StringUtil.isNotEmpty(paramVO.getTransEDate())) {
            sb.append(" AND account.create_time <= '").append(paramVO.getTransEDate()).append(" 23:59:59")
                    .append("'");
        }
        sb.append(" ORDER BY account.create_time DESC");
        /* sql  */
        Query query = entityManager.createNativeQuery(sb.toString());
        query.setParameter(1, paramVO.getBusId());
        query.setParameter(2, paramVO.getFirstBusId());
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        List<Object[]> objects = query.getResultList();
        return setBusQueryDetail(objects);
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getBusQueryTransDetail()???,?",
                e);
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}//from  ww w  . j a  va 2s.  com
 */
@Override
public Page searchDocumentsRelated(String personToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE entryId IN (SELECT document.entryId FROM org.medici.bia.domain.EpLink WHERE person.personId="
                    + personToSearch + ") AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;

}