Example usage for org.hibernate Criteria setMaxResults

List of usage examples for org.hibernate Criteria setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Criteria setMaxResults.

Prototype

public Criteria setMaxResults(int maxResults);

Source Link

Document

Set a limit upon the number of objects to be retrieved.

Usage

From source file:com.mycompany.thymeleafspringapp.dao.UsersDAOImpl.java

@Override
public Users getUserById(long id) {
    Criteria crit = sessionFactory.openSession().createCriteria(Users.class);
    crit.add(Restrictions.eq("userId", id));
    crit.setMaxResults(1);
    return (Users) crit.list().get(0);
}

From source file:com.npower.dm.hibernate.management.SoftwareTopListBean4TopList.java

License:Open Source License

private Criteria prepareCriteria(Class<?> clazz, SoftwareCategory category, SoftwareTrackingType trackingEvent,
        int maxRecords) throws DMException {
    ManagementBeanFactory factory = this.getManagementBeanFactory();
    SearchBean bean = factory.createSearchBean();
    Criteria criteria = bean.newCriteriaInstance(clazz);
    String trackingType = trackingEvent.toString();
    criteria.add(Expression.eq("trackingType", trackingType));
    criteria.setMaxResults(maxRecords);
    criteria.addOrder(Order.desc("count"));
    Criteria criteria4Software = criteria.createCriteria("software");
    Criteria criteria4Categories = criteria4Software.createCriteria("softwareCategoriesSet");
    if (category != null) {
        Set<SoftwareCategory> targetCategories = this.getTargetCategories(category);
        criteria4Categories.add(Expression.in("id.category", targetCategories));
    }/*  www . j  av a  2s.co  m*/
    return criteria;
}

From source file:com.onecheckoutV1.scheduler.SchedulerTransactionOcoBean.java

public void startSchedulerDaily() {
    PropertiesConfiguration config = OneCheckoutProperties.getOneCheckoutConfig();
    String on = config.getString("TRANSACTION.OCO.SCHEDULER.", "TRUE");

    if (on.equalsIgnoreCase("FALSE")) {
        OneCheckoutLogger.log("OneCheckoutV1 - scheduller of Transaction OCO disabled");
        return;// www  .  j  a  v a2s .c  o  m
    }

    int maxrows = config.getInt("NOTIFY.STATUS.ROWS", 50);
    Calendar max = Calendar.getInstance();
    max.add(Calendar.HOUR, -24);
    Date executeTime = max.getTime();

    Criteria criterias = getSession().createCriteria(Transactions.class);
    criterias.createAlias("merchantPaymentChannel", "mpc");
    criterias.createAlias("mpc.merchants", "m");
    criterias.createAlias("mpc.paymentChannel", "pc");
    criterias.add(Restrictions.le("incRequestdatetime", executeTime));
    criterias.addOrder(Order.desc("incRequestdatetime"));
    criterias.setMaxResults(maxrows);

    OneCheckoutLogger.log("TRANSACTION.beforeToday.date" + executeTime + "]");

    List<Transactions> trxListOco = (List<Transactions>) criterias.list();

    if (trxListOco == null || trxListOco.isEmpty()) {
        OneCheckoutLogger.log("SchedulerTransactionOcoBean.startSchedulerExecutor : 0 transactions");
        return;
    }

    for (Transactions transactions : trxListOco) {
        if (StringUtils.isEmpty(transactions.getDokuResponseCode())
                && transactions.getTransactionsStatus() == null) {
            OneCheckoutErrorMessage failedMsg = OneCheckoutErrorMessage.UNKNOWN;

            String logPrefix = "TransactionOcoScheduler_"
                    + transactions.getMerchantPaymentChannel().getMerchants().getMerchantCode() + "_"
                    + transactions.getIncTransidmerchant();
            SecurityAssociation.setPrincipal(new OneCheckoutPrincipal(logPrefix));

            failedMsg = OneCheckoutErrorMessage.FAILED_TRANSACTION_VIA_SCHEDULER;
            transactions.setDokuResponseCode(failedMsg.value());
            transactions.setTransactionsStatus(OneCheckoutTransactionStatus.FAILED.value());
            oneCheckoutV1QueryHelperBeanLocal.updateTransactions(transactions);

            OneCheckoutLogger.log("SchedulerTransactionOcoBean : update finished trx : ");
        }
    }
    SecurityAssociation.clear();
}

