Example usage for org.hibernate Criteria setFirstResult

List of usage examples for org.hibernate Criteria setFirstResult

Introduction

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

Prototype

public Criteria setFirstResult(int firstResult);

Source Link

Document

Set the first result to be retrieved.

Usage

From source file:com.sjm.financialapplication.dao.impl.AccountTransactionDAOImpl.java

@Override
public List<AccountTransaction> getAccountTransactionsById(int accountId, int start, int max) {
    Criteria crit = getCurrentSession().createCriteria(AccountTransaction.class);
    crit.add(Restrictions.eq("account.accountId", accountId));
    crit.addOrder(Order.desc("entry"));
    crit.setFirstResult(start);
    crit.setMaxResults(max);/* w  ww  .j a v a2  s .co  m*/
    return crit.list();
}

From source file:com.smartitengineering.dao.impl.hibernate.AbstractDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
private void processCriteria(Criteria criteria, String element, QueryParameter parameter) {
    switch (parameter.getParameterType()) {
    case PARAMETER_TYPE_PROPERTY: {
        criteria.add(getCriterion(element, parameter));
        return;//from  w  w w . j  ava  2  s  . com
    }
    case PARAMETER_TYPE_ORDER_BY: {
        final Order order;
        SimpleNameValueQueryParameter<com.smartitengineering.dao.common.queryparam.Order> queryParameter = QueryParameterCastHelper.SIMPLE_PARAM_HELPER
                .cast(parameter);
        com.smartitengineering.dao.common.queryparam.Order requestedOrder = queryParameter.getValue();
        switch (requestedOrder) {
        case ASC: {
            order = Order.asc(element);
            break;
        }
        case DESC: {
            order = Order.desc(element);
            break;
        }
        default: {
            order = null;
            break;
        }
        }
        if (order != null) {
            criteria.addOrder(order);
        }
        return;
    }
    case PARAMETER_TYPE_MAX_RESULT: {
        ValueOnlyQueryParameter<Integer> queryParameter = QueryParameterCastHelper.VALUE_PARAM_HELPER
                .cast(parameter);
        criteria.setMaxResults(queryParameter.getValue());
        return;
    }
    case PARAMETER_TYPE_FIRST_RESULT: {
        ValueOnlyQueryParameter<Integer> queryParameter = QueryParameterCastHelper.VALUE_PARAM_HELPER
                .cast(parameter);
        criteria.setFirstResult(queryParameter.getValue());
        return;
    }
    case PARAMETER_TYPE_DISJUNCTION: {
        processDisjunction(criteria, parameter);
        return;
    }
    case PARAMETER_TYPE_CONJUNCTION: {
        processConjunction(criteria, parameter);
        return;
    }
    case PARAMETER_TYPE_NESTED_PROPERTY: {
        processNestedParameter(criteria, element, parameter);
        return;
    }
    case PARAMETER_TYPE_COUNT: {
        final Projection countProjection = Projections.count(element);
        setProjection(criteria, countProjection);
        return;
    }
    case PARAMETER_TYPE_ROW_COUNT: {
        final Projection rowCount = Projections.rowCount();
        setProjection(criteria, rowCount);
        return;
    }
    case PARAMETER_TYPE_SUM: {
        final AggregateProjection sum = Projections.sum(element);
        setProjection(criteria, sum);
        return;
    }
    case PARAMETER_TYPE_MAX: {
        final AggregateProjection max = Projections.max(element);
        setProjection(criteria, max);
        return;
    }
    case PARAMETER_TYPE_MIN: {
        final AggregateProjection min = Projections.min(element);
        setProjection(criteria, min);
        return;
    }
    case PARAMETER_TYPE_AVG: {
        final AggregateProjection avg = Projections.avg(element);
        setProjection(criteria, avg);
        return;
    }
    case PARAMETER_TYPE_GROUP_BY: {
        final PropertyProjection groupProperty = Projections.groupProperty(element);
        setProjection(criteria, groupProperty);
        return;
    }
    case PARAMETER_TYPE_COUNT_DISTINCT: {
        final CountProjection countDistinct = Projections.countDistinct(element);
        setProjection(criteria, countDistinct);
        return;
    }
    case PARAMETER_TYPE_DISTINCT_PROP: {
        final Projection distinct = Projections.distinct(Projections.property(element));
        setProjection(criteria, distinct);
        return;
    }
    case PARAMETER_TYPE_UNIT_PROP: {
        final PropertyProjection property = Projections.property(element);
        setProjection(criteria, property);
        return;
    }
    }
}

