Example usage for javax.persistence Query setMaxResults

List of usage examples for javax.persistence Query setMaxResults

Introduction

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

Prototype

Query setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:com.gettec.fsnip.fsn.dao.business.impl.BusinessUnitDAOImpl.java

/**
 * ???//from   ww  w  .  ja v a 2 s  . c o m
 * @author HuangYog
 * Create date 2015/04/13
 */
@SuppressWarnings("unchecked")
@Override
public Object getAllBusUnitName(Integer page, Integer pageSize, String keyword, String buType)
        throws DaoException {
    try {
        if (page - 1 < 0) {
            return null;
        }
        String sql = "SELECT DISTINCT name,id FROM business_unit WHERE name IS NOT NULL AND name LIKE '%"
                + keyword + "%' ";//LIMIT ?3,?4 ";
        Query query = entityManager.createNativeQuery(sql);
        if (page > 0 && pageSize > 0) {
            query.setFirstResult((page - 1) * pageSize);
            query.setMaxResults(pageSize);
        }
        List<Object[]> result = query.getResultList();
        List<BusinessNameVO> lists = null;
        if (result != null && result.size() > 0) {
            lists = new ArrayList<BusinessNameVO>();
            for (Object[] obj : result) {
                BusinessNameVO vo = new BusinessNameVO();
                vo.setId(obj[1] != null ? Long.valueOf(obj[1].toString()) : -1);
                vo.setName(obj[0] != null ? obj[0].toString() : "");
                lists.add(vo);
            }
        }
        return lists;
    } catch (Exception e) {
        throw new DaoException("BusinessUnitDAOImpl.getAllBusUnitName()-->" + e.getMessage(), e);
    }
}

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

/**
 * {@inheritDoc}/*from w ww  . j  a va 2s.  c  o  m*/
 */
@Override
public Page findUsers(User user, PaginationFilter paginationFilter) {
    Page page = new Page(paginationFilter);
    Query query = null;
    String jpql = "FROM User ";
    String condition = getConditionOnUser(user);

    if (!StringUtils.equals(condition, "")) {
        jpql = jpql + "WHERE " + condition;
    }

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

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

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

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

    page.setList(query.getResultList());

    return page;
}

From source file:org.opencms.db.jpa.CmsSubscriptionDriver.java

/**
 * @see org.opencms.db.I_CmsSubscriptionDriver#markResourceAsVisitedBy(org.opencms.db.CmsDbContext, java.lang.String, org.opencms.file.CmsResource, org.opencms.file.CmsUser)
 *///from ww  w .  j  a  va2  s  . c o m
public void markResourceAsVisitedBy(CmsDbContext dbc, String poolName, CmsResource resource, CmsUser user)
        throws CmsDataAccessException {

    boolean entryExists = false;
    CmsVisitEntryFilter filter = CmsVisitEntryFilter.ALL.filterResource(resource.getStructureId())
            .filterUser(user.getId());
    // delete existing visited entry for the resource
    if (readVisits(dbc, OpenCms.getSubscriptionManager().getPoolName(), filter).size() > 0) {
        entryExists = true;
        deleteVisits(dbc, OpenCms.getSubscriptionManager().getPoolName(), filter);
    }

    CmsVisitEntry entry = new CmsVisitEntry(user.getId(), System.currentTimeMillis(),
            resource.getStructureId());
    addVisit(dbc, poolName, entry);

    if (!entryExists) {
        // new entry, check if maximum number of stored visited resources is exceeded
        int count = 0;

        try {
            Query q = m_sqlManager.createQuery(dbc, dbc.currentProject(), C_VISITED_USER_COUNT_1);

            q.setParameter(1, user.getId().toString());
            count = ((Number) q.getSingleResult()).intValue();

            int maxCount = OpenCms.getSubscriptionManager().getMaxVisitedCount();
            if (count > maxCount) {
                // delete old visited entries
                q = m_sqlManager.createQuery(dbc, dbc.currentProject(), C_VISITED_USER_DELETE_GETDATE_2);

                q.setParameter(1, user.getId().toString());
                q.setMaxResults(count - maxCount);
                @SuppressWarnings("unchecked")
                List<Number> res = q.getResultList();
                long deleteDate = 0;
                for (Number n : res) {
                    // get last date of result set
                    deleteDate = n.longValue();
                }
                if (deleteDate > 0) {
                    filter = CmsVisitEntryFilter.ALL.filterUser(user.getId()).filterTo(deleteDate);
                    deleteVisits(dbc, OpenCms.getSubscriptionManager().getPoolName(), filter);
                }
            }
        } catch (PersistenceException e) {
            throw new CmsDbSqlException(
                    Messages.get().container(Messages.ERR_GENERIC_SQL_1, C_VISITED_USER_DELETE_GETDATE_2), e);
        }
    }
}

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

