List of usage examples for org.hibernate Query setMaxResults
@Override
Query<R> setMaxResults(int maxResult);
From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java
License:Open Source License
/** * SQL /* w w w . ja v a 2s.c om*/ * * @param page * @param sqlString * @param resultClass * @param parameter * @return */ @SuppressWarnings("unchecked") public <E> Page<E> findBySql(Page<E> page, String sqlString, Class<?> resultClass, Object... parameter) { // get count if (!page.isDisabled() && !page.isNotCount()) { String countSqlString = "select count(*) " + removeSelect(removeOrders(sqlString)); // page.setCount(Long.valueOf(createSqlQuery(countSqlString, // parameter).uniqueResult().toString())); Query query = createSqlQuery(countSqlString, 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; } } // order by String sql = sqlString; if (StringUtils.isNotBlank(page.getOrderBy())) { sql += " order by " + page.getOrderBy(); } SQLQuery query = createSqlQuery(sql, parameter); // set page if (!page.isDisabled()) { query.setFirstResult(page.getFirstResult()); query.setMaxResults(page.getMaxResults()); } setResultTransformer(query, resultClass); page.setList(query.list()); return page; }
From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public <E> Page<E> findBySqlString(Page<E> page, String sqlString, Parameter parameter, Class[] resultClass, Class returnVO) {//from w w w . ja v a 2s.co m // get count if (!page.isDisabled() && !page.isNotCount()) { String select = StringHelper.isNull(page.getCountDistinctColumn()) ? "select count(*) " : "select count(distinct " + page.getCountDistinctColumn() + ") "; String countSqlString = select + removeSelect(removeOrders(sqlString)); // page.setCount(Long.valueOf(createSqlQuery(countSqlString, // parameter).uniqueResult().toString())); Query query = createSqlQuery(countSqlString, 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 sql = sqlString; // group by if (StringUtils.isNotBlank(page.getGroupBy())) { sql += " group by " + page.getGroupBy(); } // order by if (StringUtils.isNotBlank(page.getOrderBy())) { sql += " order by " + page.getOrderBy(); } SQLQuery query = createSqlQuery(sql, parameter); // set page if (!page.isDisabled()) { query.setFirstResult(page.getFirstResult()); query.setMaxResults(page.getMaxResults()); } boolean isMutilReturn = false; if (resultClass != null && resultClass.length == 1) setResultTransformer(query, resultClass[0]); else if (resultClass != null && resultClass.length > 1) { setResultTransformer(query, resultClass); isMutilReturn = true; } List l = query.list(); page.setList(query.list()); if (isMutilReturn) setResultReturnVO(returnVO, page); return page; }
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 w w . j av a2 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;//ww w .j a va 2s.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;/*from www . jav a 2 s. com*/ 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.jardoapps.usermodule.hbn.dao.PasswordResetTokenEntityDao.java
License:Open Source License
public PasswordResetTokenEntity getNewestToken(Session session, String email) { String queryStr = "FROM PasswordResetTokenEntity prt WHERE prt.user.email = :email ORDER BY prt.time DESC"; Query query = session.createQuery(queryStr); query.setParameter("email", email); query.setMaxResults(1); return (PasswordResetTokenEntity) query.uniqueResult(); }
From source file:com.jardoapps.usermodule.hbn.dao.UserEntityDao.java
License:Open Source License
public UserEntity findByEmail(Session session, String email) { String queryStr = "FROM UserEntity u WHERE u.email = :email AND u.deleted = false"; Query query = session.createQuery(queryStr); query.setParameter("email", email); query.setMaxResults(1); return (UserEntity) query.uniqueResult(); }
From source file:com.jardoapps.usermodule.hbn.dao.UserEntityDao.java
License:Open Source License
public UserEntity findByName(Session session, String name) { String queryStr = "FROM UserEntity u WHERE u.name = :name AND u.deleted = false"; Query query = session.createQuery(queryStr); query.setParameter("name", name); query.setMaxResults(1); return (UserEntity) query.uniqueResult(); }
From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java
License:Open Source License
@Transactional(propagation = Propagation.REQUIRED) public List<ResourceLookup> getDependentResources(ExecutionContext context, String uri, SearchCriteriaFactory searchCriteriaFactory, int current, int max) { final Resource resource = getResource(context, uri); if (resource == null) { throw new JSException("jsexception.resource.does.not.exist"); }/*from w w w . jav a 2s .co m*/ if (resource instanceof ReportDataSource) { //ReportDataSource can have dependent ReportUnit items final ReportDataSource dataSource = (ReportDataSource) resource; RepoResource repoDS = findByURI(RepoResource.class, uri, true); Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //TODO GetDependentResourcesIds query can be updated to handle ReportUnit objects too org.hibernate.Query query = session.getNamedQuery("GetDependentResourcesIds"); query.setEntity("dependency", repoDS).setFirstResult(current); if (max > 0) { query.setMaxResults(max); } List<Long> depRes = query.list(); List<ResourceLookup> depResLookup = getResourcesByIdList(depRes); return depResLookup; //TODO delete if code above proves to work fine // SearchFilter filter = new SearchFilter() { // public void applyRestrictions(String type, ExecutionContext context, SearchCriteria criteria) { // DetachedCriteria dsCriteria = criteria.createCriteria("dataSource", "ds"); // dsCriteria.add(Restrictions.eq("ds.name", dataSource.getName())); // // DetachedCriteria dsParent = dsCriteria.createCriteria("parent", "dsParent"); // dsParent.add(Restrictions.eq("dsParent.URI", dataSource.getParentFolder())); // // // will use it later on for sorting // DetachedCriteria folderCriteria = criteria.createCriteria("parent", "f"); // } // }; // // SearchSorter sorter = new SearchSorter() { // public void applyOrder(String type, ExecutionContext context, SearchCriteria criteria) { // criteria.addOrder(Order.asc("f.URI")); // criteria.addOrder(Order.asc("name")); // // by default we retrieving only id's and to be able sort we had to add property to select clause // criteria.addProjection(Projections.property("f.URI")); // criteria.addProjection(Projections.property("name")); // } // }; // SearchCriteriaFactory criteriaFactory = searchCriteriaFactory.newFactory(ReportUnit.class.getName()); // List<ResourceLookup> resources = getResources( // context, // criteriaFactory, // Arrays.asList(filter), // sorter, // new BasicTransformerFactory(), // current, max); // // return resources; //TODO: replace 'exist' with right solution, it's hardcoded fix for CE version } else if (resource instanceof ReportUnit && exist("com.jaspersoft.ji.adhoc.DashboardResource")) { //ReportUnit can have dependent DashboardResource items final RepoResource repoResource = getRepoResource(resource); SearchFilter filter = new SearchFilter() { public void applyRestrictions(String type, ExecutionContext context, SearchCriteria criteria) { DetachedCriteria resourcesCriteria = criteria.createCriteria("resources", "res"); resourcesCriteria.add(Restrictions.eq("res.id", repoResource.getId())); } }; SearchSorter sorter = new SearchSorter() { @Override protected void addOrder(String type, ExecutionContext context, SearchCriteria criteria) { criteria.addOrder(Order.asc("name")).addOrder(Order.asc("id")); } @Override protected void addProjection(String type, ExecutionContext context, SearchCriteria criteria) { } }; SearchCriteriaFactory criteriaFactory = searchCriteriaFactory .newFactory("com.jaspersoft.ji.adhoc.DashboardResource"); List<ResourceLookup> resources = getResources(context, criteriaFactory, Arrays.asList(filter), sorter, new BasicTransformerFactory(), current, max); return resources; } else { return null; //unsupported resource } }
From source file:com.javaweb.service.ChuyenbayService.java
public ArrayList<Chuyenbay> GetAllChuyenbay(int pageSize, int pageNumber) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null;/* w w w. j ava2 s . c om*/ 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; }