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:com.lhfs.fsn.dao.testReport.impl.TestReportDaoImpl.java

/**
 * ???//from  ww w .  j  a  v  a2  s .c om
 * @param businessUnit
 * @param page
 * @param pageSize
 * @return
 */
public List<ProductJGVO> getProductByCirculationBusinessUnit(BusinessUnit businessUnit, int page,
        int pageSize) {
    String sql = "SELECT DISTINCT p.id,p.name,p.format,p.status,p.product_type,(SELECT pci.name FROM product_category_info pci WHERE pci.id = p.category_id) categoryName ";
    sql += " FROM product p ";
    sql += " INNER JOIN  t_meta_from_to_business tb ON p.id = tb.pro_id ";
    sql += " INNER JOIN t_meta_enterprise_to_provider tp ON tp.provider_id = tb.from_bus_id AND tp.business_id = ?1";
    //      sql +=this.getConfig(licenseNo, qsNo,businessName, buslicenseNo);
    sql += " WHERE  tb.del = false AND tb.to_bus_id = ?2";
    //      sql +=this.getConfig(licenseNo, qsNo,businessName, buslicenseNo);
    //      if (page > 0) {
    //         int start  = 0;
    //         int end  = pageSize;
    //         start = (page - 1) * pageSize;
    //         sql += " LIMIT  "+start+","+end+" ";
    //      }
    Query query = entityManager.createNativeQuery(sql);
    query.setParameter(1, businessUnit.getId());
    query.setParameter(2, businessUnit.getId());
    query.setFirstResult((page - 1) * pageSize);
    query.setMaxResults(pageSize);
    List<Object[]> objs = query.getResultList();
    List<ProductJGVO> proList = new ArrayList<ProductJGVO>();
    for (Object[] obj : objs) {
        ProductJGVO e = new ProductJGVO();
        e.setId(obj[0] == null ? null : Long.parseLong(obj[0].toString()));
        e.setProName(obj[1] == null ? null : obj[1].toString());
        e.setFormat(obj[2] == null ? null : obj[2].toString());
        e.setStatus(obj[3] == null ? null : obj[3].toString());
        e.setProductType(obj[4] == null ? null : Integer.parseInt(obj[4].toString()) == 2 ? "" : "?");
        e.setCategoryName(obj[5] == null ? null : obj[5].toString());
        proList.add(e);
    }
    return proList;
}

From source file:org.apache.roller.weblogger.business.jpa.JPAUserManagerImpl.java

public List getUsersStartingWith(String startsWith, Boolean enabled, int offset, int length)
        throws WebloggerException {
    Query query = null;

    if (enabled != null) {
        if (startsWith != null) {
            query = strategy.getNamedQuery("User.getByEnabled&UserNameOrEmailAddressStartsWith");
            query.setParameter(1, enabled);
            query.setParameter(2, startsWith + '%');
            query.setParameter(3, startsWith + '%');
        } else {//www  .j av a  2s  .c om
            query = strategy.getNamedQuery("User.getByEnabled");
            query.setParameter(1, enabled);
        }
    } else {
        if (startsWith != null) {
            query = strategy.getNamedQuery("User.getByUserNameOrEmailAddressStartsWith");
            query.setParameter(1, startsWith + '%');
        } else {
            query = strategy.getNamedQuery("User.getAll");
        }
    }
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return query.getResultList();
}

From source file:org.medici.bia.dao.user.UserDAOJpaImpl.java

/**
 * {@inheritDoc}//from   ww w .  j  a  va 2 s. co m
 */
