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.sct.descubriendoturuta.service.UsuarioServiceImpl.java

@Override
@Transactional/*from   ww  w . j  a  v  a2 s.co m*/
public Usuario buscarFacebookId(String facebookId) {

    Query query = em.createQuery("select u from Usuario u Where u.facebookId = :fid order by u.id");
    query.setMaxResults(1);
    query.setParameter("fid", facebookId);

    List<Usuario> r = query.getResultList();

    return r.isEmpty() ? null : r.get(0);
}

From source file:org.syncope.core.persistence.dao.impl.ReportDAOImpl.java

@Override
public List<Report> findAll(final int page, final int itemsPerPage) {
    final Query query = entityManager.createQuery("SELECT e FROM " + Report.class.getSimpleName() + " e");

    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));

    if (itemsPerPage > 0) {
        query.setMaxResults(itemsPerPage);
    }/* ww  w . ja v a 2  s  .c  om*/

    return query.getResultList();
}

From source file:es.caib.sgtsic.ejb3.AbstractFacade.java

public List<T> findRange(int[] range) {
    javax.persistence.Query q = getEntityManager()
            .createQuery("select object(o) from " + entityClass.getSimpleName() + " as o");
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);/*from w  w  w  . j a  v  a 2s  .  c o m*/
    return q.getResultList();
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketDAOJpa.java

public List<Ticket> getTicketsByStatus(final TicketStatus ticketStatus, final int howMuch) throws DAOException {
    try {//from  w  ww.  j  a v  a  2s .  c o m
        return this.jpaTemplate.executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager em) throws PersistenceException {
                Query query = em.createNamedQuery("Ticket.byStatusOrderByCreateDateDESC");
                query.setParameter(1, ticketStatus.toInt());
                query.setMaxResults(howMuch);
                return query.getResultList();
            }
        });
    } catch (DataAccessException dae) {
        throw new DAOException(dae);
    }
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPAGenre.java

@Override
@Transactional/*from ww w .j  a v a2  s .c  o  m*/
public List<MDOGenre> getRandomGenre(String username, int number) {
    Query query = entityManager
            .createQuery("from MDOGenre as a where (1=1 AND a.owner.login= :userName) order by rand()");
    query.setParameter("userName", username);
    query.setMaxResults(5);
    @SuppressWarnings("unchecked")
    List<MDOGenre> results = query.getResultList();
    return results;
}

From source file:org.eurekastreams.server.persistence.JobMapper.java

/**
 * Look up job titles starting with the given prefix.
 *
 * @param prefix//w ww.ja va 2 s  .c o  m
 *            the title prefix to search for
 * @param limit
 *            the maximum number of titles to return
 * @return the list of titles
 */
@SuppressWarnings("unchecked")
public List<String> findTitlesByPrefix(final String prefix, final int limit) {
    String searchParam = prefix.toLowerCase() + "%";
    Query query = getEntityManager().createQuery(
            "select distinct(title) from Job j where lower(j.title) like :prefix " + "order by title");
    query.setMaxResults(limit);
    query.setParameter("prefix", searchParam);

    return query.getResultList();
}

From source file:com.ny.apps.dao.BaseDaoImpl.java

@Override
public List<?> find(int start, int delta, String queryStr, Object... params) {
    Query query = prepareParameterizedQuery(entityManager.createQuery(queryStr), params);
    query.setFirstResult(start);/*  w w  w .ja va 2 s .  c om*/
    query.setMaxResults(delta);
    List<?> list = query.getResultList();
    return list;
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPAAuthor.java

@Override
@Transactional//from   w  w w  . ja v a2s.  co  m
public List<MDOAuthor> getRandomAuthors(String username, int number) {
    Query query = entityManager
            .createQuery("from MDOAuthor as a where (1=1 AND a.owner.login= :userName) order by rand()");
    query.setParameter("userName", username);
    query.setMaxResults(5);
    @SuppressWarnings("unchecked")
    List<MDOAuthor> results = query.getResultList();
    return results;
}

From source file:me.doshou.admin.monitor.web.controller.SQLExecutorController.java

@PageableDefaults(pageNumber = 0, value = 10)
@RequestMapping(value = "/sql", method = RequestMethod.POST)
public String executeQL(final @RequestParam("sql") String sql, final Model model, final Pageable pageable) {

    model.addAttribute("sessionFactory", HibernateUtils.getSessionFactory(em));

    String lowerCaseSQL = sql.trim().toLowerCase();
    final boolean isDML = lowerCaseSQL.startsWith("insert") || lowerCaseSQL.startsWith("update")
            || lowerCaseSQL.startsWith("delete");
    final boolean isDQL = lowerCaseSQL.startsWith("select");

    if (!isDML && !isDQL) {
        model.addAttribute(Constants.ERROR,
                "SQL????insert?update?delete?select");
        return showSQLForm();
    }/*from   w w  w .  j av  a  2  s.c o m*/
    try {
        new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {

                if (isDML) {
                    Query query = em.createNativeQuery(sql);
                    int updateCount = query.executeUpdate();
                    model.addAttribute("updateCount", updateCount);
                } else {
                    String findSQL = sql;
                    String countSQL = "select count(*) count from (" + findSQL + ") o";
                    Query countQuery = em.createNativeQuery(countSQL);
                    Query findQuery = em.createNativeQuery(findSQL);
                    findQuery.setFirstResult(pageable.getOffset());
                    findQuery.setMaxResults(pageable.getPageSize());

                    Page page = new PageImpl(findQuery.getResultList(), pageable,
                            ((BigInteger) countQuery.getSingleResult()).longValue());

                    model.addAttribute("resultPage", page);

                    em.unwrap(Session.class).doWork(new Work() {
                        @Override
                        public void execute(final Connection connection) throws SQLException {
                            PreparedStatement psst = connection.prepareStatement(sql);
                            ResultSetMetaData metaData = psst.getMetaData();

                            List<String> columnNames = Lists.newArrayList();
                            for (int i = 1, l = metaData.getColumnCount(); i <= l; i++) {
                                columnNames.add(metaData.getColumnLabel(i));
                            }
                            psst.close();
                            model.addAttribute("columnNames", columnNames);
                        }
                    });
                }

                return null;
            }
        });
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        model.addAttribute(Constants.ERROR, sw.toString());
    }

    return showSQLForm();
}

From source file:com.hack23.cia.service.data.impl.SearchIndexerImplITest.java

@Test
@Transactional(timeout = 900)/* ww  w  . ja v a 2  s  .c  o m*/
public void testSearchIndex() throws Exception {

    final QueryBuilder qb = getFullTextEntityManager().getSearchFactory().buildQueryBuilder()
            .forEntity(DocumentContentData.class).get();
    final org.apache.lucene.search.Query luceneQuery = qb.keyword().onFields("content")
            .matching("programmering").createQuery();

    // wrap Lucene query in a javax.persistence.Query
    final javax.persistence.Query jpaQuery = getFullTextEntityManager().createFullTextQuery(luceneQuery,
            DocumentContentData.class);

    // execute search
    final List<DocumentContentData> result = jpaQuery.setMaxResults(500).getResultList();
    assertTrue("expect some result", result.size() > 0);
}