Example usage for org.hibernate.criterion Restrictions le

List of usage examples for org.hibernate.criterion Restrictions le

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions le.

Prototype

public static SimpleExpression le(String propertyName, Object value) 

Source Link

Document

Apply a "less than or equal" constraint to the named property

Usage

From source file:com.avectis.transportcontrol.DAO.CarHibernateDAO.java

/**
 * get Cars for period from DB using Hibernate
 * // ww  w  .j a v  a  2s. c  o  m
 * @param startDate Date - from date
 * @param endDate Date - to date
 * @return List of Car objects
 */
@Override
public List<Car> getCars(Date startDate, Date endDate) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Car.class);
    if (startDate != null) {
        criteria.add(Restrictions.ge("createDate", startDate));
    }
    if (endDate != null) {
        criteria.add(Restrictions.le("createDate", endDate));
    }
    criteria.addOrder(Order.asc("createDate"));
    return (List<Car>) criteria.list();
}

From source file:com.avectis.transportcontrol.DAO.CarHibernateDAO.java

/**
 * get Cars for period from DB using Hibernate
 * /*  w  w w. ja va  2  s .c  om*/
 * @param startDate Date - from date
 * @param endDate Date - to date
 * @param endDate String - filter on carNumber
 * @return List of Car objects
 */
@Override
public List<Car> getCars(Date startDate, Date endDate, String carNumber) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Car.class);
    if (startDate != null) {
        criteria.add(Restrictions.ge("createDate", startDate));
    }
    if (endDate != null) {
        criteria.add(Restrictions.le("createDate", endDate));
    }
    if (carNumber != null) {
        StringBuffer sb = new StringBuffer();
        sb.append("%").append(carNumber).append("%");
        criteria.add(Restrictions.like("carNumber", sb.toString()));
    }
    criteria.addOrder(Order.asc("createDate"));
    return (List<Car>) criteria.list();
}

From source file:com.Bean.PostinfoHelper.java

