Java Code Examples for org.hibernate.ScrollableResults#first()

The following examples show how to use org.hibernate.ScrollableResults#first() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FooPaginationPersistenceIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect() {
    final int pageSize = 10;
    final String hql = "FROM Foo f order by f.name";
    final Query query = session.createQuery(hql);

    final ScrollableResults resultScroll = query.scroll(ScrollMode.FORWARD_ONLY);

    // resultScroll.last();
    // final int totalResults = resultScroll.getRowNumber() + 1;

    resultScroll.first();
    resultScroll.scroll(0);
    final List<Foo> fooPage = Lists.newArrayList();
    int i = 0;
    while (pageSize > i++) {
        fooPage.add((Foo) resultScroll.get(0));
        if (!resultScroll.next()) {
            break;
        }
    }

    assertThat(fooPage, hasSize(lessThan(10 + 1)));
}
 
Example 2
Source File: ScrollableCollectionFetchingTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testScrollingJoinFetchesPositioning() {
	TestData data = new TestData();
	data.prepare();

	Session s = openSession();
	Transaction txn = s.beginTransaction();

	ScrollableResults results = s
	        .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
	        .setString( "desc", "root%" )
	        .scroll();

	results.first();
	Animal animal = ( Animal ) results.get( 0 );
	assertEquals( "first() did not return expected row", data.root1Id, animal.getId() );

	results.scroll( 1 );
	animal = ( Animal ) results.get( 0 );
	assertEquals( "scroll(1) did not return expected row", data.root2Id, animal.getId() );

	results.scroll( -1 );
	animal = ( Animal ) results.get( 0 );
	assertEquals( "scroll(-1) did not return expected row", data.root1Id, animal.getId() );

	results.setRowNumber( 1 );
	animal = ( Animal ) results.get( 0 );
	assertEquals( "setRowNumber(1) did not return expected row", data.root1Id, animal.getId() );

	results.setRowNumber( 2 );
	animal = ( Animal ) results.get( 0 );
	assertEquals( "setRowNumber(2) did not return expected row", data.root2Id, animal.getId() );

	txn.commit();
	s.close();

	data.cleanup();
}
 
Example 3
Source File: IndexHelper.java    From document-management-system with GNU General Public License v2.0 4 votes vote down vote up
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());
			}

			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;
}
 
Example 4
Source File: HibernateLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Create a {@link ScrollIterator}.
 *
 * @param sr scrollable result set
 */
public ScrollIterator(ScrollableResults sr) {
	this.sr = sr;
	hasNext = sr.first();
}