List of usage examples for org.hibernate Query scroll
ScrollableResults scroll(ScrollMode scrollMode);
From source file:mitm.application.djigzo.impl.hibernate.UserDAO.java
License:Open Source License
public CloseableIterator<String> searchEmail(String search, Integer firstResult, Integer maxResults, SortDirection sortDirection) {/*from w w w. j a va 2 s .co m*/ if (sortDirection == null) { sortDirection = SortDirection.ASC; } /* * We will use HQL because I do not (yet?) know how to return just a field instead of * an object when using the Criteria API. */ final String hql = "select n.email from " + entityName + " n where n.email LIKE :search order by n.email " + sortDirection; Query query = createQuery(hql); query.setParameter("search", search); if (firstResult != null) { query.setFirstResult(firstResult); } if (maxResults != null) { query.setMaxResults(maxResults); } ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY); return new EmailIterator(scrollableResults); }
From source file:mitm.application.djigzo.impl.hibernate.UserPreferencesDAO.java
License:Open Source License
public CloseableIterator<String> getPreferencesIterator(String category) { /*/*www. j av a 2 s . c o m*/ * We will use HQL because I do not (yet?) know how to return just a field instead of * an object when using the Criteria API. */ final String hql = "select p.name from " + entityName + " p where p.category = :category"; Query query = createQuery(hql); query.setString("category", category); ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY); return new StringIterator(scrollableResults); }
From source file:mitm.application.djigzo.impl.hibernate.UserPreferencesDAO.java
License:Open Source License
public CloseableIterator<String> getPreferencesIterator(String category, Integer firstResult, Integer maxResults, SortDirection sortDirection) { if (sortDirection == null) { sortDirection = SortDirection.ASC; }//from w w w . ja v a2 s . c om /* * We will use HQL because I do not (yet?) know how to return just a field instead of * an object when using the Criteria API. */ final String hql = "select p.name from " + entityName + " p where p.category = :category order by p.name " + sortDirection; Query query = createQuery(hql); query.setString("category", category); if (firstResult != null) { query.setFirstResult(firstResult); } if (maxResults != null) { query.setMaxResults(maxResults); } ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY); return new StringIterator(scrollableResults); }
From source file:mitm.application.djigzo.impl.hibernate.UserPreferencesDAO.java
License:Open Source License
public CloseableIterator<String> getCategoryIterator() { /*/* w w w .jav a 2 s. c o m*/ * We will use HQL because I do not (yet?) know how to return just a field instead of * an object when using the Criteria API. */ final String hql = "select distinct p.category from " + entityName + " p"; Query query = createQuery(hql); ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY); return new StringIterator(scrollableResults); }
From source file:mitm.common.security.certstore.dao.X509CertStoreDAOHibernate.java
License:Open Source License
@Override public CloseableIterator<X509CertStoreEntryHibernate> getByEmail(String email, Match match, Expired expired, MissingKeyAlias missingKeyAlias, Date date, Integer firstResult, Integer maxResults) { /*/*from w w w . jav a2 s . c om*/ * we need to get distinct results using the distinct keyword. This is not supported by Criteria * AFAIK so we will need to use HQL */ String baseQuery = "select distinct c from " + entityName + " c"; Query query = createByEmailQuery(baseQuery, email, match, expired, missingKeyAlias, date); if (firstResult != null) { query.setFirstResult(firstResult); } if (maxResults != null) { query.setMaxResults(maxResults); } ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); return new X509CertStoreEntryIterator(results); }
From source file:nl.strohalm.cyclos.utils.ScrollableResultsIterator.java
License:Open Source License
public ScrollableResultsIterator(final Query query, final Transformer<Object[], T> transformer) { this.results = query.scroll(ScrollMode.FORWARD_ONLY); if (query instanceof SQLQuery) { // The getReturnTypes doesn't work for SQLQueries... Assume an array array = true;/* w w w . ja va 2 s .com*/ } else { // this (extra) check to see if the query starts with "select new" is just to support the // following case: SELECT new A(e.prop1, e.prop2) FROM Entity e ... // in that case we musn't return an array in the next() method. array = query.getReturnTypes().length > 1 && !query.getQueryString().trim().toLowerCase().startsWith("select new"); } this.transformer = transformer; getNextObject(); DataIteratorHelper.registerOpen(this, true); }
From source file:org.candlepin.gutterball.curator.ComplianceSnapshotCurator.java
License:Open Source License
/** * Retrieves the compliance status counts over the given time span with the specified criteria. * The counts are returned in a map of maps, with the outer map mapping the dates to the inner * map which maps the statuses to their respective counts. * <p></p>/* w ww . ja v a2 s .co m*/ * If the start and/or end dates are null, the time span will be similarly unrestricted. Note * that the time within a given Date object is ignored. If neither the start nor end dates are * provided, all known compliance status data will be used. * * @param startDate * The date at which the time span should begin. If null, all compliance statuses before the * end date (if provided) will be used. * * @param endDate * The date at which the time span should end. If null, all compliance statuses after the * start date (if provided) will be used. * * @param sku * A subscription sku to use to filter compliance status counts. If provided, only consumers * using the specified sku will be counted. * * @param subscriptionName * A subscription name to use to filter compliance status counts. If provided, only consumers * using subscriptions with the specified product name will be counted. * * @param productName * A product name to use to filter compliance status counts. If provided, only consumers with * an installed product with the specified product name will be counted. * * @param attributes * A map of entitlement attributes to use to filter compliance status counts. If provided, only * consumers with entitlements having the specified values for the given attributes will be * counted. * * @param ownerKey * An owner key to use to filter compliance status counts. If provided, only consumers * associated with the specified owner key/account will be counted. * * @param pageRequest * A PageRequest instance containing paging information from the request. If null, no paging * will be performed. * * @return * A page containing a map of maps containing the compliance status counts, grouped by day. If * no counts were found for the given time span, the page will contain an empty map. */ public Page<Map<Date, Map<String, Integer>>> getComplianceStatusCounts(Date startDate, Date endDate, String ownerKey, List<String> consumerUuids, String sku, String subscriptionName, String productName, Map<String, String> attributes, PageRequest pageRequest) { Page<Map<Date, Map<String, Integer>>> page = new Page<Map<Date, Map<String, Integer>>>(); page.setPageRequest(pageRequest); // Build our query... // Impl note: This query's results MUST be sorted by date in ascending order. If it's not, // the algorithm below breaks. Query query = this.buildComplianceStatusCountQuery(this.currentSession(), startDate, endDate, ownerKey, consumerUuids, sku, subscriptionName, productName, attributes); // Clamp our dates so they're no further out than "today." Date today = new Date(); if (startDate != null && startDate.after(today)) { startDate = today; } if (endDate != null && endDate.after(today)) { endDate = today; } // Execute & process results... Map<Date, Map<String, Integer>> resultmap = new TreeMap<Date, Map<String, Integer>>(); Map<String, Object[]> cstatusmap = new HashMap<String, Object[]>(); // Step through our data and do our manual aggregation bits... ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); if (results.next()) { Calendar date = Calendar.getInstance(); Object[] row = results.get(); String uuid = (String) row[0]; row[1] = ((String) row[1]).toLowerCase(); date.setTime((Date) row[2]); // Prime the calendars here... Calendar cdate = Calendar.getInstance(); cdate.setTime(startDate != null ? startDate : date.getTime()); cdate.set(Calendar.HOUR_OF_DAY, 23); cdate.set(Calendar.MINUTE, 59); cdate.set(Calendar.SECOND, 59); cdate.set(Calendar.MILLISECOND, 999); Calendar end = Calendar.getInstance(); end.setTimeInMillis(endDate != null ? endDate.getTime() : Long.MAX_VALUE); for (; this.compareCalendarsByDate(cdate, end) <= 0; cdate.add(Calendar.DATE, 1)) { while (this.compareCalendarsByDate(date, cdate) <= 0) { // Date is before our current date. Store the uuid's status so we can add it to // our counts later. cstatusmap.put(uuid, row); if (!results.next()) { if (endDate == null) { end.setTimeInMillis(cdate.getTimeInMillis()); } break; } row = (Object[]) results.get(); uuid = (String) row[0]; row[1] = ((String) row[1]).toLowerCase(); date.setTime((Date) row[2]); } Date hashdate = cdate.getTime(); Map<String, Integer> statusmap = new HashMap<String, Integer>(); // Go through and add up all our counts for the day. for (Object[] cstatus : cstatusmap.values()) { if (cstatus[3] == null || this.compareDatesByDate(hashdate, (Date) cstatus[3]) < 0) { Integer count = statusmap.get((String) cstatus[1]); statusmap.put((String) cstatus[1], (count != null ? count + 1 : 1)); } } resultmap.put(hashdate, statusmap); } } results.close(); // Pagination // This is horribly inefficient, but the only way to do it with the current implementation. if (pageRequest != null && pageRequest.isPaging()) { page.setMaxRecords(resultmap.size()); int offset = (pageRequest.getPage() - 1) * pageRequest.getPerPage(); int nextpage = offset + pageRequest.getPerPage(); // Trim results. :( Iterator<Date> iterator = resultmap.keySet().iterator(); for (int pos = 0; iterator.hasNext(); ++pos) { iterator.next(); if (pos < offset || pos >= nextpage) { iterator.remove(); } } } page.setPageData(resultmap); return page; }
From source file:org.candlepin.model.ColumnarResultIteratorTest.java
License:Open Source License
@Test public void testHasNextWithElements() { this.ownerCurator.create(TestUtil.createOwner()); Query query = this.session.createQuery("SELECT o FROM Owner o"); ColumnarResultIterator<Owner> iterator = new ColumnarResultIterator<>(this.session, query.scroll(ScrollMode.FORWARD_ONLY), 0, false); try {//from ww w . jav a2 s .c om assertTrue(iterator.hasNext()); } finally { iterator.close(); } }
From source file:org.candlepin.model.ColumnarResultIteratorTest.java
License:Open Source License
@Test public void testHasNextWithoutElements() { Query query = this.session.createQuery("SELECT o FROM Owner o"); ColumnarResultIterator<Owner> iterator = new ColumnarResultIterator<>(this.session, query.scroll(ScrollMode.FORWARD_ONLY), 0, false); try {//from ww w . ja va 2 s .co m assertFalse(iterator.hasNext()); } finally { iterator.close(); } }
From source file:org.candlepin.model.ColumnarResultIteratorTest.java
License:Open Source License
@Test public void testNextWithElements() { Owner owner1 = TestUtil.createOwner(); Owner owner2 = TestUtil.createOwner(); Owner owner3 = TestUtil.createOwner(); this.ownerCurator.create(owner1); this.ownerCurator.create(owner2); this.ownerCurator.create(owner3); Query query = this.session.createQuery("SELECT o FROM Owner o"); ColumnarResultIterator<Owner> iterator = new ColumnarResultIterator<>(this.session, query.scroll(ScrollMode.FORWARD_ONLY), 0, false); try {//from www .j a v a2s .c o m List<Owner> owners = new LinkedList<>(); // Note: Since we're testing everything in isolation here, we can't // be expecting .hasNext to be functional here. :) owners.add(iterator.next()); owners.add(iterator.next()); owners.add(iterator.next()); assertTrue(owners.contains(owner1)); assertTrue(owners.contains(owner2)); assertTrue(owners.contains(owner3)); } finally { iterator.close(); } }