List of usage examples for org.hibernate SQLQuery setFirstResult
@Override
Query<R> setFirstResult(int startPosition);
From source file:cn.edu.xjtu.manage.dao.LicenseDao.java
License:Open Source License
public List<License> getLicense() { SQLQuery q = session.createSQLQuery("SELECT t.id,t.license,t.expires,t.isvalid FROM t_license t "); q.setFirstResult(0).setMaxResults(1); List l = q.list();/*from ww w . ja v a 2s.c o m*/ List<License> re = new ArrayList<License>(); for (int i = 0; i < l.size(); i++) { Object[] row = (Object[]) l.get(i); ; Integer id = (Integer) row[0]; String licensestr = (String) row[1]; Date expires = (Date) row[2]; int isvalid = (Integer) row[3]; License license = new License(id, licensestr, expires, isvalid); re.add(license); } return re; }
From source file:com.abssh.util.GenericDao.java
License:Apache License
/** * ?SQL/*from ww w . java2s.c o m*/ * * @param pageSize * ??,0? * @param pageNo * ??1? * @param sql * SQL * @param param * ? * @return ??object */ @SuppressWarnings("unchecked") public List findList(int pageSize, int pageNo, String sql, Object... param) { if (pageSize <= 0 || pageNo <= 0) { // ? return findList(sql, param); } SQLQuery query = getSession().createSQLQuery(sql); query.setFirstResult(((pageNo - 1) * pageSize)); query.setMaxResults(pageSize); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { query.setParameter(i, param[i]); } } return query.list(); }
From source file:com.abssh.util.GenericDao.java
License:Apache License
/** * SQL/* w w w.j a va 2 s . com*/ * * @param pageSize * ??,0? * @param pageNo * ??1? * @param sql * SQL * @param clazz * ?list * @param param * ? * @return */ @SuppressWarnings("unchecked") public List findList(int pageSize, int pageNo, String sql, Class clazz, Object... param) { if (pageSize <= 0 || pageNo <= 0) { // ? return findList(sql, clazz, param); } SQLQuery query = getSession().createSQLQuery(sql).addEntity(clazz); query.setFirstResult(((pageNo - 1) * pageSize)); query.setMaxResults(pageSize); if (param != null && param.length > 0) { for (int i = 0; i < param.length; i++) { query.setParameter(i, param[i]); } } return query.list(); }
From source file:com.bookselling.dao.SellingPostDaoImpl.java
@Override public PaginationData<SellingPost> getBestSelling(int first, int items) { SQLQuery sqlQuery = getSession().createSQLQuery("select boughtEntityId, sum(quatity) " + "from tradedetail trd " + "group by boughtEntityId " + "order by sum(quatity) desc"); sqlQuery.setFirstResult(first).setMaxResults(items); List result = sqlQuery.list(); Integer boughtEntityIds[] = new Integer[result.size()]; int index = 0; for (Object rows : boughtEntityIds) { Object[] row = (Object[]) rows; boughtEntityIds[index++] = Integer.valueOf((String) row[0]); }/* w w w . j a va 2 s. c om*/ Criteria criteria = getSession().createCriteria(SellingPost.class); criteria.add(Restrictions.eq("status", SellingPostStatus.CONFIRM)) .add(Restrictions.in("id", boughtEntityIds)); Set<SellingPost> posts = new HashSet<>(criteria.list()); HibernateInitSupport.setCls(SellingPost.class); for (SellingPost post : posts) HibernateInitSupport.initDomain(post); PaginationData paginationData = new PaginationData( (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, posts); return paginationData; }
From source file:com.duroty.application.files.manager.FilesManager.java
License:Open Source License
/** * DOCUMENT ME!/* w ww . ja v a 2 s . co m*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param folderName DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws FilesException DOCUMENT ME! */ public Vector getFiles(Session hsession, String repositoryName, String folderName, int label, int page, int messagesByPage, int order, String orderType) throws FilesException { Vector files = new Vector(); try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); Query hquery = null; String[] folderNameList = new String[0]; try { folderName = parseFolder(folderName); folderNameList = new String[] { folderName }; if (folderName.equals(this.folderAll) || folderName.equals(this.folderHidden)) { folderNameList = new String[] { this.folderAll, this.folderDraft, this.folderHidden, this.folderImportant, this.folderInbox, this.folderSent }; } } catch (Exception ex) { } if ((folderNameList.length == 0) && (label <= 0)) { hquery = hsession.getNamedQuery("attachments"); } else if ((folderNameList.length > 0) && (label <= 0)) { hquery = hsession.getNamedQuery("attachments-by-folder"); } else if ((folderNameList.length == 0) && (label > 0)) { hquery = hsession.getNamedQuery("attachments-by-label"); } else if ((folderNameList.length > 0) && (label > 0)) { hquery = hsession.getNamedQuery("attachments-by-folder-label"); } String aux = hquery.getQueryString(); switch (order) { case ORDER_BY_SIZE: if (orderType.equals("ASC")) { aux += " order by att_size asc"; } else { aux += " order by att_size desc"; } break; case ORDER_BY_DATE: if (orderType.equals("ASC")) { aux += " order by mes_date asc"; } else { aux += " order by mes_date desc"; } break; case ORDER_BY_TYPE: if (orderType.equals("ASC")) { aux += " order by att_content_type asc"; } else { aux += " order by att_content_type desc"; } break; default: if (!orderType.equals("ASC")) { aux += " order by att_name desc"; } else { aux += " order by att_name asc"; } break; } SQLQuery h2query = hsession.createSQLQuery(aux); if ((folderNameList.length == 0) && (label <= 0)) { h2query.setParameterList("no_boxes", new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE }); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } else if ((folderNameList.length > 0) && (label <= 0)) { h2query.setParameterList("boxes", folderNameList); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } else if ((folderNameList.length == 0) && (label > 0)) { h2query.setInteger("label", label); h2query.setParameterList("no_boxes", new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE }); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } else if ((folderNameList.length > 0) && (label > 0)) { h2query.setInteger("label", label); h2query.setParameterList("boxes", folderNameList); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } h2query.setFirstResult(page * messagesByPage); h2query.setMaxResults(messagesByPage); h2query.addEntity("testo", AttachmentWithDate.class); ScrollableResults scroll = h2query.scroll(); while (scroll.next()) { AttachmentWithDate attachment = (AttachmentWithDate) scroll.get(0); AttachmentObj obj = new AttachmentObj(); obj.setContentType(attachment.getAttContentType()); Date date = attachment.getAttDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); obj.setDate(date); obj.setIdint(attachment.getAttIdint()); obj.setName(attachment.getAttName()); obj.setPart(attachment.getAttPart()); int size = attachment.getAttSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } String extension = (String) this.extensions.get(attachment.getAttContentType()); if (StringUtils.isBlank(extension)) { extension = "generic"; } obj.setExtension(extension); Message message = attachment.getMessage(); if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer lab = new StringBuffer(); while (it.hasNext()) { if (lab.length() > 0) { lab.append(", "); } LabMes labMes = (LabMes) it.next(); lab.append(labMes.getId().getLabel().getLabName()); } if (lab.length() > 0) { obj.setLabel(lab.toString()); } else { } } obj.setBox(message.getMesBox()); obj.setMid(message.getMesName()); files.addElement(obj); } return files; } catch (Exception e) { throw new FilesException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!//from w w w . j a va 2 s . c om * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param label DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public Vector getMessages(Session hsession, String repositoryName, Label label, int page, int messagesByPage, int order, String orderType) throws MailException { Vector messages = new Vector(); try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); Query hquery = hsession.getNamedQuery("messages-by-label"); String aux = hquery.getQueryString(); switch (order) { case ORDER_BY_IMPORTANT: if (orderType.equals("ASC")) { aux += " order by mes_flagged asc"; } else { aux += " order by mes_flagged desc"; } break; case ORDER_BY_ADDRESS: if (orderType.equals("ASC")) { aux += " order by mes_from asc"; } else { aux += " order by mes_from desc"; } break; case ORDER_BY_SIZE: if (orderType.equals("ASC")) { aux += " order by mes_size asc"; } else { aux += " order by mes_size desc"; } break; case ORDER_BY_SUBJECT: if (orderType.equals("ASC")) { aux += " order by mes_subject asc"; } else { aux += " order by mes_subject desc"; } break; case ORDER_BY_DATE: if (orderType.equals("ASC")) { aux += " order by mes_date asc"; } else { aux += " order by mes_date desc"; } break; case ORDER_BY_UNREAD: if (orderType.equals("ASC")) { aux += " order by mes_recent asc"; } else { aux += " order by mes_recent desc"; } break; default: aux += " order by mes_date desc"; break; } SQLQuery h2query = hsession.createSQLQuery(aux); h2query.addEntity("i", Message.class); h2query.setInteger("label", label.getLabIdint()); h2query.setInteger("user", user.getUseIdint()); h2query.setString("folderTrash", this.folderTrash); h2query.setString("folderSpam", this.folderSpam); h2query.setString("folderDelete", FOLDER_DELETE); h2query.setFirstResult(page * messagesByPage); h2query.setMaxResults(messagesByPage); ScrollableResults scroll = h2query.scroll(); while (scroll.next()) { Message message = (Message) scroll.get(0); MessageObj obj = new MessageObj(message.getMesName()); obj.setBox(message.getMesBox()); obj.setFrom(message.getMesFrom()); if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) { obj.setHasAttachment(true); } else { obj.setHasAttachment(false); } int size = message.getMesSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } if (message.getMesBox().equals(folderSent)) { try { obj.setEmail(message.getMesTo()); } catch (Exception e) { obj.setEmail("unknown to"); } } else { obj.setEmail(message.getMesFrom()); } Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer buff = new StringBuffer(); while (it.hasNext()) { if (buff.length() > 0) { buff.append(", "); } LabMes labMes = (LabMes) it.next(); buff.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(buff.toString()); } try { if (StringUtils.isBlank(message.getMesSubject())) { obj.setSubject("(no subject)"); } else { obj.setSubject(message.getMesSubject()); } } catch (Exception ex) { obj.setSubject("(no subject)"); } if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.isMesRecent()) { obj.setRecent(true); } else { obj.setRecent(false); } String priority = "normal"; if (MessageUtilities.isHighPriority(message.getMesHeaders())) { priority = "high"; } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) { priority = "low"; } obj.setPriority(priority); messages.addElement(obj); } return messages; } catch (Exception e) { throw new MailException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java
License:Open Source License
public SQLQuery findBySQL(final String sql, final int pageNo, final int numPerPage) { return (SQLQuery) (T) getSession().doReturningWork(new ReturningWork() { public SQLQuery execute(Connection connection) throws SQLException { SQLQuery sqlQuery = getSession().createSQLQuery(sql); if (pageNo > 0) { sqlQuery.setFirstResult((pageNo - 1) * numPerPage); sqlQuery.setMaxResults(numPerPage); }/*from w w w . j a v a 2 s . c o m*/ return sqlQuery; } }); }
From source file:com.iwancool.dsm.dao.impl.AbstractBaseGenericORMDaoImpl.java
License:Open Source License
@Override public List<Object[]> findBySql(String sql, int page, int rows) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); 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<Object[]> findBySql(String sql, Map<String, Object> params, int page, int rows) { SQLQuery q = this.getCurrentSession().createSQLQuery(sql); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { q.setParameter(key, params.get(key)); }// ww w . java 2 s .c o m } return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list(); }
From source file:com.lighting.platform.base.dao.HibernateDao.java
License:Apache License
public <K> Page<K> pageSQLQuery(PageConfig pageConfig, SQLQuery sqlQuery, Object... values) { Page<K> page = new Page<K>(pageConfig); String totalSql = "select count(*) as total from(" + sqlQuery.getQueryString() + ")"; Query countQuery = this.getSession().createSQLQuery(totalSql); setParams(countQuery, values);//from w w w . j a v a 2s . com Number total = (Number) countQuery.uniqueResult(); if (total != null) { page.setTotal(total.longValue()); sqlQuery.setFirstResult(page.getOffset()).setMaxResults(page.getPageSize()); setParams(sqlQuery, values); List<K> list = sqlQuery.list(); page.setRows(list); } else { page.setTotal(0); page.setRows(new ArrayList<K>()); } return page; }