Package org.hibernate

Examples of org.hibernate.ScrollableResults


   * of evict usage for memory management.
   */
  public void testScrollProjectionAndManaged() {
    Transaction tx = sess.beginTransaction();
    TermQuery tq = new TermQuery( new Term( "dept", "num") );
    ScrollableResults scrollableResults = sess
      .createFullTextQuery( tq, Employee.class )
      .setProjection(
          FullTextQuery.OBJECT_CLASS,
          FullTextQuery.ID,
          FullTextQuery.THIS,
          "lastname",
          FullTextQuery.THIS
          )
      .setFetchSize( 10 )
      .scroll();
    scrollableResults.last();
    assertEquals( 132, scrollableResults.getRowNumber() );
    scrollableResults.beforeFirst();
    assertEquals( -1, scrollableResults.getRowNumber() );
    int position = scrollableResults.getRowNumber();
    while ( scrollableResults.next() ) {
      position++;
      Object[] objs = scrollableResults.get();
      assertEquals( Employee.class, objs[0] );
      assertEquals( position, objs[1] );
      assertTrue( objs[2] instanceof Employee );
      sess.contains( objs[2] );
      assertEquals( "Rossi", objs[3] );
      assertTrue( objs[4] instanceof Employee );
      sess.contains( objs[4] );
      assertTrue( objs[2]==objs[4] ); //projected twice the same entity
      // detach some objects:
      if ( position%3 == 0 ) {
        sess.evict( objs[2] );
      }
    }
    //verify we scrolled to the end:
    assertEquals( 132, position );
    // and now the other way around, checking entities are attached again:
    while ( scrollableResults.previous() ) {
      position--;
      Object[] objs = scrollableResults.get();
      assertTrue( objs[2] instanceof Employee );
      sess.contains( objs[2] );
      assertTrue( objs[4] instanceof Employee );
      sess.contains( objs[4] );
      assertTrue( objs[2]==objs[4] );
    }
    assertEquals( -1, position );
    scrollableResults.close();
    tx.commit();
  }
View Full Code Here


    s.close();

    //check non created object does get found!!1
    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    ScrollableResults results = s.createCriteria( Email.class ).scroll( ScrollMode.FORWARD_ONLY );
    int index = 0;
    while ( results.next() ) {
      index++;
      s.index( results.get( 0 ) );
      if ( index % 5 == 0 ) s.clear();
    }
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
View Full Code Here

    s.close();

    //check non created object does get found!!1
    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    ScrollableResults results = s.createCriteria( Email.class ).scroll( ScrollMode.FORWARD_ONLY );
    int index = 0;
    while ( results.next() ) {
      index++;
      final Email o = (Email) results.get( 0 );
      s.index( o );
      if ( index % 5 == 0 ) {
        s.flushToIndexes();
        s.clear();
      }
View Full Code Here

        FullTextQuery.SCORE,
        FullTextQuery.DOCUMENT,
        FullTextQuery.ID
    );

    ScrollableResults projections = hibQuery.scroll();

    // There are a lot of methods to check in ScrollableResultsImpl
    // so, we'll use methods to check each projection as needed.

    projections.beforeFirst();
    projections.next();
    Object[] projection = projections.get();
    checkProjectionFirst( projection, s );
    assertTrue( projections.isFirst() );

    projections.last();
    projection = projections.get();
    checkProjectionLast( projection, s );
    assertTrue( projections.isLast() );

    projections.next();
    projection = projections.get();
    assertNull( projection );

    projections.previous();
    projection = projections.get();
    checkProjectionLast( projection, s );

    projections.first();
    projection = projections.get();
    checkProjectionFirst( projection, s );

    projections.scroll( 2 );
    projection = projections.get();
    checkProjection2( projection, s );

    projections.scroll( -5 );
    projection = projections.get();
    assertNull( projection );

    //cleanup
    for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
      s.delete( element );
