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.sun.socialsite.business.impl.JPANotificationManagerImpl.java

/**
 * Get notifications for a group//from w  w  w.  j  a va2s .  c o m
 *
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<MessageContent> getNotificationsByGroup(int offset, int length, String handle)
        throws SocialSiteException {

    GroupManager gmgr = Factory.getSocialSite().getGroupManager();
    Group grp = gmgr.getGroupByHandle(handle);
    if (grp == null)
        throw new SocialSiteException("No group found with handle " + handle);
    Query query = strategy.getNamedQuery("MessageContent.getGroupNotification");
    query.setParameter(1, grp);

    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<MessageContent>) query.getResultList();
}

From source file:org.apache.roller.planet.business.jpa.JPAPlanetManagerImpl.java

public List getEntries(Subscription sub, int offset, int len) throws PlanetException {
    if (sub == null) {
        throw new PlanetException("subscription cannot be null");
    }//from   w w  w.j a  v a 2 s.c o m
    Query q = strategy.getNamedQuery("SubscriptionEntry.getBySubscription");
    q.setParameter(1, sub);
    if (offset != 0)
        q.setFirstResult(offset);
    if (len != -1)
        q.setMaxResults(len);
    return q.getResultList();
}

From source file:com.sun.socialsite.business.impl.JPANotificationManagerImpl.java

/**
 * Get notifications for user's groups/*from   w  w w .j  a  va2  s.  c  om*/
 *
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<MessageContent> getGroupNotificationsForUser(int offset, int length, Profile user)
        throws SocialSiteException {

    Query query = strategy.getNamedQuery("MessageContent.getGroupNotificationsForUser");
    query.setParameter(1, user);

    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<MessageContent>) query.getResultList();
}

From source file:com.sun.socialsite.business.impl.JPAProfileManagerImpl.java

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *//*w w  w . j  a v a2s  . c  o  m*/
@SuppressWarnings(value = "unchecked")
public List<Profile> getMostRecentlyUpdatedProfiles(int offset, int length) throws SocialSiteException {
    Query query = strategy.getNamedQuery("Profile.getMostRecentlyUpdated");
    if (offset != 0)
        query.setFirstResult(offset);
    if (length != -1)
        query.setMaxResults(length);
    return (List<Profile>) query.getResultList();
}

From source file:com.sun.socialsite.business.impl.JPAPermissionManagerImpl.java

/**
 * {@inheritDoc}//from   ww w . j a  v  a2s  .  c om
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<PermissionGrant> getPermissionGrants(App app, int offset, int length) throws SocialSiteException {
    log.debug("app=" + app);
    Query query = strategy.getNamedQuery("PermissionGrant.getByApp");
    query.setParameter(1, app);
    if (offset != 0)
        query.setFirstResult(offset);
    if (length != -1)
        query.setMaxResults(length);
    return (List<PermissionGrant>) query.getResultList();
}

From source file:BO.UserHandler.java

public boolean login(VUser u) {

    EntityManager em;/*w ww  .  ja  va  2s  .  c o  m*/
    EntityManagerFactory emf;
    emf = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
    em = emf.createEntityManager();

    try {

        Query q = em.createQuery("SELECT u FROM User u WHERE u.email LIKE :email");
        q.setParameter("email", u.getEmail());
        q.setMaxResults(1);
        q.getSingleResult();
        return true;
    } catch (NoResultException e) {
        em.getTransaction().begin();
        User userToInsert = new User(u.getEmail());
        em.persist(userToInsert);
        em.flush();
        em.getTransaction().commit();
        return true;
    } finally {
        if (em != null) {
            em.close();
        }
        emf.close();
    }
}

From source file:com.dbs.sdwt.jpa.JpaUtil.java

public void applyPagination(Query query, SearchParameters sp) {
    if (sp.getFirst() > 0) {
        query.setFirstResult(sp.getFirst());
    }//w  ww  .ja  v a  2 s .  c om
    if (sp.getPageSize() > 0) {
        query.setMaxResults(sp.getPageSize());
    } else if (sp.getMaxResults() > 0) {
        query.setMaxResults(sp.getMaxResults());
    }
}

From source file:es.ucm.fdi.dalgs.degree.repository.DegreeRepository.java

@SuppressWarnings("unchecked")
public List<Degree> getDegrees(Integer pageIndex, Boolean showAll) {
    Query query = null;

    if (showAll)/*from  w  w w . j  a  v  a  2  s  .c om*/
        query = em.createQuery("select a from Degree a  order by a.id DESC");
    else
        query = em.createQuery("select a from Degree a  where a.isDeleted='false' order by a.id DESC");

    if (query.getResultList().isEmpty())
        return null;

    return query.setMaxResults(noOfRecords).setFirstResult(pageIndex * noOfRecords).getResultList();

}

From source file:fr.mby.opa.picsimpl.dao.DbPictureDao.java

@Override
public List<Picture> findPicturesByAlbumId(final Long albumId, final Long since) {
    Assert.notNull(albumId, "Album Id should be supplied !");
    final Timestamp sinceTimstamp = new Timestamp((since != null) ? since : 0L);

    final EmCallback<List<Picture>> emCallback = new EmCallback<List<Picture>>(this.getEmf()) {

        @Override//from  w  w  w  .  ja  va  2 s.c  o  m
        @SuppressWarnings("unchecked")
        protected List<Picture> executeWithEntityManager(final EntityManager em) throws PersistenceException {
            final Query findByAlbumQuery = em.createNamedQuery(Picture.FIND_PICTURE_BY_ALBUM_ORDER_BY_DATE);
            findByAlbumQuery.setParameter("albumId", albumId);
            findByAlbumQuery.setParameter("since", sinceTimstamp);
            findByAlbumQuery.setMaxResults(DbPictureDao.PAGINATION_SIZE);

            final List<Picture> pictureId = findByAlbumQuery.getResultList();
            return pictureId;
        }
    };

    List<Picture> pictures = emCallback.getReturnedValue();
    if (pictures == null) {
        pictures = Collections.emptyList();
    }

    return pictures;
}

From source file:com.bizintelapps.bugtracker.dao.impl.GenericDaoImpl.java

private List<T> executeNamedQueryReturnList(final String namedQuery, final PagingParams pagingParams,
        final Object... params) throws DataAccessException {
    Query query = entityManager.createNamedQuery(namedQuery);
    int i = 1;/* w  w  w  .j  av a2s  .  c  o m*/
    for (Object param : params) {
        query.setParameter(i++, param);
    }
    if (pagingParams != null) {
        query.setFirstResult((int) pagingParams.getStart());
        query.setMaxResults(pagingParams.getMaxLimit());
    }
    try {
        List result = query.getResultList();
        return result;
    } catch (RuntimeException re) {
        log.debug(re);
        return null;
    }
}