From source file:com.socialsite.dao.hibernate.AbstractDaoImpl.java

License:Open Source License

/**
 * a helper method to find the list of item matching the filter
 * //w  w w . j  ava 2s .c  o m
 * @param filter
 *            filter text
 * @param first
 *            first index of the resul
 * @param count
 *            count
 * @param sortParam
 *            sorting is done based on this property
 * @param clazz
 *            class of the item
 * @param field
 *            field that will be matched with the criteria
 * @return list of item matching the filter text
 */
@SuppressWarnings("unchecked")
protected List find(String filter, final int first, final int count, final SortParam sortParam,
        final Class clazz, final String field) {
    // avoids the NPE
    filter = filter == null ? "" : filter;

    // order
    final Order order = sortParam.isAscending() ? Order.asc(sortParam.getProperty())
            : Order.desc(sortParam.getProperty());
    final Criteria criteria = getSession().createCriteria(clazz);
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);
    criteria.add(Restrictions.ilike(field, filter, MatchMode.ANYWHERE));
    criteria.addOrder(order);
    return criteria.list();
}

From source file:com.socialsite.dao.hibernate.AnswerDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Answer> getAnswers(final long questionId, final int first, final int count) {
    final Criteria criteria = getSession().createCriteria(domainClass);
    criteria.add(Restrictions.eq("question.id", questionId));
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);//from w w w  . j av  a 2  s .  c  o  m

    // order by date
    criteria.addOrder(Order.desc("time"));
    return criteria.list();
}

From source file:com.socialsite.dao.hibernate.CommentDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Comment> getComments(final long answerId, final int first, final int count) {
    final Criteria criteria = getSession().createCriteria(domainClass);
    criteria.add(Restrictions.eq("answer.id", answerId));
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);/*  w  ww  . j av a  2s.c  o m*/

    // order by date
    criteria.addOrder(Order.desc("time"));
    return criteria.list();

}

From source file:com.socialsite.dao.hibernate.QuestionDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Question> getQuestions(final long courseId, final int first, final int count) {
    final Criteria criteria = getSession().createCriteria(domainClass);
    criteria.add(Restrictions.eq("course.id", courseId));
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);/*w  w w  .  ja v  a  2s .  c om*/

    // order by date
    criteria.addOrder(Order.desc("time"));
    return criteria.list();
}

From source file:com.square.core.dao.implementations.ActionDureeDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w w w .j av  a 2s . co m
public List<ActionDuree> rechercherDureeActionParCriteres(DimensionCriteresRechercheDto criteres) {
    final Criteria criteria = createCriteria(ActionDuree.class);

    // Critre sur l'identifiant
    if (criteres.getId() != null) {
        criteria.add(Restrictions.eq("id", criteres.getId()));
    }

    // Critre sur le libell
    if (criteres.getLibelle() != null && !criteres.getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("libelle", criteres.getLibelle().toLowerCase() + "%"));
    }

    // Critre sur la visibilit
    if (criteres.getVisible() != null) {
        criteria.add(Restrictions.eq("visible", criteres.getVisible()));
    }
    // Maxresults
    if (criteres.getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getMaxResults());
    }

    criteria.addOrder(Order.asc("ordre"));

    return criteria.list();
}