public List<Post> searchByCriteria(PostSearcher postSearcher, int page) {

    List<Post> postList = new ArrayList<Post>();
    try {/*w  ww .  j  a v  a2  s  .c  o m*/
        Criteria c = session.createCriteria(Post.class);
        if (postSearcher.getTitle() != null) {
            c.add(Restrictions.like("title", "%" + postSearcher.getTitle() + "%"));
        }
        if ((postSearcher.getMinPrice() >= 0) && (postSearcher.getMaxPrice() >= 0)) {
            c.add(Restrictions.between("price", postSearcher.getMinPrice(), postSearcher.getMaxPrice()));
        } else if (postSearcher.getMinPrice() >= 0) {
            c.add(Restrictions.ge("price", postSearcher.getMinPrice()));
        } else if (postSearcher.getMaxPrice() >= 0) {
            c.add(Restrictions.le("price", postSearcher.getMaxPrice()));
        }
        if (postSearcher.getTypes() != null) {
            c.add(Restrictions.in("type", postSearcher.getTypes()));
        }
        maxPage = c.list().size() / itemsPerPage;
        c.setFirstResult(page * itemsPerPage);
        c.setMaxResults(itemsPerPage);
        postList = (List<Post>) c.list();
        return postList;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.bloatit.data.queries.DaoAbstractQuery.java

License:Open Source License

/**
 * Creates a criterion on a {@link Comparable} element.
 * <p>/*from w ww.  ja va  2 s  .c  o  m*/
 * For example: <code>
 * createNbRestriction(GREATER, &quot;amount&quot;, 12)
 * </code> Will select elements with amount > 12.
 * </p>
 * 
 * @param cmp the comparator
 * @param element the countable element name.
 * @param nb the number on which we try to compare the <code>element</code>
 *            value.
 * @return the criterion
 */
protected final Criterion createNbCriterion(final Comparator cmp, final String element, final Object nb) {
    switch (cmp) {
    case EQUAL:
        return Restrictions.eq(element, nb);
    case LESS:
        return Restrictions.lt(element, nb);
    case LESS_EQUAL:
        return Restrictions.le(element, nb);
    case GREATER:
        return Restrictions.gt(element, nb);
    case GREATER_EQUAL:
        return Restrictions.ge(element, nb);
    default:
        return Restrictions.eq(element, nb);
    }
}

From source file:com.blue.ssh.core.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ??Criterion,./*from   w w  w. j a va2  s .  c om*/
 */
protected Criterion buildCriterion(final String propertyName, final Object propertyValue,
        final MatchType matchType) {
    Assert.hasText(propertyName, "propertyName?");
    Criterion criterion = null;
    // ?MatchTypecriterion
    switch (matchType) {
    case EQ:
        criterion = Restrictions.eq(propertyName, propertyValue);
        break;
    case LIKE:
        criterion = Restrictions.like(propertyName, (String) propertyValue, MatchMode.ANYWHERE);
        break;

    case LE:
        criterion = Restrictions.le(propertyName, propertyValue);
        break;
    case LT:
        criterion = Restrictions.lt(propertyName, propertyValue);
        break;
    case GE:
        criterion = Restrictions.ge(propertyName, propertyValue);
        break;
    case GT:
        criterion = Restrictions.gt(propertyName, propertyValue);
    case NE:
        criterion = Restrictions.ne(propertyName, propertyValue);
    }
    return criterion;
}

From source file:com.bookselling.dao.SellerInvoiceDaoImpl.java

private Object[] filterCriteria(SellerInvoiceFilterForm form, int first, int items, int id) {
    String keyword = form.getKeyword();
    SellerInvoiceFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    Double fromPrice = form.getFromPrice();
    Double toPrice = form.getToPrice();
    SellerInvoiceOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    Criteria criteria = getSession().createCriteria(SellerInvoice.class);
    criteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc");

    if (keyword != null) {
        keyword = "%" + keyword + "%";
        if (searchBy == SellerInvoiceFilterType.BUYER)
            criteria.add(Restrictions.like("acc.username", keyword));
        else if (searchBy == SellerInvoiceFilterType.OWNER) {
            Name name = new Name();
            name.setName(keyword);//  www.  j a  v a  2 s  . c om
            criteria.add(Restrictions.like("bye.name", name));
        } else if (searchBy == SellerInvoiceFilterType.ADDRESS) {
            Address address = new Address();
            address.setAddress(keyword);
            criteria.add(Restrictions.like("contact.address", address));
        } else if (searchBy == SellerInvoiceFilterType.PHONE) {
            PhoneNumber phone = new PhoneNumber();
            phone.setPhoneNumber(keyword);
            criteria.add(Restrictions.like("contact.phoneNumber", phone));
        }
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("createdDate", toDate));

    if (fromPrice != null)
        criteria.add(Restrictions.ge("totalPrice", fromPrice));
    if (toPrice != null)
        criteria.add(Restrictions.le("totalPrice", toPrice));

    String propertyName = null;
    if (orderBy == SellerInvoiceOrderType.BUYER)
        propertyName = "acc.username";
    else if (orderBy == SellerInvoiceOrderType.OWNER)
        propertyName = "bye.name";
    else if (orderBy == SellerInvoiceOrderType.DATE)
        propertyName = "createdDate";
    else if (orderBy == SellerInvoiceOrderType.PRICE)
        propertyName = "totalPrice";

    if (id != -1)
        criteria.add(Restrictions.eq("sl.id", id));

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellerInvoice.class);
    subCriteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    return new Object[] { subCriteria, rowCount };
}

From source file:com.bookselling.dao.SystemInvoiceDaoImpl.java

@Override
public PaginationData<SystemInvoice> filter(SystemInvoiceFilterForm form, int first, int items) {
    String keyword = form.getKeyword();
    SystemInvoiceFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    SystemInvoiceOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    Criteria criteria = getSession().createCriteria(SystemInvoice.class);
    criteria.createAlias("poster", "pst").createAlias("post", "ps").createAlias("pst.account", "acc");

    if (keyword == null) {
        keyword = "%" + keyword + "%";
        if (searchBy == SystemInvoiceFilterType.ACCOUNT)
            criteria.add(Restrictions.like("acc.username", keyword));
        else if (searchBy == SystemInvoiceFilterType.POSTER) {
            Name name = new Name();
            name.setName(keyword);/*from w  ww.java  2 s. c  om*/
            criteria.add(Restrictions.like("pst.name", name));
        } else if (searchBy == SystemInvoiceFilterType.POST_HEADER)
            criteria.add(Restrictions.like("ps.header", keyword));
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("createdDate", toDate));

    String propertyName = null;
    if (orderBy == SystemInvoiceOrderType.ACCOUNT)
        propertyName = "acc.username";
    else if (orderBy == SystemInvoiceOrderType.POSTER)
        propertyName = "pst.name";
    else if (orderBy == SystemInvoiceOrderType.POST_HEADER)
        propertyName = "ps.header";
    else if (orderBy == SystemInvoiceOrderType.DATE)
        propertyName = "createdDate";

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SystemInvoice.class);
    subCriteria.createAlias("poster", "pst").createAlias("post", "ps").createAlias("pst.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    Set<SystemInvoice> invoices = new LinkedHashSet<>(subCriteria.list());
    HibernateInitSupport.setCls(SystemInvoice.class);
    for (SystemInvoice invoice : invoices)
        HibernateInitSupport.initDomain(invoice);

    PaginationData paginationData = new PaginationData(rowCount, items, first, invoices);

    return paginationData;
}

From source file:com.bookselling.dao.TradeDaoImpl.java

private Object[] filterCriteria(TradeFilterForm form, int first, int items, int id) {
    String keyword = form.getKeyword();
    TradeFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    Double fromPrice = form.getFromPrice();
    Double toPrice = form.getToPrice();
    TradeOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    Criteria criteria = getSession().createCriteria(Trade.class);
    criteria.createAlias("buyer", "bye").createAlias("bye.account", "acc");

    if (keyword != null) {
        keyword = "%" + keyword + "%";
        if (searchBy == TradeFilterType.ADDRESS) {
            Address address = new Address();
            address.setAddress(keyword);
            criteria.add(Restrictions.like("contact.address", address));
        } else if (searchBy == TradeFilterType.PHONE) {
            PhoneNumber phone = new PhoneNumber();
            phone.setPhoneNumber(keyword);
            criteria.add(Restrictions.like("contact.phoneNumber", phone));
        }//from www . j a  v  a  2 s .  c o m
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("createdDate", toDate));

    if (fromPrice != null)
        criteria.add(Restrictions.ge("totalPrice", fromPrice));
    if (toPrice != null)
        criteria.add(Restrictions.le("totalPrice", toPrice));

    String propertyName = null;
    if (orderBy == TradeOrderType.BUYER)
        propertyName = "acc.username";
    else if (orderBy == TradeOrderType.OWNER)
        propertyName = "bye.name";
    else if (orderBy == TradeOrderType.DATE)
        propertyName = "createdDate";
    else if (orderBy == TradeOrderType.PRICE)
        propertyName = "totalPrice";

    if (id != -1)
        criteria.add(Restrictions.eq("bye.id", id));

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(Trade.class);
    subCriteria.createAlias("buyer", "bye").createAlias("bye.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    return new Object[] { subCriteria, rowCount };
}

From source file:com.bookselling.dao.UserDaoImpl.java

@Override
public PaginationData filter(UserFilterForm form, int first, int items) {
    Criteria criteria = getSession().createCriteria(User.class);

    //Get form data
    String keyword = form.getKeyword();
    AccountStatus[] accStatus = form.getAccStatus();
    UserFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    UserOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    //To criteria
    criteria.createAlias("account", "acc").createAlias("acc.role", "rls").add(Restrictions.eq("rls.id", 1));

    if (keyword != null && !keyword.isEmpty()) {
        keyword = "%" + keyword + "%";
        if (searchBy == UserFilterType.ADDRESS) {
            Address address = new Address();
            address.setAddress(keyword);
            criteria.add(Restrictions.like("contact.address", address));
        } else if (searchBy == UserFilterType.EMAIL) {
            criteria.add(Restrictions.like("acc.email", keyword));
        } else if (searchBy == UserFilterType.OWNER) {
            Name name = new Name();
            name.setName(keyword);/*from   www .j av  a 2  s.  c  o m*/
            criteria.add(Restrictions.like("name", name));
        } else if (searchBy == UserFilterType.PHONE) {
            PhoneNumber phone = new PhoneNumber();
            phone.setPhoneNumber(keyword);
            criteria.add(Restrictions.like("contact.phone", phone));
        } else if (searchBy == UserFilterType.USERNAME) {
            criteria.add(Restrictions.like("acc.username", keyword));
        }
    }

    if (accStatus.length != 0) {
        criteria.add(Restrictions.in("acc.status", accStatus));
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("acc.createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("acc.createdDate", toDate));

    String propertyName = null;
    if (orderBy == UserOrderType.CREATEDDATE)
        propertyName = "acc.createdDate";
    else if (orderBy == UserOrderType.NAME)
        propertyName = "name";
    else if (orderBy == UserOrderType.STATUS)
        propertyName = "acc.status";
    else if (orderBy == UserOrderType.USERNAME)
        propertyName = "acc.username";

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(User.class);
    subCriteria.createAlias("account", "acc").createAlias("acc.role", "rls").add(Restrictions.eq("rls.id", 1))
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    //get list
    Set<User> users = new LinkedHashSet<>(subCriteria.list());
    for (User user : users) {
        HibernateInitSupport.initUser(user);
    }

    //Pagination
    PaginationData paginationData = new PaginationData(rowCount, items, first, users);

    return paginationData;
}

From source file:com.cai310.lottery.service.lottery.keno.impl.KenoServiceImpl.java

/**
 * ?//from w w  w  . j av a2 s .c  o m
 * 
 * @param dateStar
 *            
 * @param dateEnd
 *            ?
 * @param dateMin
 *            ??action
 * @return ???
 */
public void oprIssueTime(Date dateStar, Date dateEnd, Integer dateMin) {
    if (null == dateStar)
        throw new ServiceException("");
    if (null == dateEnd)
        throw new ServiceException("?");
    if (null == dateMin)
        throw new ServiceException("?.");
    Class<S> clazz = ReflectionUtils.getSuperClassGenricType(getClass(), 0);
    DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
    criteria.add(Restrictions.ge("endedTime", dateStar));
    criteria.add(Restrictions.le("endedTime", dateEnd));
    criteria.addOrder(Order.asc("endedTime"));
    List<I> resultList = schemeDao.findByDetachedCriteria(criteria, true);
    if (resultList != null && resultList.size() > 0) {
        for (I issue : resultList) {
            issue.setStartTime(DateUtils.addMinutes(issue.getStartTime(), dateMin));
            issue.setEndedTime(DateUtils.addMinutes(issue.getEndedTime(), dateMin));
            issue.setPrizeTime(DateUtils.addMinutes(issue.getPrizeTime(), dateMin));
        }
    } else {
        if (null == dateMin)
            throw new ServiceException("?.");
    }
}