* 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();
}