From source file:com.square.core.dao.implementations.ActionNatureDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w ww  .  ja v a2  s  .c  om*/
public List<ActionNature> rechercherNatureActionParCritere(DimensionCriteresRechercheDto criteres) {

    final Criteria criteria = createCriteria(ActionNature.class);

    // Critre sur l'identifiant
    if (criteres.getId() != null) {
        criteria.add(Restrictions.eq("id", criteres.getId()));
    }
    // Critre sur le libell
    if (criteres.getLibelle() != null && !criteres.getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("libelle", criteres.getLibelle().toLowerCase() + "%"));
    }
    // Critre sur la visibilit
    if (criteres.getVisible() != null) {
        criteria.add(Restrictions.eq("visible", criteres.getVisible()));
    }
    // Maxresults
    if (criteres.getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getMaxResults());
    }
    criteria.addOrder(Order.asc("ordre"));
    criteria.addOrder(Order.asc("libelle"));

    return criteria.list();
}

From source file:com.square.core.dao.implementations.ActionNatureResultatDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w w  . j  a  va 2  s.c o m*/
public List<ActionNatureResultat> rechercherActionNatureResultatParCritere(
        ActionNatureResultatCriteresRechercheDto criteres) {
    final Criteria criteria = createCriteria(ActionNatureResultat.class);

    // Critre sur l'identifiant
    if (criteres.getId() != null) {
        criteria.add(Restrictions.eq("id", criteres.getId()));
    }
    // Critre sur le libell
    if (criteres.getLibelle() != null && !criteres.getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("libelle", criteres.getLibelle().toLowerCase() + "%"));
    }
    // Critre sur la visibilit
    if (criteres.getVisible() != null) {
        criteria.add(Restrictions.eq("visible", criteres.getVisible()));
    }
    // Maxresults
    if (criteres.getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getMaxResults());
    }
    // Critre sur l'identifiant de la nature d'action
    if (criteres.getIdActionNature() != null) {
        criteria.add(Restrictions.eq("nature.id", criteres.getIdActionNature()));
    }
    criteria.addOrder(Order.asc("ordre"));
    criteria.addOrder(Order.asc("libelle"));

    return criteria.list();
}

From source file:com.square.core.dao.implementations.ActionObjetDaoImplementation.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w  w. j av  a  2s  . co  m*/
public List<ActionObjet> rechercherObjetActionParCritere(DimensionCritereRechercheObjetDto criteres) {
    if (criteres.getDimensionCriteres() == null) {
        criteres.setDimensionCriteres(new DimensionCriteresRechercheDto());
    }
    final Criteria criteria = createCriteria(ActionObjet.class);

    // Critre sur l'identifiant
    if (criteres.getDimensionCriteres().getId() != null) {
        criteria.add(Restrictions.eq("id", criteres.getDimensionCriteres().getId()));
    }
    // Critre sur le libelle
    if (criteres.getDimensionCriteres().getLibelle() != null
            && !criteres.getDimensionCriteres().getLibelle().equals("")) {
        criteria.add(Restrictions.ilike("libelle",
                criteres.getDimensionCriteres().getLibelle().toLowerCase() + "%"));
    }
    // Critre sur la visibilit
    if (criteres.getDimensionCriteres().getVisible() != null) {
        criteria.add(Restrictions.eq("visible", criteres.getDimensionCriteres().getVisible()));
    }
    // Critre sur le type de l'objet.
    if (criteres.getIdType() != null) {
        criteria.createAlias("actionType", "actionType")
                .add(Restrictions.eq("actionType.id", criteres.getIdType()));
    }
    // Maxresults
    if (criteres.getDimensionCriteres().getMaxResults() != null) {
        criteria.setFirstResult(0);
        criteria.setMaxResults(criteres.getDimensionCriteres().getMaxResults());
    }
    // Ordonner les lments
    criteria.addOrder(Order.asc("ordre"));
    criteria.addOrder(Order.asc("libelle"));

    return criteria.list();
}