List of usage examples for org.hibernate ScrollableResults last
boolean last();
From source file:com.algoTrader.entity.marketData.BarDaoBase.java
/** * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance. * @param queryObject/* w w w. j a v a 2s .co m*/ * @param transform * @param pageNumber * @param pageSize * @return PaginationResult */ @SuppressWarnings({ "unchecked" }) protected PaginationResult getPaginationResult(final Query queryObject, final int transform, int pageNumber, int pageSize) { try { final ScrollableResults scrollableResults = queryObject.scroll(); scrollableResults.last(); int totalCount = scrollableResults.getRowNumber(); totalCount = totalCount >= 0 ? totalCount + 1 : 0; if (pageNumber > 0 && pageSize > 0) { queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); queryObject.setMaxResults(pageSize); } // Unchecked transformation because Set object is reused, cannot be strongly typed. Set results = new LinkedHashSet(queryObject.list()); transformEntities(transform, results); return new PaginationResult(results.toArray(new Object[results.size()]), totalCount); } catch (HibernateException ex) { throw super.convertHibernateAccessException(ex); } }
From source file:com.farmafene.commons.hibernate.HBPaginaFactory.java
License:Open Source License
/** * Metodo factora/*from ww w. j av a2s. co m*/ * * @param begin * primer registro * @param size * tamao pgina * @param result * dnde buscar * @return pgina generada * @since 1.0.0 */ public HBPagina<T> getPagina(int begin, int size, ScrollableResults result) { HBPaginaImp lista = new HBPaginaImp(); if (lista.getFirst() < 1) { lista.setFirst(1); } if (lista.getPageSize() < 0) { lista.setPageSize(0); } result.beforeFirst(); result.scroll(lista.getFirst() - 1); for (int i = 0; result.next() && (lista.getPageSize() == 0 || i < lista.getPageSize()); i++) { T obj = handler.makeObject(result.get()); if (obj != null) { lista.getLista().add(obj); } } if (result.last()) { lista.setTotalSize(result.getRowNumber() + 1); } else { lista.setTotalSize(0); lista.setFirst(0); } return lista; }
From source file:com.ibm.asset.trails.dao.jpa.VSoftwareLparDAOJpa.java
public void paginatedList(DisplayTagList data, Account account, ReconSetting reconSetting, int startIndex, int objectsPerPage, String sort, String dir) { Criteria criteria = getHibernateSessionCriteria(); criteria.createAlias("hardwareLpar", "hl") .createAlias("hl.hardwareLparEff", "hle", CriteriaSpecification.LEFT_JOIN) .createAlias("hl.hardware", "h").createAlias("h.machineType", "mt") .createAlias("installedSoftwares", "is") .createAlias("is.scheduleF", "sf", CriteriaSpecification.LEFT_JOIN) .createAlias("sf.scope", "scope", CriteriaSpecification.LEFT_JOIN) .createAlias("is.softwareLpar", "sl").createAlias("is.alert", "aus") .createAlias("aus.reconcile", "r", CriteriaSpecification.LEFT_JOIN) .createAlias("r.usedLicenses", "ul", CriteriaSpecification.LEFT_JOIN) .createAlias("ul.license", "license", CriteriaSpecification.LEFT_JOIN) .createAlias("r.reconcileType", "rt", CriteriaSpecification.LEFT_JOIN) .createAlias("is.software", "sw") .createAlias("sw.manufacturer", "mf", CriteriaSpecification.LEFT_JOIN) .add(Restrictions.eq("account", account)); if (reconSetting.getReconcileType() != null) { criteria.add(Restrictions.eq("rt.id", reconSetting.getReconcileType())); }/*www . j ava2 s .co m*/ if (StringUtils.isNotBlank(reconSetting.getAlertStatus())) { boolean open = false; if (reconSetting.getAlertStatus().equals("OPEN")) { open = true; criteria.add(Restrictions.eq("aus.open", open)); } else { criteria.add(Restrictions.and(Restrictions.eq("aus.open", false), Restrictions.eqProperty("is.id", "r.installedSoftware.id"))); } } else { criteria.add(Restrictions.or(Restrictions.eq("aus.open", true), Restrictions.and(Restrictions.eq("aus.open", false), Restrictions.eqProperty("is.id", "r.installedSoftware.id")))); } if (null != reconSetting.getAlertFrom() && reconSetting.getAlertFrom().intValue() >= 0) { criteria.add(Restrictions.ge("aus.alertAge", reconSetting.getAlertFrom())); } if (null != reconSetting.getAlertTo() && reconSetting.getAlertTo().intValue() >= 0) { criteria.add(Restrictions.le("aus.alertAge", reconSetting.getAlertTo())); } if (StringUtils.isNotBlank(reconSetting.getAssigned())) { if (reconSetting.getAssigned().equals("Assigned")) { criteria.add(Restrictions.ne("aus.remoteUser", "STAGING")); } if (reconSetting.getAssigned().equals("Unassigned")) { criteria.add(Restrictions.eq("aus.remoteUser", "STAGING")); } } if (StringUtils.isNotBlank(reconSetting.getAssignee())) { criteria.add(Restrictions.eq("aus.remoteUser", reconSetting.getAssignee()).ignoreCase()); } if (StringUtils.isNotBlank(reconSetting.getOwner())) { if (reconSetting.getOwner().equalsIgnoreCase("IBM")) { criteria.add(Restrictions.eq("h.owner", reconSetting.getOwner()).ignoreCase()); } else if (reconSetting.getOwner().equalsIgnoreCase("Customer")) { ArrayList<String> lalOwner = new ArrayList<String>(); lalOwner.add("CUST"); lalOwner.add("CUSTO"); criteria.add(Restrictions.in("h.owner", lalOwner)); } } // I'm not sure why the heck we aren't just getting a list of strings? if (reconSetting.getCountries().length > 0) { List<String> list = new ArrayList<String>(); for (int i = 0; i < reconSetting.getCountries().length; i++) { if (StringUtils.isNotBlank(reconSetting.getCountries()[i])) { list.add(reconSetting.getCountries()[i].toUpperCase()); } } if (list.size() > 0) { criteria.add(Restrictions.in("h.country", list)); } } if (reconSetting.getNames().length > 0) { List<String> list = new ArrayList<String>(); for (int i = 0; i < reconSetting.getNames().length; i++) { if (StringUtils.isNotBlank(reconSetting.getNames()[i])) { list.add(reconSetting.getNames()[i].toUpperCase()); } } if (list.size() > 0) { criteria.add(Restrictions.in("hl.name", list)); } } if (reconSetting.getSwcmIDs().length > 0) { List<String> list = new ArrayList<String>(); for (int i = 0; i < reconSetting.getSwcmIDs().length; i++) { if (StringUtils.isNotBlank(reconSetting.getSwcmIDs()[i])) { list.add(reconSetting.getSwcmIDs()[i].toUpperCase()); } } if (list.size() > 0) { criteria.add(Restrictions.in("license.extSrcId", list)); } } if (reconSetting.getSerialNumbers().length > 0) { List<String> list = new ArrayList<String>(); for (int i = 0; i < reconSetting.getSerialNumbers().length; i++) { if (StringUtils.isNotBlank(reconSetting.getSerialNumbers()[i])) { list.add(reconSetting.getSerialNumbers()[i].toUpperCase()); } } if (list.size() > 0) { criteria.add(Restrictions.in("h.serial", list)); } } if (reconSetting.getProductInfoNames().length > 0) { List<String> list = new ArrayList<String>(); for (int i = 0; i < reconSetting.getProductInfoNames().length; i++) { if (StringUtils.isNotBlank(reconSetting.getProductInfoNames()[i])) { list.add(reconSetting.getProductInfoNames()[i]); } } if (list.size() > 0) { criteria.add(Restrictions.in("sw.softwareName", list)); } } if (StringUtils.isNotBlank(reconSetting.getScope())) { if ("Not specified".equalsIgnoreCase(reconSetting.getScope())) { criteria.add(Restrictions.isNull("scope.description")); } else { criteria.add(Restrictions.eq("scope.description", reconSetting.getScope())); } } if (StringUtils.isNotBlank(reconSetting.getFinanResp())) { if ("Not Specified".trim().equalsIgnoreCase(reconSetting.getFinanResp())) { criteria.add(Restrictions.isNull("sf.SWFinanceResp")); } else { criteria.add(Restrictions.eq("sf.SWFinanceResp", reconSetting.getFinanResp())); } } criteria.setProjection(Projections.projectionList().add(Projections.property("aus.id").as("alertId")) .add(Projections.property("r.id").as("reconcileId")) .add(Projections.property("aus.alertAge").as("alertAgeI")) .add(Projections.property("is.id").as("installedSoftwareId")) .add(Projections.property("hl.name").as("hostname")) .add(Projections.property("sl.name").as("sl_hostname")) .add(Projections.property("hl.spla").as("spla")) .add(Projections.property("hl.sysplex").as("sysplex")) .add(Projections.property("hl.internetIccFlag").as("internetIccFlag")) .add(Projections.property("h.serial").as("serial")) .add(Projections.property("h.country").as("country")) .add(Projections.property("h.owner").as("owner")) .add(Projections.property("h.mastProcessorType").as("mastProcessorType")) .add(Projections.property("h.processorManufacturer").as("processorManufacturer")) .add(Projections.property("h.mastProcessorModel").as("mastProcessorModel")) .add(Projections.property("h.nbrCoresPerChip").as("nbrCoresPerChip")) .add(Projections.property("h.nbrOfChipsMax").as("nbrOfChipsMax")) .add(Projections.property("h.cpuLsprMips").as("cpuLsprMips")) .add(Projections.property("h.cpuIfl").as("cpuIFL")) .add(Projections.property("hl.partLsprMips").as("partLsprMips")) .add(Projections.property("h.cpuGartnerMips").as("cpuGartnerMips")) .add(Projections.property("hl.partGartnerMips").as("partGartnerMips")) .add(Projections.property("hl.effectiveThreads").as("effectiveThreads")) .add(Projections.property("hl.vcpu").as("vcpu")).add(Projections.property("h.cpuMsu").as("cpuMsu")) .add(Projections.property("hl.partMsu").as("partMsu")) .add(Projections.property("hl.serverType").as("lparServerType")) .add(Projections.property("h.shared").as("shared")) .add(Projections.property("h.multi_tenant").as("multi_tenant")) .add(Projections.property("mt.type").as("assetType")) .add(Projections.property("mt.name").as("assetName")) .add(Projections.property("h.hardwareStatus").as("hardwareStatus")) .add(Projections.property("hl.lparStatus").as("lparStatus")) .add(Projections.property("processorCount").as("processorCount")) .add(Projections.property("sw.softwareName").as("productInfoName")) .add(Projections.property("sw.softwareId").as("productInfoId")) .add(Projections.property("sw.pid").as("pid")) .add(Projections.property("mf.manufacturerName").as("manufacturerName")) .add(Projections.property("rt.name").as("reconcileTypeName")) .add(Projections.property("rt.id").as("reconcileTypeId")) .add(Projections.property("aus.remoteUser").as("assignee")) .add(Projections.property("h.processorCount").as("hardwareProcessorCount")) .add(Projections.property("hle.processorCount").as("hwLparEffProcessorCount")) .add(Projections.property("hl.osType").as("osType")) .add(Projections.property("hle.status").as("hwLparEffProcessorStatus")) .add(Projections.property("h.chips").as("chips"))); criteria.setResultTransformer(new AliasToBeanResultTransformer(ReconWorkspace.class)); criteria.addOrder(Order.desc("aus.open")); if (dir.equalsIgnoreCase("ASC")) { criteria.addOrder(Order.asc(sort)); } else { criteria.addOrder(Order.desc(sort)); } ArrayList<ReconWorkspace> list = new ArrayList<ReconWorkspace>(); ScrollableResults itemCursor = criteria.scroll(); itemCursor.beforeFirst(); if (itemCursor.next()) { itemCursor.scroll(startIndex); int i = 0; while (objectsPerPage > i++) { ReconWorkspace rw = (ReconWorkspace) itemCursor.get(0); if (null != rw.getHwLparEffProcessorStatus() && rw.getHwLparEffProcessorStatus().equalsIgnoreCase("INACTIVE")) { rw.setHwLparEffProcessorCount(0); } list.add(rw); if (!itemCursor.next()) break; } data.setList(list); itemCursor.last(); data.setFullListSize(itemCursor.getRowNumber() + 1); itemCursor.close(); addSchedulef2List(account, data.getList()); } else { data.setList(null); data.setFullListSize(0); itemCursor.close(); } }
From source file:com.ikon.module.db.stuff.IndexHelper.java
License:Open Source License
protected int doRebuildIndex() throws Exception { FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate(); fullTextSession.setFlushMode(org.hibernate.FlushMode.MANUAL); fullTextSession.setCacheMode(org.hibernate.CacheMode.IGNORE); fullTextSession.purgeAll(NodeDocumentVersion.class); fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class); String query = "select ndv from NodeDocumentVersion ndv"; ScrollableResults cursor = fullTextSession.createQuery(query).scroll(); cursor.last(); int count = cursor.getRowNumber() + 1; log.warn("Re-building Wine index for " + count + " objects."); if (count > 0) { int batchSize = 300; cursor.first(); // Reset to first result row int i = 0; while (true) { fullTextSession.index(cursor.get(0)); if (++i % batchSize == 0) { fullTextSession.flushToIndexes(); fullTextSession.clear(); // Clear persistence context for each batch log.info("Flushed index update " + i + " from Thread " + Thread.currentThread().getName()); }//from w ww. jav a2 s . co m if (cursor.isLast()) { break; } cursor.next(); } } cursor.close(); fullTextSession.flushToIndexes(); fullTextSession.clear(); // Clear persistence context for each batch fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class); return count; }
From source file:com.lynn.dao.BaseDao.java
/** * <HQL>//from www . j a va 2 s .c om * @param hql HQL? * @param countHql ?HQL? * @param pageNo * @param pageSize ? * @param values ?Object? * @return PageResults???????List? */ public PageResults<T> findPageByFetchedHql(String hql, String countHql, int pageNo, int pageSize, Object... values) { PageResults<T> retValue = new PageResults<T>(); Query query = this.getSession().createQuery(hql); if (values != null) { for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } } int currentPage = pageNo > 1 ? pageNo : 1; retValue.setCurrentPage(currentPage); retValue.setPageSize(pageSize); if (countHql == null) { ScrollableResults results = query.scroll(); results.last(); retValue.setTotalCount(results.getRowNumber() + 1);// } else { Long count = countByHql(countHql, values); retValue.setTotalCount(count.intValue()); } retValue.resetPageNo(); List<T> itemList = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list(); if (itemList == null) { itemList = new ArrayList<T>(); } retValue.setResults(itemList); return retValue; }
From source file:com.reignite.query.StructuredQuery.java
License:Open Source License
private int runQuery(Criteria criteria, QueryResult result, int maxResults) { ScrollableResults scroll = criteria.scroll(ScrollMode.FORWARD_ONLY); int count = 0; if (scroll.setRowNumber(startIndex)) { while (count < maxResults) { Object[] row = scroll.get(); count = fillResult(result, row) ? count += 1 : count; if (!scroll.next()) { break; }//w ww . j a v a 2 s .c o m } } int totalResultCount = 0; if (scroll.last()) { totalResultCount = scroll.getRowNumber() + 1; } result.setTotalResults(totalResultCount); scroll.close(); return count; }
From source file:de.innovationgate.webgate.api.jdbc.WGSQLResultSet.java
License:Open Source License
private int getNormalQueryResults(String query) { ScrollableResults scroll = getQuery().scroll(); scroll.last(); return (scroll.getRowNumber() + 1); }
From source file:it.eng.qbe.statement.hibernate.HQLDataSet.java
License:Mozilla Public License
private int getResultNumberUsingScrollableResults(org.hibernate.Query hibernateQuery, Session session) { int resultNumber = 0; logger.debug("Scrolling query " + statement.getQueryString() + " ..."); ScrollableResults scrollableResults = hibernateQuery.scroll(); scrollableResults.last(); logger.debug("Scrolled query " + statement.getQueryString()); resultNumber = scrollableResults.getRowNumber() + 1; // Hibernate ScrollableResults row number starts with 0 logger.debug("Number of fetched records: " + resultNumber + " for query " + statement.getQueryString()); resultNumber = resultNumber < 0 ? 0 : resultNumber; return resultNumber; }
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
@Override @Transactional(readOnly = true)//from w ww . ja v a 2 s.c o m public int count(final List<Criterion> criterions, final Map<String, String> aliases, final SpecifiedIdsFilter filter) { return getHibernateTemplate().execute(new HibernateCallback<Integer>() { @Override public Integer doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = createCriteriaForFilter(session, filter); if (aliases != null && !aliases.isEmpty()) { for (Map.Entry<String, String> alias : aliases.entrySet()) { criteria.createAlias(alias.getKey(), alias.getValue()); } } if (criterions != null && !criterions.isEmpty()) { for (Criterion restrictions : criterions) { criteria.add(restrictions); } } ScrollableResults scroll = criteria.scroll(); return scroll.last() ? scroll.getRowNumber() + 1 : 0; } }); }
From source file:net.firejack.platform.core.store.BaseStore.java
License:Apache License
public Integer findCountWithFilter(final List<Criterion> criterions, final Map<String, String> aliases, final SpecifiedIdsFilter filter, final Projection projection) { return getHibernateTemplate().execute(new HibernateCallback<Integer>() { public Integer doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = createCriteriaForFilter(session, filter); if (aliases != null && !aliases.isEmpty()) { for (Map.Entry<String, String> alias : aliases.entrySet()) { criteria.createAlias(alias.getKey(), alias.getValue(), CriteriaSpecification.LEFT_JOIN); }/* w ww . j a v a 2 s . c om*/ } if (criterions != null && !criterions.isEmpty()) { for (Criterion restrictions : criterions) { criteria.add(restrictions); } } if (projection != null) { criteria.setProjection(projection); } ScrollableResults scroll = criteria.scroll(); return scroll.last() ? scroll.getRowNumber() + 1 : 0; } }); }