Example usage for org.hibernate Query setFirstResult

List of usage examples for org.hibernate Query setFirstResult

Introduction

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

Prototype

@Override
    Query<R> setFirstResult(int startPosition);

Source Link

Usage

From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public <E> Page<E> find(Page<E> page, String qlString, Parameter parameter, Class[] resultClass,
        Class returnVO) {/*from  w  ww.j  a v  a 2 s .  c o  m*/
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countQlString = "select count(*) " + removeSelect(removeOrders(qlString));
        // page.setCount(Long.valueOf(createQuery(countQlString,
        // parameter).uniqueResult().toString()));
        Query query = createQuery(countQlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    String ql = qlString;
    // group by
    if (StringUtils.isNotBlank(page.getGroupBy())) {
        ql += " group by " + page.getGroupBy();
    }
    // order by

    if (StringUtils.isNotBlank(page.getOrderBy())) {
        ql += " order by " + page.getOrderBy();
    }
    Query query = createQuery(ql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    page.setList(query.list());

    boolean isMutilReturn = false;

    if (resultClass != null && resultClass.length > 1)
        isMutilReturn = true;

    if (isMutilReturn)
        setResultReturnVO(returnVO, page);
    return page;
}

From source file:com.ivanbiz.dao.impl.KelasDAOImpl.java

@Override
public String getLastNoTransaksi() throws Exception {
    Kelas kelas = null;//from  w  ww.  ja  v  a 2  s . c  o  m
    String noTransaksi = "";
    try {
        HibernateUtil.beginTransaction();
        Session session = HibernateUtil.getSession();
        Query query = session.createQuery("from com.ivanbiz.model.Kelas k order by k.id desc");
        query.setFirstResult(0);
        query.setMaxResults(1);
        kelas = (Kelas) query.uniqueResult();
        if (kelas != null) {
            noTransaksi = kelas.getTransactionReference();
        }
        HibernateUtil.commitTransaction();
    } catch (Exception e) {
        HibernateUtil.rollbackTransaction();
        throw e;
    } finally {
        HibernateUtil.closeSession();
    }

    return noTransaksi;
}

From source file:com.ivanbiz.dao.impl.PembayaranDAOImpl.java

@Override
public String getLastNoTransaksi() throws Exception {
    Pembayaran pembayaran = null;// w  ww .  j  a  va2  s .c o  m
    String noTransaksi = "";
    try {
        HibernateUtil.beginTransaction();
        Session session = HibernateUtil.getSession();
        Query query = session
                .createQuery("from Pembayaran p where p.transactionReference like '%PL%' order by p.id desc");
        query.setFirstResult(0);
        query.setMaxResults(1);
        pembayaran = (Pembayaran) query.uniqueResult();
        if (pembayaran != null) {
            noTransaksi = pembayaran.getTransactionReference();
        }
        HibernateUtil.commitTransaction();
    } catch (Exception e) {
        HibernateUtil.rollbackTransaction();
        throw e;
    } finally {
        HibernateUtil.closeSession();
    }
    return noTransaksi;
}

From source file:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java

License:Open Source License

@Override
public List<T> find(String hql, int page, int rows) {
    Query q = this.getCurrentSession().createQuery(hql);
    return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}

From source file:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java

License:Open Source License

@Override
public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
    Query q = this.getCurrentSession().createQuery(hql);
    if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
            q.setParameter(key, params.get(key));
        }//  ww w. j ava2  s . c  o m
    }
    return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}

From source file:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java

License:Open Source License

@Override
public List<T> findByOffset(String hql, Map<String, Object> params, int offset, int limit) {
    Query q = this.getCurrentSession().createQuery(hql);
    if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
            q.setParameter(key, params.get(key));
        }//from   ww  w. j  ava 2  s .c  o m
    }
    return q.setFirstResult(offset).setMaxResults(limit).list();
}

From source file:com.javaweb.service.ChuyenbayService.java

public ArrayList<Chuyenbay> GetAllChuyenbay(int pageSize, int pageNumber) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;//from  ww  w. j  a v a 2s  .c o  m
    ArrayList<Chuyenbay> listChuyenbay = new ArrayList<Chuyenbay>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Chuyenbay");
        chuyenbaycount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listChuyenbay = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listChuyenbay;
}

From source file:com.javaweb.service.ChuyenbayService.java

public ArrayList<Chuyenbay> SearchChuyenbay(int pageSize, int pageNumber, String Tu, String Den,
        String NgayKhoiHanh) {//from  w  w  w.  j a  v  a 2 s  .co  m
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    ArrayList listChuyenbay = new ArrayList<Chuyenbay>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Chuyenbay where " + "Tu like  '" + Tu + "' and " + "Den like '"
                + Den + "' " + "and NgayKhoiHanh = '" + NgayKhoiHanh + "'");
        chuyenbaycount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listChuyenbay = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listChuyenbay;
}

From source file:com.javaweb.service.TaikhoanService.java

public ArrayList<Taikhoan> GetAllTaikhoan(int pageSize, int pageNumber) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;//  w w w  .j  a  va 2 s .  c o  m
    ArrayList<Taikhoan> listTaikhoan = new ArrayList<Taikhoan>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Taikhoan");
        taikhoancount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listTaikhoan = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listTaikhoan;
}

From source file:com.javaweb.service.TaikhoanService.java

public ArrayList<Taikhoan> SearchTaikhoan(int pageSize, int pageNumber, String Email) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/*w ww.  j  a  v  a2 s. c  o m*/
    ArrayList listTaikhoan = new ArrayList<Taikhoan>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Taikhoan where Email like '%" + Email + "%'");
        taikhoancount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listTaikhoan = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listTaikhoan;
}