@Override
public Page searchWhoIsOnline(PaginationFilter paginationFilter) {
    // We prepare object of return method.
    Page page = new Page(paginationFilter);

    // Number of minutes to check last update for people online
    //DateTime dateTime = (new DateTime(System.currentTimeMillis())).minusMinutes(5);
    DateTime dateTime = (new DateTime(System.currentTimeMillis())).minusMinutes(30);

    String baseQuery = " FROM User user, AccessLog accessLog WHERE user.account = accessLog.account and (accessLog.dateAndTime > '"
            + DateUtils.getMYSQLDateTime(dateTime) + "')";

    Query query = null;
    // We set size of result.
    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(DISTINCT user) " + baseQuery;

        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    // We manage sorting (this manages sorting on multiple fields)
    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    String jpql = "SELECT DISTINCT(user) " + baseQuery
            + getOrderByQuery(paginationFilter.getSortingCriterias(), "user");
    logger.info("JPQL Query : " + jpql);
    query = getEntityManager().createQuery(jpql);
    // We set pagination  
    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    // We set search result on return method
    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.image.ImageDAOJpaImpl.java

/**
 * {@inheritDoc}/*from ww w  .j ava2 s .c  o m*/
 */
@Override
public Image findVolumeImage(Integer volNum, String volLetExt, Integer imageOrder) throws PersistenceException {
    StringBuilder stringBuilder = new StringBuilder(" FROM Image WHERE volNum=:volNum and volLetExt ");
    if (!StringUtils.isEmpty(volLetExt))
        stringBuilder.append(" LIKE :volLetExt");
    else
        stringBuilder.append(" is null");
    stringBuilder.append(" and imageOrder=:imageOrder");

    Query query = getEntityManager().createQuery(stringBuilder.toString());
    query.setParameter("volNum", volNum);
    if (!StringUtils.isEmpty(volLetExt)) {
        query.setParameter("volLetExt", volLetExt);
    }

    query.setParameter("imageOrder", imageOrder);
    query.setFirstResult(0);
    query.setMaxResults(1);
    return (Image) query.getSingleResult();
}

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

/**
 * {@inheritDoc}/*from   w w  w .j  a  v a2 s  . c o m*/
 */
@Override
public Page searchDocumentsCreatedBefore(Date date, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    String jpql = "FROM Document WHERE dateCreated <= :dateTime";
    Query query = null;

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

    query = getEntityManager().createQuery(jpql + getOrderByQuery(paginationFilter.getSortingCriterias()));
    query.setParameter("dateTime", date);

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

    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.user.UserDAOJpaImpl.java

/**
 * {@inheritDoc}//from   w w w .  j av  a  2  s  . c om
 */
@Override
public Page searchMYSQL(org.medici.bia.common.search.Search searchContainer,
        PaginationFilter paginationFilter) {
    // We prepare object of return method.
    Page page = new Page(paginationFilter);

    Query query = null;
    // We set size of result.
    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + searchContainer.toJPAQuery();

        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    String objectsQuery = searchContainer.toJPAQuery();

    // We manage sorting (this manages sorting on multiple fields)
    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    String jpql = objectsQuery + getOrderByQuery(paginationFilter.getSortingCriterias());
    logger.info("JPQL Query : " + jpql);
    query = getEntityManager().createQuery(jpql);
    // We set pagination  
    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    // We set search result on return method
    page.setList(query.getResultList());

    return page;
}

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

/**
 * Funziona SOLO CON IMPLEMENTAZIONE HIBERNATE
 * /*from w  w  w .  j av  a  2  s . c  om*/
 * @param aList
 * @param aFilter
 * @return
 * @throws Exception
 */
public <T extends EntityBaseStandard> ArrayList<T> getFilteredList(Class<T> classObj, String aFilter)
        throws Exception {
    ArrayList<T> returnValue = null;

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Session session = null;
    Criteria cri = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        session = (Session) em.getDelegate();

        org.hibernate.Query q = session.createQuery("from " + classObj.getName());
        q.setFirstResult(0);
        q.setMaxResults(100);
        List<T> resAll = q.list();

        org.hibernate.Query filterQuery = session.createFilter(resAll, aFilter);

        /* Effettuo la query */
        returnValue = (ArrayList) filterQuery.list();
    } catch (Exception e) {
        throw e;
    } finally {
        session = null;
        cri = null;
        em = null;
    }
    return returnValue;
}

From source file:com.gettec.fsnip.fsn.dao.member.impl.MemberDAOImpl.java

/**
 * ???????//from   ww  w.  ja  v  a2  s.c o  m
 * @param condition 
 * @param page
 * @param pageSize
 * @return List<String>
 * @throws DaoException
 * @author LongXianZhen
 */
@SuppressWarnings("unchecked")
@Override
public List<String> getBarcodeListByCondition(String condition, int page, int pageSize) throws DaoException {
    try {
        List<String> barcodes = null;
        if (StringUtils.isNotBlank(condition)) {
            String sql = "select barcode from member where barcode like :barcode ";
            Query query = entityManager.createNativeQuery(sql).setParameter("barcode", "%" + condition + "%");
            if (page > 0) {
                query.setFirstResult((page - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            barcodes = query.getResultList();
        }
        return barcodes;
    } catch (Exception e) {
        throw new DaoException(
                "MemberDAOImpl.getBarcodeListByCondition()???????, ?",
                e);
    }
}

From source file:com.gettec.fsnip.fsn.dao.member.impl.MemberDAOImpl.java

/**
 * ??//from  w  w  w  .j  a  va 2  s.c  o m
 * @param page 
 * @param size 
 * @param organization 
 * @return
 * @throws DaoException 
 */
@SuppressWarnings("unchecked")
@Override
public List<Member> getAllNotLocalMember(int page, int size, Long organization) throws DaoException {
    try {
        String sql = "SELECT  p.* FROM member p " + "RIGHT JOIN t_meta_initialize_member t ON p.id=t.member_id "
                + "WHERE t.`local`=0 AND t.first_storage_id IS NULL AND t.organization=?1";
        Query query = entityManager.createNativeQuery(sql.toString(), Member.class);
        query.setParameter(1, organization);
        if (page > 0) {
            query.setFirstResult((page - 1) * size);
            query.setMaxResults(size);
        }
        List<Member> result = query.getResultList();
        return result;
    } catch (Exception e) {
        throw new DaoException(
                "MemberDAOImpl.getAllNotLocalMember() ???",
                e);
    }
}

From source file:com.gettec.fsnip.fsn.dao.member.impl.MemberDAOImpl.java

/**
 * ??//from ww w  .ja v a2s  .com
 * @author tangxin 2015-05-05
 */
@SuppressWarnings("unchecked")
@Override
public List<DetailAlbumVO> getMemberAlbums(Long organization, int page, int pageSize, String cut)
        throws DaoException {
    try {
        String sql = "SELECT pd.id,pd.`name`,tm0.URL,pd.format,pd.des,tm1.censcon1,tm2.censcon2,tm3.censcon3,pd.riskIndex FROM member pd "
                + "LEFT JOIN (SELECT tt.id,tt.URL,tt.UPLOAD_DATE FROM (SELECT pr.id,res.URL,res.UPLOAD_DATE FROM member pr "
                + "LEFT JOIN t_test_member_to_resource t2p ON pr.id = t2p.PRODUCT_ID "
                + "LEFT JOIN t_test_resource res ON t2p.RESOURCE_ID = res.RESOURCE_ID "
                + "WHERE pr.organization = :organization ORDER BY res.UPLOAD_DATE DESC) tt GROUP BY tt.id ORDER BY tt.id DESC) tm0 ON pd.id = tm0.id "
                +

                "LEFT JOIN (SELECT pro.id,count(tr.id) 'censcon1' FROM member pro "
                + "LEFT JOIN member_instance pi ON pro.id = pi.member_id "
                + "LEFT JOIN test_result tr ON pi.id = tr.sample_id "
                + "where pro.organization = :organization AND tr.publish_flag = 1 and tr.test_type = '??' GROUP BY pro.id) tm1 ON pd.id = tm1.id "
                +

                "LEFT JOIN (SELECT pro.id,count(tr.id) 'censcon2' FROM member pro "
                + "LEFT JOIN member_instance pi ON pro.id = pi.member_id "
                + "LEFT JOIN test_result tr ON pi.id = tr.sample_id "
                + "where pro.organization = :organization AND tr.publish_flag = 1 and tr.test_type = '' GROUP BY pro.id) tm2 ON pd.id = tm2.id "
                +

                "LEFT JOIN (SELECT pro.id,count(tr.id) 'censcon3' FROM member pro "
                + "LEFT JOIN member_instance pi ON pro.id = pi.member_id "
                + "LEFT JOIN test_result tr ON pi.id = tr.sample_id "
                + "where pro.organization = :organization AND tr.publish_flag = 1 and tr.test_type = '?' GROUP BY pro.id) tm3 ON pd.id = tm3.id "
                +

                " where pd.organization = :organization ORDER BY pd.id DESC";
        Query query = entityManager.createNativeQuery(sql);
        if (page > 0 && pageSize > 0) {
            page = (page - 1) * pageSize;
            query.setFirstResult(page);
            query.setMaxResults(pageSize);
        }
        query.setParameter("organization", organization);
        List<Object[]> listMember = query.getResultList();
        //         return createDetailAlbumVO(listMember,cut);
        return null;
    } catch (Exception e) {
        throw new DaoException(e.getMessage(), e);
    }
}