View Full Code Here

   */
  public void testScrollingForward() {
    Transaction tx = sess.beginTransaction();
    TermQuery tq = new TermQuery( new Term( "summary", "number") );
    Sort sort = new Sort( new SortField( "summary" ) );
    ScrollableResults scrollableResults = sess
      .createFullTextQuery( tq, AlternateBook.class )
      .setSort( sort )
      .setFetchSize( 10 )
      .setFirstResult( 20 )
      .setMaxResults( 111 )
      .scroll();
    assertEquals( -1, scrollableResults.getRowNumber() );
    assertTrue( scrollableResults.last() );
    assertEquals( 110, scrollableResults.getRowNumber() );
    scrollableResults.beforeFirst();
    int position = scrollableResults.getRowNumber();
    while ( scrollableResults.next() ) {
      position++;
      int bookId = position + 20;
      assertEquals( position, scrollableResults.getRowNumber() );
      AlternateBook book = (AlternateBook) scrollableResults.get()[0];
      assertEquals( bookId, book.getId().intValue() );
      assertEquals( "book about the number " + bookId, book.getSummary() );
      assertTrue( sess.contains( book ) );
    }
    assertEquals( 110, position );
    scrollableResults.close();
    tx.commit();
  }
View Full Code Here

   */
  public void testScrollingBackwards() {
    Transaction tx = sess.beginTransaction();
    TermQuery tq = new TermQuery( new Term( "summary", "number") );
    Sort sort = new Sort( new SortField( "summary" ) );
    ScrollableResults scrollableResults = sess
      .createFullTextQuery( tq, AlternateBook.class )
      .setSort( sort )
      .setFetchSize( 10 )
      .scroll();
    scrollableResults.beforeFirst();
    // initial position should be -1 as in Hibernate Core
    assertEquals( -1, scrollableResults.getRowNumber() );
    assertTrue( scrollableResults.last() );
    int position = scrollableResults.getRowNumber();
    assertEquals( 323, position );
    while ( scrollableResults.previous() ) {
      AlternateBook book = (AlternateBook) scrollableResults.get()[0];
      assertEquals( --position, book.getId().intValue() );
      assertEquals( "book about the number " + position, book.getSummary() );
    }
    assertEquals( 0, position );
    assertEquals( -1, scrollableResults.getRowNumber() );
    scrollableResults.close();
    tx.commit();
  }
View Full Code Here

   */
  public void testResultsAreManaged() {
    Transaction tx = sess.beginTransaction();
    TermQuery tq = new TermQuery( new Term( "summary", "number") );
    Sort sort = new Sort( new SortField( "summary" ) );
    ScrollableResults scrollableResults = sess
      .createFullTextQuery( tq, AlternateBook.class )
      .setSort( sort )
      .setFetchSize( 10 )
      .scroll();
    int position = -1;
    while ( scrollableResults.next() ) {
      position++;
      AlternateBook book = (AlternateBook) scrollableResults.get()[0];
      assertTrue( sess.contains( book ) );
      // evict some entities:
      if ( position % 3 == 0 ) {
        sess.evict( book );
        assertFalse( sess.contains( book ) );
      }
    }
    //verifies it did scroll to the end:
    assertEquals( 323, position );
    //assert the entities are re-attached after eviction:
    while ( scrollableResults.previous() ) {
      position--;
      AlternateBook book = (AlternateBook) scrollableResults.get()[0];
      assertTrue( sess.contains( book ) );
    }
    assertEquals( -1, position );
    sess.clear();
    //assert the entities are re-attached after Session.clear:
    while ( scrollableResults.next() ) {
      position++;
      AlternateBook book = (AlternateBook) scrollableResults.get()[0];
      assertTrue( sess.contains( book ) );
    }
    assertEquals( 323, position );
    tx.commit();
  }
View Full Code Here

  }
 
  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;
  }
View Full Code Here

    assertTrue( "Incorrect result size", iter.hasNext() );
    obj = iter.next();
    assertTrue( "Incorrect return type", obj instanceof Map );
    assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 );

    ScrollableResults sr = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).scroll();
    assertTrue( "Incorrect result size", sr.next() );
    obj = sr.get(0);
    assertTrue( "Incorrect return type", obj instanceof Map );
    assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 );
    sr.close();

    sr = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).scroll();
    assertTrue( "Incorrect result size", sr.next() );
    assertTrue( "Incorrect return type", sr.get(0) instanceof Animal );
    sr.close();

    // caching...
    QueryStatistics stats = getSessions().getStatistics().getQueryStatistics( "select new Animal(an.description, an.bodyWeight) from Animal an" );
    results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" )
        .setCacheable( true )
View Full Code Here

    session.close();

    session = openSession();

    ScrollableResults sr = session.createQuery( query )
       .setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll();
    assertTrue( "Incorrect result size", sr.next() );
    assertTrue( "Incorrect return type", sr.get(0) instanceof Animal );
    assertFalse(session.contains(sr.get(0)));
    sr.close();

    session.close();

    session = openSession();
View Full Code Here

TOP

Related Classes of org.hibernate.ScrollableResults

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.