/**
 * ?//w  w  w .  ja v  a  2s  . co m
 * @return
 * @author ZhaWanNeng   2015/04/17
 */
@SuppressWarnings("unchecked")
public List<Member> getmemberList(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<Member> result = query.getResultList();
        return result;
    } catch (Exception e) {
        throw new DaoException("MemberDAOImpl.getmemberList()-->> ?", e);
    }
}

From source file:com.gettec.fsnip.fsn.dao.business.impl.BusinessUnitDAOImpl.java

/**
 * ??????????/*from w  ww.  j  a v a  2s. c  o  m*/
 * @param page 
 * @param pageSize ??
 * @param businessName ???
 * @param businessType ?
 * @param startDate ? 
 * @param endDate ??
 * @return List<BusinessStaVO>
 * @throws DaoException
 * @author LongXianZhen
 */
@SuppressWarnings("unchecked")
@Override
public List<BusinessStaVO> findBusinessIdByNameType(int page, int pageSize, String businessName,
        String businessType, String startDate, String endDate) throws DaoException {
    try {
        String new_configure = " WHERE organization IS NOT NULL AND organization!=0 ";
        //????????
        if (!businessName.equals("") && businessName != null) {
            new_configure = new_configure + " AND  e.name like " + "'%" + businessName + "%' ";
        }
        //????
        if (!businessType.equals("") && businessType != null) {
            new_configure = new_configure + " AND e.type like " + "'%" + businessType + "%' ";
        }
        ////????
        if (!startDate.equals("") && !endDate.equals("")) {
            endDate = DateUtil.addDays(endDate, 1);
            new_configure = new_configure + " AND e.enterpriteDate BETWEEN '" + startDate + "' AND '" + endDate
                    + "' ";
        }
        String jpql = "SELECT e.id,e.name,e.type,e.organization,enterpriteDate FROM business_unit e ";
        if (new_configure != null && !new_configure.equals("")) {
            jpql = jpql + " " + new_configure;
        }
        Query query = entityManager.createNativeQuery(jpql);
        if (page != 0 && pageSize != 0) {
            query.setFirstResult((page - 1) * pageSize);
            query.setMaxResults(pageSize);
        }
        List<Object[]> objs = query.getResultList();
        List<BusinessStaVO> businessSta = new ArrayList<BusinessStaVO>();
        for (Object[] obj : objs) {
            BusinessStaVO buSta = new BusinessStaVO();
            buSta.setBusinessId(Long.valueOf(obj[0].toString()));
            buSta.setBusinessName(obj[1] == null ? null : obj[1].toString());
            buSta.setBusinessType(obj[2] == null ? null : obj[2].toString());
            buSta.setOrganization(obj[3] == null ? null : Long.valueOf(obj[3].toString()));
            buSta.setEnterpriteDate(obj[4] == null ? null : (Date) obj[4]);
            businessSta.add(buSta);
        }
        return businessSta;
    } catch (Exception e) {
        throw new DaoException(
                "BusinessUnitDAOImpl.findBusinessIdByNameType()-->??????????",
                e);
    }
}

From source file:com.lhfs.fsn.dao.testReport.impl.TestReportDaoImpl.java

@SuppressWarnings("unchecked")
@Override/* www.  ja  v a2s .c o  m*/
public List<ProductInfoVO> getByLicenseNoProduct(String licenseNo, Integer page, Integer pageSize) {
    String sql = "SELECT p.id,p.name,p.barcode  FROM product  p ";
    sql += " LEFT JOIN business_unit bu ON bu.id=p.producer_id ";
    sql += " WHERE bu.license_no = ?1";
    Query query = entityManager.createNativeQuery(sql);
    query.setParameter(1, licenseNo);
    // ?
    if (page > 0) {
        query.setFirstResult((page - 1) * pageSize);
        query.setMaxResults(pageSize);
    }
    List<Object[]> objs = query.getResultList();
    List<ProductInfoVO> proList = new ArrayList<ProductInfoVO>();
    for (Object[] obj : objs) {
        ProductInfoVO vo = new ProductInfoVO();
        vo.setProductId(obj[0] == null ? null : Long.parseLong(obj[0].toString()));
        vo.setProductName(obj[1] == null ? null : obj[1].toString());
        vo.setBarcode(obj[2] == null ? null : obj[2].toString());
        proList.add(vo);
    }
    return proList;
}