From source file:com.overhead.markeugie.manager.AccountsManager.java

@Override
public Accounts getByName(String username) throws RemoteException {

    Accounts loginAccount = null;/*www. j a  v  a  2s.c om*/
    Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(Accounts.class);
    criteria.add(Restrictions.eq("username", username));
    criteria.setMaxResults(1);
    loginAccount = (Accounts) criteria.uniqueResult();
    session.getTransaction().commit();
    session.close();

    return loginAccount;
}

From source file:com.overhead.markeugie.manager.AttendanceLogManager.java

@Override
public AttendanceLog getLogByParticipant(Participants participant, Date date) throws RemoteException {

    Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from   www  .  ja  v a2 s  . c  o  m
    Criteria criteria = session.createCriteria(AttendanceLog.class);
    criteria.add(Restrictions.eq("date", date));
    criteria.add(Restrictions.eq("participant", participant));
    criteria.setMaxResults(1);
    AttendanceLog participantLog = (AttendanceLog) criteria.uniqueResult();
    session.getTransaction().commit();
    session.close();

    return participantLog;
}

From source file:com.overhead.markeugie.manager.ParticipantManager.java

@Override
public Participants getById(String id) throws RemoteException {

    Participants participant = null;/*from  www. ja  v  a2 s  .  co m*/
    Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(Participants.class);
    criteria.add(Restrictions.eq("id", id));
    criteria.setMaxResults(1);
    participant = (Participants) criteria.uniqueResult();
    session.getTransaction().commit();
    session.close();
    return participant;
}

From source file:com.overhead.markeugie.manager.ParticipantManager.java

@Override
public Participants getByFirstNameAndLastName(String firstName, String lastName) throws RemoteException {

    Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from w  w  w .  j a  va 2 s  . c  om
    Criteria criteria = session.createCriteria(Participants.class);
    criteria.add(Restrictions.eq("firstName", firstName));
    criteria.add(Restrictions.eq("lastName", lastName));
    criteria.setMaxResults(1);
    Participants participant = (Participants) criteria.uniqueResult();
    session.getTransaction().commit();
    session.close();

    return participant;
}

From source file:com.overhead.markeugie.manager.ParticipantManager.java

@Override
public int getMaxId() throws RemoteException {
    int id = 0;//from w w  w  .  j ava 2s  .  c o m
    Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(Participants.class);
    criteria.setProjection(Projections.max("id"));
    criteria.setMaxResults(1);
    String lastId = (String) criteria.uniqueResult();
    if (lastId == null || lastId.equals("")) {
        id = 0;
    } else {
        id = Integer.parseInt(lastId);
    }
    session.getTransaction().commit();
    session.close();

    return id;
}

From source file:com.ponysdk.hibernate.dao.HibernateDAO.java

License:Apache License

@SuppressWarnings("unchecked")
public <T> List<T> findAll(final Class<T> _class, final int maxResults, final int firstResult) {
    final Criteria criteria = createCriteria(_class);
    return criteria.setMaxResults(maxResults).setFirstResult(firstResult).list();
}

From source file:com.project.framework.dao.BaseDao.java

License:Apache License

/**
 * ?Criteria,.// w w  w .  j  a  v a2  s.  c  o m
 */
protected Criteria setPageParameter(final Criteria c, final Page<T> page) {
    if (page.getStartIndex() > Integer.MAX_VALUE)
        throw new ClassCastException("Hibernate can not support startIndex Greater than Integer.Max");

    //hibernatefirstResult??0
    c.setFirstResult((int) page.getStartIndex());
    c.setMaxResults(page.getPageSize());

    //order by ?
    if (page.isOrder()) {
        Map<String, String> orderMap = page.getOrderMap();
        Iterator<String> iterator = orderMap.keySet().iterator();
        while (iterator.hasNext()) {

            String propertyName = iterator.next();
            if (orderMap.get(propertyName).equals(Page.ORDER_ASC)) {
                c.addOrder(Order.asc(propertyName));
            } else {
                c.addOrder(Order.desc(propertyName));
            }
        }
    }
    return c;
}