List of usage examples for org.hibernate Query scroll
ScrollableResults scroll(ScrollMode scrollMode);
From source file:de.innovationgate.webgate.api.jdbc.HibernateQueryIterator.java
License:Open Source License
public HibernateQueryIterator(Query q, HibernateResultSet resultSet) { _resultSet = resultSet;/*from w w w. j av a 2s. c o m*/ long timeBefore = System.currentTimeMillis(); _set = q.scroll(ScrollMode.SCROLL_INSENSITIVE); long timeAfter = System.currentTimeMillis(); if (resultSet != null) { resultSet.setExecutionTime(timeAfter - timeBefore); } _endReached = !_set.next(); }
From source file:de.innovationgate.webgate.api.jdbc.StructEntryIterator.java
License:Open Source License
public StructEntryIterator(WGDatabaseImpl parent, Query q) { _parent = parent; _set = q.scroll(ScrollMode.SCROLL_INSENSITIVE); _endReached = !_set.next(); }
From source file:de.powerstaff.business.service.impl.ProfileIndexerServiceImpl.java
License:Open Source License
/** * Run the indexer./*from w w w . java 2 s . com*/ */ @Transactional public void runIndexer() { if (!systemParameterService.isIndexingEnabled()) { LOGGER.info("Indexing disabled"); return; } if (running) { LOGGER.info("Indexing already running"); } running = true; LOGGER.info("Running indexing"); readerFactory.initialize(); serviceLogger.logStart(SERVICE_ID, ""); // Jetzt luft er long theStartTime = System.currentTimeMillis(); try { int theFetchSize = 100; int theLogCount = theFetchSize * 10; Session theHibernateSession = sessionFactory.getCurrentSession(); FullTextSession theFT = Search.getFullTextSession(theHibernateSession); org.hibernate.Query theQuery = theHibernateSession.createQuery("from Freelancer"); theQuery.setFetchSize(theFetchSize); ScrollableResults theResults = theQuery.scroll(ScrollMode.FORWARD_ONLY); int counter = 0; while (theResults.next()) { Freelancer theFreelancer = (Freelancer) theResults.get(0); boolean needsToUpdate = true; TermQuery theTermQuery = new TermQuery(new Term("id", "" + theFreelancer.getId())); FullTextQuery theHibernateQuery = theFT.createFullTextQuery(theTermQuery, Freelancer.class); theHibernateQuery.setProjection(FullTextQuery.DOCUMENT); for (Object theSingleEntity : theHibernateQuery.list()) { needsToUpdate = false; Object[] theRow = (Object[]) theSingleEntity; Document theDocument = (Document) theRow[0]; long theNumberOfProfiles = Long.parseLong(theDocument.get(ProfileIndexerService.NUM_PROFILES)); List<FreelancerProfile> theProfiles = profileSearchService.loadProfilesFor(theFreelancer); if (theNumberOfProfiles != theProfiles.size()) { LOGGER.info("Updating freelancer " + theFreelancer.getId() + " as the number of profiles changed from " + theNumberOfProfiles + " to " + theProfiles.size()); needsToUpdate = true; } else { for (int i = 1; i <= theNumberOfProfiles; i++) { String theFileName = theDocument.get(ProfileIndexerService.PROFILE_PATH_PREFIX + i); File theFileOnServer = new File(theFileName); if (theFileOnServer.exists()) { long theModification = Long.parseLong( theDocument.get(ProfileIndexerService.PROFILE_MODIFICATION_PREFIX + i)); long theLastModified = theFileOnServer.lastModified() / 1000; if (theModification != theLastModified) { LOGGER.info("Updating freelancer " + theFreelancer.getId() + " as profile " + theFileOnServer + " was modified"); needsToUpdate = true; } } else { LOGGER.info("Updating freelancer " + theFreelancer.getId() + " as profile " + theFileOnServer + " seems to be deleted"); needsToUpdate = true; } } } } if (needsToUpdate) { theFT.index(theFreelancer); } if (counter % theLogCount == 0) { LOGGER.info("Processing record " + counter); } if (counter % theFetchSize == 0) { LOGGER.debug("Flushing session and index"); theFT.flushToIndexes(); theFT.clear(); theHibernateSession.clear(); } counter++; } } catch (Exception ex) { LOGGER.error("Error on indexing", ex); } finally { theStartTime = System.currentTimeMillis() - theStartTime; LOGGER.info("Indexing finished"); serviceLogger.logEnd(SERVICE_ID, "Dauer = " + theStartTime + "ms"); running = false; } }
From source file:de.powerstaff.business.service.impl.WrongDataServiceImpl.java
License:Open Source License
private void processFreelancer(File aReportFile) throws FileNotFoundException, ParseException { File theDBOhneProfil = new File(aReportFile, "Freiberufler_mit_Code_ohne_Profil.csv"); File theFreelancerOhneNewsletter = new File(aReportFile, "Freiberufler_ohne_Newsletter.csv"); File theFreelancerMitHomepageOhneKontakt = new File(aReportFile, "Freiberufler_mit_Homepage_ohne_Kontakt.csv"); File theFreelancerForNewsletter = new File(aReportFile, "Freiberufler_fr_Newsletter.csv"); File theProfileOhneDB = new File(aReportFile, "Profile_ohne_Datenbankeintrag.csv"); File theProfileDoppelterCode = new File(aReportFile, "Profile_Kodierung_doppelt.csv"); PrintWriter theDBOhneProfilWriter = null; PrintWriter theFreelancerOhneNewsletterWriter = null; PrintWriter theFreelancerMitHomepageOhneKontaktWriter = null; PrintWriter theFreelancerForNewsletterWriter = null; PrintWriter theProfileOhneDBWriter = null; PrintWriter theProfileDoppelterCodeWriter = null; FreelancerBackingBeanDataModel theModel = new FreelancerBackingBeanDataModel(); try {// w ww.j a v a 2s .c o m theProfileDoppelterCodeWriter = new PrintWriter(theProfileDoppelterCode); theDBOhneProfilWriter = new PrintWriter(theDBOhneProfil); theFreelancerOhneNewsletterWriter = new PrintWriter(theFreelancerOhneNewsletter); theFreelancerMitHomepageOhneKontaktWriter = new PrintWriter(theFreelancerMitHomepageOhneKontakt); theFreelancerForNewsletterWriter = new PrintWriter(theFreelancerForNewsletter); theProfileOhneDBWriter = new PrintWriter(theProfileOhneDB); theDBOhneProfilWriter.println("Kodierung;Name;Vorname;Kreditor"); theFreelancerOhneNewsletterWriter.println("Kodierung;Name;Vorname;Mail"); theFreelancerMitHomepageOhneKontaktWriter.println("Kodierung;Name;Vorname;Homepage"); theFreelancerForNewsletterWriter.println( "Krzel;Name;Vorname;Titel;eMail;Eintrag in Kreditor;Verfgbarkeit;Homepage;letzter Kontakt;Status;Xing;Gulp"); theProfileOhneDBWriter.println("Kodierung;Dateinamen"); theProfileDoppelterCodeWriter.println("Kodierung;Dateinamen"); boolean newsletterEnabled = systemParameterService.isNewsletterEnabled(); Set<String> theMails = new HashSet<String>(); Date theStartDate = null; DateFormat theDateFormat = new SimpleDateFormat("dd.MM.yyyy"); if (newsletterEnabled) { theStartDate = theDateFormat.parse(systemParameterService.getStartDateForNotInNewsletter()); for (NewsletterMail theMail : websiteDao.getConfirmedMails()) { theMails.add(theMail.getMail().toLowerCase()); } } Session theSession = sessionFactory.getCurrentSession(); int theFetchSize = 100; int theLogCount = theFetchSize * 10; Query theQuery = theSession.createQuery("from Freelancer"); theQuery.setFetchSize(theFetchSize); ScrollableResults theResults = theQuery.scroll(ScrollMode.FORWARD_ONLY); int counter = 0; Set<String> theKnownCodes = new HashSet<String>(); while (theResults.next()) { Freelancer theFreelancer = (Freelancer) theResults.get(0); String theCode = theFreelancer.getCode(); if (!StringUtils.isEmpty(theCode)) { theCode = theCode.toLowerCase(); theKnownCodes.add(theCode); Set<File> theFiles = fsCache.getFilesForCode(theCode); if ((theFiles == null || theFiles.size() == 0)) { theDBOhneProfilWriter.println(theCode + ";" + saveString(theFreelancer.getName1()) + ";" + saveString(theFreelancer.getName2()) + ";" + saveString(theFreelancer.getKreditorNr())); } } List<FreelancerContact> theMailContacts = theFreelancer.getEMailContacts(); List<FreelancerContact> theWebContacts = theFreelancer.getWebContacts(); Date theLastContact = theFreelancer.getLastContactDate(); if (!theFreelancer.isContactforbidden()) { String theMail = null; for (FreelancerContact theContact : theMailContacts) { if (StringUtils.isEmpty(theMail) && "eMail".equalsIgnoreCase(theContact.getType().getDescription())) { theMail = theContact.getValue(); } } String theWeb = ""; for (FreelancerContact theContact : theWebContacts) { if (StringUtils.isEmpty(theWeb) && "Web".equalsIgnoreCase(theContact.getType().getDescription())) { theWeb = theContact.getValue(); } } String theGulp = ""; for (FreelancerContact theContact : theWebContacts) { if (StringUtils.isEmpty(theWeb) && "Gulp".equalsIgnoreCase(theContact.getType().getDescription())) { theGulp = theContact.getValue(); } } String theXing = ""; for (FreelancerContact theContact : theWebContacts) { if (StringUtils.isEmpty(theWeb) && "Xing".equalsIgnoreCase(theContact.getType().getDescription())) { theXing = theContact.getValue(); } } String theAvailable = ""; Date theAvailability = theFreelancer.getAvailabilityAsDate(); if (theAvailability != null) { theAvailable = theDateFormat.format(theAvailability); } theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getCode())); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getName1())); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getName2())); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getTitel())); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theMail)); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getKreditorNr())); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theAvailable)); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theWeb)); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theLastContact)); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter .print(saveString(theModel.getStatusAsString(theFreelancer.getStatus()))); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theXing)); theFreelancerForNewsletterWriter.print(";"); theFreelancerForNewsletterWriter.print(saveString(theGulp)); theFreelancerForNewsletterWriter.println(); } if (newsletterEnabled) { if (theLastContact != null && !theFreelancer.isContactforbidden()) { String theMail = ""; boolean hasMail = false; for (FreelancerContact theContact : theMailContacts) { theMail = theContact.getValue(); if (theMails.contains(theMail.toLowerCase())) { hasMail = true; } } if (!hasMail) { theFreelancerOhneNewsletterWriter.println(theFreelancer.getCode() + ";" + theFreelancer.getName1() + ";" + theFreelancer.getName2() + ";" + theMail); } } } if (theLastContact == null) { boolean hasHomepage = false; String theHomepage = null; for (FreelancerContact theContact : theWebContacts) { theHomepage = theContact.getValue(); hasHomepage = true; } if (hasHomepage) { theFreelancerMitHomepageOhneKontaktWriter.println(theFreelancer.getCode() + ";" + theFreelancer.getName1() + ";" + theFreelancer.getName2() + ";" + theHomepage); } } if (counter % theLogCount == 0) { LOGGER.info("Processing record " + counter); } if (counter % theFetchSize == 0) { LOGGER.debug("Flushing session"); theSession.clear(); } counter++; } Set<String> theCodesFromFiles = new HashSet<String>(); theCodesFromFiles.addAll(fsCache.getKnownCodes()); for (String theCode : theCodesFromFiles) { Set<File> theFiles = fsCache.getFilesForCode(theCode); if (theFiles != null && theFiles.size() > 1) { // Doppelter Code StringBuilder theBuilder = new StringBuilder(); for (File theFile : theFiles) { if (theBuilder.length() > 0) { theBuilder.append(";"); } theBuilder.append(theFile.toString()); } theProfileDoppelterCodeWriter.println(theCode + ";" + theBuilder); } } theCodesFromFiles.removeAll(theKnownCodes); for (String theCode : theCodesFromFiles) { Set<File> theFiles = fsCache.getFilesForCode(theCode); if (theFiles != null) { for (File theFile : theFiles) { theProfileOhneDBWriter.println(theCode + ";" + theFile); } } } } catch (Exception e) { LOGGER.error("Error processing freelancer", e); } finally { IOUtils.closeQuietly(theDBOhneProfilWriter); IOUtils.closeQuietly(theFreelancerOhneNewsletterWriter); IOUtils.closeQuietly(theFreelancerMitHomepageOhneKontaktWriter); IOUtils.closeQuietly(theFreelancerForNewsletterWriter); IOUtils.closeQuietly(theProfileOhneDBWriter); IOUtils.closeQuietly(theProfileDoppelterCodeWriter); } }
From source file:de.tudarmstadt.ukp.lmf.transform.DBToXMLTransformer.java
License:Apache License
protected void doTransform(boolean includeAxes, final Lexicon... includeLexicons) throws SAXException { final int bufferSize = 100; commitCounter = 1;/* www.j a va 2 s. c o m*/ writeStartElement(lexicalResource); // Iterate over all lexicons if (includeLexicons == null || includeLexicons.length > 0) { for (Lexicon lexicon : lexicalResource.getLexicons()) { String lexiconName = lexicon.getName(); // Check if we want to include this lexicon. if (includeLexicons != null) { boolean found = false; for (Lexicon l : includeLexicons) { if (lexiconName.equals(l.getName())) { found = true; break; } } if (!found) { continue; } } logger.info("Processing lexicon: " + lexiconName); writeStartElement(lexicon); // Iterate over all possible sub-elements of this Lexicon and // write them to the XML Class<?>[] lexiconClassesToSave = { LexicalEntry.class, SubcategorizationFrame.class, SubcategorizationFrameSet.class, SemanticPredicate.class, Synset.class, SynSemCorrespondence.class, //ConstraintSet.class }; // "Unfortunately, MySQL does not treat large offset values efficiently by default and will still read all the rows prior to an offset value. It is common to see a query with an offset above 100,000 take over 20 times longer than an offset of zero!" // http://www.numerati.com/2012/06/26/reading-large-result-sets-with-hibernate-and-mysql/ for (Class<?> clazz : lexiconClassesToSave) { /*DetachedCriteria criteria = DetachedCriteria.forClass(clazz) .add(Restrictions.sqlRestriction("lexiconId = '" + lexicon.getId() + "'")); CriteriaIterator<Object> iter = new CriteriaIterator<Object>(criteria, sessionFactory, bufferSize); while (iter.hasNext()) { Object obj = iter.next(); writeElement(obj); session.evict(obj); commitCounter++; if (commitCounter % 1000 == 0) logger.info("progress: " + commitCounter + " class instances written to file"); }*/ Session lookupSession = sessionFactory.openSession(); Query query = lookupSession.createQuery("FROM " + clazz.getSimpleName() + " WHERE lexiconId = '" + lexicon.getId() + "' ORDER BY id"); query.setReadOnly(true); if (DBConfig.MYSQL.equals(dbConfig.getDBType())) { query.setFetchSize(Integer.MIN_VALUE); // MIN_VALUE gives hint to JDBC driver to stream results } else { query.setFetchSize(1000); } ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); while (results.next()) { // For streamed query results, no further queries are allowed (incl. lazy proxy queries!) // Detach the object from the lookup session and reload it using the "official" session. Object[] rows = results.get(); Object row = rows[0]; lookupSession.evict(row); lookupSession.evict(rows); rows = null; row = session.get(row.getClass(), ((IHasID) row).getId()); writeElement(row); session.evict(row); row = null; commitCounter++; if (commitCounter % 1000 == 0) { logger.info("progress: " + commitCounter + " class instances written to file"); } if (commitCounter % 10000 == 0) { closeSession(); openSession(); } } results.close(); lookupSession.close(); } writeEndElement(lexicon); } } // Iterate over SenseAxes and write them to XMLX when not only // lexicons should be converted if (includeAxes) { logger.info("Processing sense axes"); DetachedCriteria criteria = DetachedCriteria.forClass(SenseAxis.class) .add(Restrictions.sqlRestriction("lexicalResourceId = '" + lexicalResource.getName() + "'")); CriteriaIterator<Object> iter = new CriteriaIterator<Object>(criteria, sessionFactory, bufferSize); while (iter.hasNext()) { Object obj = iter.next(); writeElement(obj); session.evict(obj); commitCounter++; if (commitCounter % 1000 == 0) { logger.info("progress: " + commitCounter + " class instances written to file"); } } logger.info("Processing predicateargument axes"); DetachedCriteria criteria2 = DetachedCriteria.forClass(PredicateArgumentAxis.class) .add(Restrictions.sqlRestriction("lexicalResourceId = '" + lexicalResource.getName() + "'")); CriteriaIterator<Object> iter2 = new CriteriaIterator<Object>(criteria2, sessionFactory, bufferSize); while (iter2.hasNext()) { Object obj = iter2.next(); writeElement(obj); session.evict(obj); commitCounter++; if (commitCounter % 1000 == 0) { logger.info("progress: " + commitCounter + " class instances written to file"); } } } writeEndElement(lexicalResource); writeEndDocument(); }
From source file:debop4k.data.orm.hibernate.dao.HibernateDao.java
License:Apache License
public ScrollableResults scroll(@NonNull Query query, ScrollMode scrollMode) { return query.scroll(scrollMode); }
From source file:edu.scripps.fl.pubchem.app.ResultDownloader.java
License:Apache License
protected void process() throws Exception { Query query = PubChemDB.getSession().createSQLQuery( "select assay_aid from PCAssay where assay_version_changed = 'T' and assay_total_sid_count > 0 order by assay_aid"); Iterator<Integer> iter = new ScrollableResultsIterator(Integer.class, query.scroll(ScrollMode.FORWARD_ONLY)); process(iter);/*from w w w .j a v a2 s . c om*/ }
From source file:jp.go.nict.langrid.dao.hibernate.HibernateOverUseStateDao.java
License:Open Source License
@Override public OverUseStateSearchResult searchOverUseWithPeriod(int startIndex, int maxCount, String gridId, Calendar startDateTime, Calendar endDateTime, Order[] orders, Period period) throws DaoException { //## hibernate-postgresql???? startDateTime = CalendarUtil.toDefaultTimeZone(startDateTime); endDateTime = CalendarUtil.toDefaultTimeZone(endDateTime); if (orders.length == 0) { orders = new Order[] { new Order("lastAccessDateTime", OrderDirection.DESCENDANT) }; }// w w w . jav a2s. co m for (Order o : orders) { if (o.getFieldName().equals("period")) { o.setFieldName("ol.period"); } } Session session = getSession(); getContext().beginTransaction(); try { // AccessState Query qState = session.createQuery(countClouse + fromAndWhereClouseStateWithPeriod); qState.setString("gridId", gridId); qState.setCalendar("startDateTime", startDateTime); qState.setCalendar("endDateTime", endDateTime); qState.setInteger("period", period.ordinal()); long countState = (Long) qState.uniqueResult(); qState = session.createQuery(selectClouseState + fromAndWhereClouseStateWithPeriod + QueryUtil.buildOrderByQuery(Object.class, "ac", orders)); qState.setString("gridId", gridId); qState.setCalendar("startDateTime", startDateTime); qState.setCalendar("endDateTime", endDateTime); qState.setInteger("period", period.ordinal()); List<OverUseState> result = new ArrayList<OverUseState>(); ScrollableResults iState = qState.scroll(ScrollMode.FORWARD_ONLY); for (int i = 0; i < (startIndex + maxCount); i++) { OverUseState currentState = getNextOverUseState(iState); if (currentState == null) { break; } if (startIndex <= i) result.add(currentState); } OverUseStateSearchResult r = new OverUseStateSearchResult(result.toArray(new OverUseState[] {}), (int) (countState), true); getContext().commitTransaction(); return r; } catch (HibernateException e) { getContext().rollbackTransaction(); logAdditionalInfo(e); throw new DaoException(e); } catch (RuntimeException e) { getContext().rollbackTransaction(); throw new DaoException(e); } catch (Error e) { getContext().rollbackTransaction(); throw new DaoException(e); } }
From source file:magoffin.matt.dao.hbm.GenericHibernateDao.java
License:Open Source License
/** * Execute a batch callback using a named query. * /* ww w . j ava 2 s . c om*/ * @param queryName the named query name * @param parameters the named parameters to pass to the query * @param callback the callback * @return the number of items processed */ protected Integer executeNamedQueryBatchCallback(final String queryName, final Map<String, Object> parameters, final BatchCallback<T> callback) { return getHibernateTemplate().execute(new HibernateCallback<Integer>() { @SuppressWarnings("unchecked") @Override public Integer doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.getNamedQuery(queryName); if (parameters != null) { for (String paramName : parameters.keySet()) { q.setParameter(paramName, parameters.get(paramName)); } } q.setCacheMode(CacheMode.IGNORE); ScrollableResults items = q.scroll(ScrollMode.FORWARD_ONLY); int count = 0; OUTER: while (items.next()) { T item = (T) items.get(0); BatchCallbackResult action = callback.handle(item); switch (action) { case DELETE: session.delete(item); break; case UPDATE: case UPDATE_STOP: store(item); if (action == BatchCallbackResult.UPDATE_STOP) { break OUTER; } break; case STOP: break OUTER; case CONTINUE: // nothing to do break; } if (++count % batchFlushCount == 0) { session.flush(); session.clear(); } } return count; } }); }
From source file:mitm.application.djigzo.impl.hibernate.UserDAO.java
License:Open Source License
public CloseableIterator<String> getEmailIterator(Integer firstResult, Integer maxResults, SortDirection sortDirection) {//from ww w . ja v a 2 s . com 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 order by n.email " + sortDirection; Query query = createQuery(hql); if (firstResult != null) { query.setFirstResult(firstResult); } if (maxResults != null) { query.setMaxResults(maxResults); } ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY); return new EmailIterator(scrollableResults); }