From source file:com.lhfs.fsn.dao.testReport.impl.TestReportDaoImpl.java

/**
 * ???/*from ww  w.  j  a va  2 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.medici.bia.dao.user.UserDAOJpaImpl.java

/**
 * {@inheritDoc}//from  ww  w  .  ja v a 2s  . c  o m
 */
@SuppressWarnings("unchecked")
@Override
public Page findForumMembersByText(String text, PaginationFilter paginationFilter) {
    String queryString = "FROM User WHERE forumJoinedDate IS NOT NULL AND account LIKE '%" + text
            + "%' ORDER BY account";

    // We prepare object of return method.
    Page page = new Page(paginationFilter);
    Query query = null;

    // We set size of result.
    if (paginationFilter.getPageTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + queryString;

        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
        page.setTotalPages(PageUtils.calculeTotalPages(page.getTotal(), page.getElementsForPage()));
    } else {
        page.setTotal(paginationFilter.getTotal());
        page.setTotalPages(paginationFilter.getPageTotal());
    }

    query = getEntityManager().createQuery(queryString);

    // We set pagination  
    query.setFirstResult(PageUtils.calculeStart(page.getThisPage(), page.getElementsForPage()));
    query.setMaxResults(page.getElementsForPage());
    // We manage sorting (this manages sorting on multiple fields)
    List<User> list = (List<User>) query.getResultList();
    // We set search result on return method
    page.setList(list);

    return page;
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}/*from  ww w  .j  a v  a  2s .  c  om*/
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findNextHistoryCursor(User user, Integer idUserHistory) throws PersistenceException {
    // -- Next
    // SELECT * FROM tblUserHistory WHERE user.account='lpasquinelli' AND idUserHistory > 15 and entryId is not null ORDER BY idUserHistory ASC LIMIT 1
    StringBuilder queryString = new StringBuilder("FROM UserHistory WHERE user.account='");
    queryString.append(user.getAccount());
    queryString.append('\'');
    queryString.append(" AND (category NOT LIKE '");
    queryString.append(Category.FORUM.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.FORUM_TOPIC.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.FORUM_POST.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.MARKED_LIST.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_DOCUMENT.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_PEOPLE.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_PLACE.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_VOLUME.name());
    queryString.append("')");
    queryString.append(" AND idUserHistory > ");
    queryString.append(idUserHistory);

    queryString.append(" AND logicalDelete = false ORDER BY idUserHistory ASC LIMIT 1");

    Query query = getEntityManager().createQuery(queryString.toString());
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() > 0) {
        return result.get(0);
    }

    return null;
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}//from  w w w .  ja v a2  s.c  o  m
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findPreviousHistoryCursor(User user, Integer idUserHistory) throws PersistenceException {
    // -- Previous
    // SELECT * FROM tblUserHistory WHERE user.account='lpasquinelli' AND idUserHistory < 15 and entryId is not null ORDER BY idUserHistory DESC LIMIT 1
    StringBuilder queryString = new StringBuilder("FROM UserHistory WHERE user.account='");
    queryString.append(user.getAccount());
    queryString.append('\'');
    queryString.append(" AND (category NOT LIKE '");
    queryString.append(Category.FORUM.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.FORUM_TOPIC.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.FORUM_POST.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.MARKED_LIST.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_DOCUMENT.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_PEOPLE.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_PLACE.name());
    queryString.append("' AND category NOT LIKE '");
    queryString.append(Category.SEARCH_VOLUME.name());
    queryString.append("')");
    queryString.append(" AND idUserHistory < ");
    queryString.append(idUserHistory);

    queryString.append(" AND logicalDelete = false ORDER BY idUserHistory DESC LIMIT 1");

    Query query = getEntityManager().createQuery(queryString.toString());
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() > 0) {
        return result.get(0);
    }

    return null;
}