Use try-with-resources to ensure scroll and stream tests close underlying resources

This commit is contained in:
Christian Beikov 2023-08-15 12:39:57 +02:00
parent 0bef808d3e
commit 981ccc813e
26 changed files with 701 additions and 669 deletions

View File

@ -455,10 +455,10 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
q.setParameter( "count", new Integer( -1 ) ); q.setParameter( "count", new Integer( -1 ) );
assertTrue( q.list().size() == 1 ); assertTrue( q.list().size() == 1 );
ScrollableResults sr = s.createQuery( "from Simple s" ).scroll(); try (ScrollableResults sr = s.createQuery( "from Simple s" ).scroll()) {
sr.next(); sr.next();
sr.get(); sr.get();
sr.close(); }
s.delete( other ); s.delete( other );
s.delete( simple ); s.delete( simple );

View File

@ -92,14 +92,15 @@ public class BatchTest extends BaseCoreFunctionalTestCase {
s.setCacheMode( CacheMode.IGNORE ); s.setCacheMode( CacheMode.IGNORE );
t = s.beginTransaction(); t = s.beginTransaction();
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get(); DataPoint dp = (DataPoint) sr.get();
dp.setDescription( "done!" ); dp.setDescription( "done!" );
if ( ++i % nBeforeFlush == 0 ) { if ( ++i % nBeforeFlush == 0 ) {
s.flush(); s.flush();
s.clear(); s.clear();
}
} }
} }
t.commit(); t.commit();
@ -109,14 +110,15 @@ public class BatchTest extends BaseCoreFunctionalTestCase {
s.setCacheMode( CacheMode.IGNORE ); s.setCacheMode( CacheMode.IGNORE );
t = s.beginTransaction(); t = s.beginTransaction();
i = 0; i = 0;
sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get(); DataPoint dp = (DataPoint) sr.get();
s.delete( dp ); s.delete( dp );
if ( ++i % nBeforeFlush == 0 ) { if ( ++i % nBeforeFlush == 0 ) {
s.flush(); s.flush();
s.clear(); s.clear();
}
} }
} }
t.commit(); t.commit();

View File

@ -79,21 +79,22 @@ public class EagerManyToOneStreamTest {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE ); .setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream(); try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() ); List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( children.size() ).isEqualTo( 1 ); assertThat( children.size() ).isEqualTo( 1 );
Parent parent = children.get( 0 ).getParent(); Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull(); assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isTrue(); assertThat( Hibernate.isInitialized( parent ) ).isTrue();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
} }
); );
} }
@ -106,29 +107,30 @@ public class EagerManyToOneStreamTest {
session -> { session -> {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c ", Child.class ); .createQuery( "select c from Child as c ", Child.class );
Stream<Child> resultStream = query.getResultStream(); try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() ); List<Child> children = resultStream.collect( Collectors.toList() );
// with Stream the association is not batch loaded // with Stream the association is not batch loaded
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 );
assertThat( children.size() ).isEqualTo( 2 ); assertThat( children.size() ).isEqualTo( 2 );
Parent parent = children.get( 0 ).getParent(); Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull(); assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isTrue(); assertThat( Hibernate.isInitialized( parent ) ).isTrue();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 );
Parent parent1 = children.get( 1 ).getParent(); Parent parent1 = children.get( 1 ).getParent();
assertThat( parent1 ).isNotNull(); assertThat( parent1 ).isNotNull();
assertThat( Hibernate.isInitialized( parent1 ) ).isTrue(); assertThat( Hibernate.isInitialized( parent1 ) ).isTrue();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 );
assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 ); assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 3 );
}
} }
); );
} }
@ -140,9 +142,11 @@ public class EagerManyToOneStreamTest {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c", Child.class ); .createQuery( "select c from Child as c", Child.class );
query.getResultStream().forEach( try (Stream<Child> resultStream = query.getResultStream()) {
child -> assertThat( child.getParent() ).isNotNull() resultStream.forEach(
); child -> assertThat( child.getParent() ).isNotNull()
);
}
} }
); );
} }
@ -154,10 +158,11 @@ public class EagerManyToOneStreamTest {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE ); .setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream(); try (Stream<Child> resultStream = query.getResultStream()) {
Optional<Child> child = resultStream.findFirst(); Optional<Child> child = resultStream.findFirst();
assertThat( child.isEmpty() ).isFalse(); assertThat( child.isEmpty() ).isFalse();
assertThat( child.get().getParent() ).isNotNull(); assertThat( child.get().getParent() ).isNotNull();
}
} }
); );
} }

View File

@ -80,20 +80,21 @@ public class LazyManyToOneStreamTest {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE ); .setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream(); try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() ); List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( children.size() ).isEqualTo( 1 ); assertThat( children.size() ).isEqualTo( 1 );
Parent parent = children.get( 0 ).getParent(); Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull(); assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isFalse(); assertThat( Hibernate.isInitialized( parent ) ).isFalse();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
} }
); );
} }
@ -106,29 +107,30 @@ public class LazyManyToOneStreamTest {
session -> { session -> {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c order by id", Child.class ); .createQuery( "select c from Child as c order by id", Child.class );
Stream<Child> resultStream = query.getResultStream(); try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() ); List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( children.size() ).isEqualTo( 2 ); assertThat( children.size() ).isEqualTo( 2 );
Parent parent = children.get( 0 ).getParent(); Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull(); assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isFalse(); assertThat( Hibernate.isInitialized( parent ) ).isFalse();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE ); assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
Parent parent1 = children.get( 1 ).getParent(); Parent parent1 = children.get( 1 ).getParent();
assertThat( parent1 ).isNotNull(); assertThat( parent1 ).isNotNull();
// parent2 has been batch loaded // parent2 has been batch loaded
assertThat( Hibernate.isInitialized( parent1 ) ).isTrue(); assertThat( Hibernate.isInitialized( parent1 ) ).isTrue();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 ); assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 ); assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
} }
); );
} }
@ -140,15 +142,16 @@ public class LazyManyToOneStreamTest {
QueryImplementor<Child> query = session QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class ) .createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE ); .setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream(); try (Stream<Child> resultStream = query.getResultStream()) {
Optional<Child> child = resultStream.findFirst(); Optional<Child> child = resultStream.findFirst();
assertThat( child.isEmpty() ).isFalse(); assertThat( child.isEmpty() ).isFalse();
Parent parent = child.get().getParent(); Parent parent = child.get().getParent();
assertThat( parent ).isNotNull(); assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isFalse(); assertThat( Hibernate.isInitialized( parent ) ).isFalse();
}
} }
); );
} }

View File

@ -138,13 +138,14 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
final String qry = "select d from D d"; final String qry = "select d from D d";
final Query query = session.createQuery( qry ); final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query ); try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
final DEntity dEntity = (DEntity) scrollableResults.get(); final DEntity dEntity = (DEntity) scrollableResults.get();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyString" ) ); assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyString" ) );
assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyStringBlobGroup" ) ); assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyStringBlobGroup" ) );
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
}
} }
} }
); );
@ -231,12 +232,13 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
final String qry = "select e from E e join fetch e.d"; final String qry = "select e from E e join fetch e.d";
final Query query = session.createQuery( qry ); final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query ); try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
final EEntity eEntity = (EEntity) scrollableResults.get(); final EEntity eEntity = (EEntity) scrollableResults.get();
final DEntity dEntity = eEntity.getD(); final DEntity dEntity = eEntity.getD();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
}
} }
} }
); );
@ -251,14 +253,15 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
"join fetch d.g"; "join fetch d.g";
final Query query = session.createQuery( qry ); final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query ); try (ScrollableResults scrollableResults = getScrollableResults( query )) {
int i = 0; int i = 0;
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
i++; i++;
final DEntity dEntity = (DEntity) scrollableResults.get(); final DEntity dEntity = (DEntity) scrollableResults.get();
assertThat( dEntity.getBs().size(), is( 2 ) ); assertThat( dEntity.getBs().size(), is( 2 ) );
}
assertThat( i, is( 1 ) );
} }
assertThat( i, is( 1 ) );
} }
); );
@ -267,9 +270,10 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
final String qry = "select g from G g join fetch g.dEntities"; final String qry = "select g from G g join fetch g.dEntities";
final Query query = session.createQuery( qry ); final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query ); try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
final Object o = scrollableResults.get(); final Object o = scrollableResults.get();
}
} }
} }
); );
@ -296,16 +300,17 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
"join fetch d.g"; "join fetch d.g";
final Query query = session.createQuery( qry ); final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query ); try (ScrollableResults scrollableResults = getScrollableResults( query )) {
int i = 0; int i = 0;
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
i++; i++;
final Object[] result = (Object[]) scrollableResults.get(); final Object[] result = (Object[]) scrollableResults.get();
final DEntity dEntity = (DEntity) result[0]; final DEntity dEntity = (DEntity) result[0];
assertThat( dEntity.getBs().size(), is( 2 ) ); assertThat( dEntity.getBs().size(), is( 2 ) );
assertThat( result[1], is( "bla" ) ); assertThat( result[1], is( "bla" ) );
}
assertThat( i, is( 1 ) );
} }
assertThat( i, is( 1 ) );
} }
); );
} }

View File

@ -111,24 +111,24 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
// both scroll() and iterate() cause batching to hold on // both scroll() and iterate() cause batching to hold on
// to resources, which should make aggressive-release not release // to resources, which should make aggressive-release not release
// the connection (and thus cause serialization to fail) // the connection (and thus cause serialization to fail)
ScrollableResults sr = s.createQuery( "from Silly" ).scroll(); try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) {
sr.next(); sr.next();
try { try {
SerializationHelper.serialize( s ); SerializationHelper.serialize( s );
fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" ); fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
}
catch (IllegalStateException e) {
// expected behavior
}
// getting the first row only because SybaseASE15Dialect throws NullPointerException
// if data is not read before closing the ResultSet
sr.next();
// Closing the ScrollableResults does currently force batching to
// aggressively release the connection
} }
catch( IllegalStateException e ) {
// expected behavior
}
// getting the first row only because SybaseASE15Dialect throws NullPointerException
// if data is not read before closing the ResultSet
sr.next();
// Closing the ScrollableResults does currently force batching to
// aggressively release the connection
sr.close();
SerializationHelper.serialize( s ); SerializationHelper.serialize( s );
s.delete( silly ); s.delete( silly );
@ -146,22 +146,19 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
s.save( silly ); s.save( silly );
s.flush(); s.flush();
ScrollableResults sr = s.createQuery( "from Silly" ).scroll(); try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) {
assertTrue( sr.next() ); assertTrue( sr.next() );
Silly silly2 = ( Silly ) sr.get(); Silly silly2 = (Silly) sr.get();
assertEquals( silly, silly2 ); assertEquals( silly, silly2 );
sr.close(); }
sr = s.createQuery( "from Silly" ).scroll(); try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll(); ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll()) {
assertTrue( sr.next() );
assertTrue( sr.next() ); assertEquals( silly, sr.get() );
assertEquals( silly, sr.get() ); assertTrue( sr2.next() );
assertTrue( sr2.next() ); assertEquals( silly, sr2.get() );
assertEquals( silly, sr2.get() ); }
sr.close();
sr2.close();
s.delete( silly ); s.delete( silly );
s.flush(); s.flush();

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.orm.test.connections; package org.hibernate.orm.test.connections;
import org.hibernate.ScrollableResults;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
@ -134,19 +135,21 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
Session sessionUnderTest = getSessionUnderTest(); Session sessionUnderTest = getSessionUnderTest();
// force the connection to be retained // force the connection to be retained
sessionUnderTest.createQuery( "from Silly" ).scroll().next(); try (ScrollableResults sr = sessionUnderTest.createQuery( "from Silly" ).scroll()) {
sr.next();
try { try {
SerializationHelper.serialize( sessionUnderTest ); SerializationHelper.serialize( sessionUnderTest );
fail( "Serialization of connected session allowed!" ); fail( "Serialization of connected session allowed!" );
} }
catch( IllegalStateException e ) { catch (IllegalStateException e) {
// expected behaviour // expected behaviour
} }
finally { finally {
release( sessionUnderTest ); release( sessionUnderTest );
done(); done();
}
} }
} }
@ -247,18 +250,19 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
sessionUnderTest.save( silly ); sessionUnderTest.save( silly );
sessionUnderTest.flush(); sessionUnderTest.flush();
sessionUnderTest.createQuery( "from Silly" ).scroll(); try (ScrollableResults sr = sessionUnderTest.createQuery( "from Silly" ).scroll()) {
disconnect( sessionUnderTest ); disconnect( sessionUnderTest );
SerializationHelper.serialize( sessionUnderTest ); SerializationHelper.serialize( sessionUnderTest );
checkSerializedState( sessionUnderTest ); checkSerializedState( sessionUnderTest );
reconnect( sessionUnderTest ); reconnect( sessionUnderTest );
sessionUnderTest.delete( silly ); sessionUnderTest.delete( silly );
sessionUnderTest.flush(); sessionUnderTest.flush();
release( sessionUnderTest ); release( sessionUnderTest );
done(); done();
}
} }
/** /**

View File

@ -3266,17 +3266,17 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
assertTrue( ( (Map) results.get( 0 ) ).containsKey("descr") ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("descr") );
assertTrue( ( (Map) results.get( 0 ) ).containsKey("bw") ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("bw") );
ScrollableResults sr = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).scroll(); try (ScrollableResults sr = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).scroll()) {
assertTrue( "Incorrect result size", sr.next() ); assertTrue( "Incorrect result size", sr.next() );
obj = sr.get(); obj = sr.get();
assertTrue( "Incorrect return type", obj instanceof Map ); assertTrue( "Incorrect return type", obj instanceof Map );
assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 ); assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 );
sr.close(); }
sr = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).scroll(); try (ScrollableResults sr = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).scroll()) {
assertTrue( "Incorrect result size", sr.next() ); assertTrue( "Incorrect result size", sr.next() );
assertTrue( "Incorrect return type", sr.get() instanceof Animal ); assertTrue( "Incorrect return type", sr.get() instanceof Animal );
sr.close(); }
// caching... // caching...
QueryStatistics stats = sessionFactory().getStatistics().getQueryStatistics( "select new Animal(an.description, an.bodyWeight) from Animal an" ); QueryStatistics stats = sessionFactory().getStatistics().getQueryStatistics( "select new Animal(an.description, an.bodyWeight) from Animal an" );
@ -3557,13 +3557,12 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
t = session.beginTransaction(); t = session.beginTransaction();
ScrollableResults sr = session.createQuery( query ) try (ScrollableResults sr = session.createQuery( query )
.setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll(); .setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll()) {
assertTrue( "Incorrect result size", sr.next() );
assertTrue( "Incorrect result size", sr.next() ); assertTrue( "Incorrect return type", sr.get() instanceof Animal );
assertTrue( "Incorrect return type", sr.get() instanceof Animal ); assertFalse( session.contains( sr.get() ) );
assertFalse( session.contains( sr.get() ) ); }
sr.close();
t.commit(); t.commit();
session.close(); session.close();
@ -3620,12 +3619,11 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
session = openSession(); session = openSession();
t = session.beginTransaction(); t = session.beginTransaction();
ScrollableResults sr = session.createQuery( query ) try (ScrollableResults sr = session.createQuery( query )
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll(); .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).scroll()) {
assertTrue( "Incorrect result size", sr.next() );
assertTrue( "Incorrect result size", sr.next() ); assertTrue( "Incorrect return type", sr.get() instanceof Map );
assertTrue( "Incorrect return type", sr.get() instanceof Map ); }
sr.close();
t.commit(); t.commit();
session.close(); session.close();
@ -3815,7 +3813,7 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
s.beginTransaction(); s.beginTransaction();
Query query = s.createQuery( hql ); Query query = s.createQuery( hql );
preparer.prepare( query ); preparer.prepare( query );
query.scroll(); query.scroll().close();
s.getTransaction().commit(); s.getTransaction().commit();
s.close(); s.close();
return this; return this;

View File

@ -960,20 +960,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
//end::jpql-api-stream-example[] //end::jpql-api-stream-example[]
// tag::jpql-api-stream-terminal-operation[]
List<Person> persons = entityManager.createQuery(
"select p " +
"from Person p " +
"where p.name like :name",
Person.class)
.setParameter("name", "J%")
.getResultStream()
.skip(5)
.limit(5)
.collect(Collectors.toList());
//end::jpql-api-stream-terminal-operation[]
}); });
} }

View File

@ -46,8 +46,8 @@ public class ScrollableCollectionFetchingTest {
public void testTupleReturnFails(SessionFactoryScope scope) { public void testTupleReturnFails(SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction(
session -> { session -> {
try { try (ScrollableResults<?> sr = session.createQuery(
session.createQuery( "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll(); "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll()) {
fail( "scroll allowed with collection fetch and reurning tuples" ); fail( "scroll allowed with collection fetch and reurning tuples" );
} }
catch (IllegalArgumentException e) { catch (IllegalArgumentException e) {
@ -74,55 +74,56 @@ public class ScrollableCollectionFetchingTest {
assertEquals( 0, size ); assertEquals( 0, size );
// now get the scrollable results // now get the scrollable results
ScrollableResults results = s.createQuery( query ).setParameter( "desc", "root%" ).scroll(); try (ScrollableResults results = s.createQuery( query ).setParameter( "desc", "root%" ).scroll()) {
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
results.beforeFirst();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.first() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
results.afterLast();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.last() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
for ( int i = 1; i < 3; i++ ) {
assertFalse( results.scroll( i ) );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertFalse( results.scroll( -i ) ); assertFalse( results.next() );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertFalse( results.setRowNumber( i ) ); assertFalse( results.previous() );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertFalse( results.setRowNumber( -i ) ); results.beforeFirst();
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.first() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
results.afterLast();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.last() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
for ( int i = 1; i < 3; i++ ) {
assertFalse( results.scroll( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.scroll( -i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( -i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
}
} }
} }
); );
@ -158,84 +159,85 @@ public class ScrollableCollectionFetchingTest {
.setParameter( "desc", "root%" ) .setParameter( "desc", "root%" )
.uniqueResult() ); .uniqueResult() );
ScrollableResults results = session try (ScrollableResults results = session
.createQuery( .createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" ) "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" ).scroll(); .setParameter( "desc", "root%" ).scroll()) {
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertFalse( results.previous() ); assertFalse( results.previous() );
assertTrue( results.next() ); assertTrue( results.next() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertTrue( results.previous() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertTrue( results.next() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
results.beforeFirst();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
assertTrue( results.first() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
results.afterLast();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertTrue( results.last() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
assertTrue( results.first() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
for ( int i = 1; i < 3; i++ ) {
assertTrue( results.setRowNumber( 1 ) );
assertTrue( results.isFirst() ); assertTrue( results.isFirst() );
assertTrue( results.isLast() ); assertTrue( results.isLast() );
assertFalse( results.scroll( i ) ); assertFalse( results.next() );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertTrue( results.setRowNumber( 1 ) ); assertTrue( results.previous() );
assertTrue( results.isFirst() ); assertTrue( results.isFirst() );
assertTrue( results.isLast() ); assertTrue( results.isLast() );
assertFalse( results.scroll( -i ) ); assertFalse( results.previous() );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
if ( i != 1 ) { assertTrue( results.next() );
assertFalse( results.setRowNumber( i ) ); assertTrue( results.isFirst() );
assertTrue( results.isLast() );
results.beforeFirst();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
assertTrue( results.first() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
results.afterLast();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertTrue( results.last() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
assertTrue( results.first() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
for ( int i = 1; i < 3; i++ ) {
assertTrue( results.setRowNumber( 1 ) );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.scroll( i ) );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
assertFalse( results.setRowNumber( -i ) ); assertTrue( results.setRowNumber( 1 ) );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.scroll( -i ) );
assertFalse( results.isFirst() ); assertFalse( results.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
if ( i != 1 ) {
assertFalse( results.setRowNumber( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( -i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
}
} }
} }
} }
@ -260,20 +262,20 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction( scope.inTransaction(
s -> { s -> {
ScrollableResults results = s try (ScrollableResults results = s
.createQuery( .createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" ) "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" ) .setParameter( "desc", "root%" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
int counter = 0; int counter = 0;
while ( results.next() ) { while ( results.next() ) {
counter++; counter++;
Animal animal = (Animal) results.get(); Animal animal = (Animal) results.get();
checkResult( animal ); checkResult( animal );
}
assertEquals( 2, counter, "unexpected result count" );
} }
assertEquals( 2, counter, "unexpected result count" );
} }
); );
} }
@ -287,21 +289,21 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction( scope.inTransaction(
s -> { s -> {
ScrollableResults results = s try (ScrollableResults results = s
.createQuery( .createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" ) "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" ).scroll(); .setParameter( "desc", "root%" ).scroll()) {
results.afterLast(); results.afterLast();
int counter = 0; int counter = 0;
while ( results.previous() ) { while ( results.previous() ) {
counter++; counter++;
Animal animal = (Animal) results.get(); Animal animal = (Animal) results.get();
checkResult( animal ); checkResult( animal );
}
assertEquals( 2, counter, "unexpected result count" );
} }
assertEquals( 2, counter, "unexpected result count" );
} }
); );
} }
@ -315,31 +317,32 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction( scope.inTransaction(
session -> { session -> {
ScrollableResults results = session try (ScrollableResults results = session
.createQuery( .createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" ) "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" ) .setParameter( "desc", "root%" )
.scroll(); .scroll()) {
results.first(); results.first();
Animal animal = (Animal) results.get(); Animal animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "first() did not return expected row" ); assertEquals( data.root1Id, animal.getId(), "first() did not return expected row" );
results.scroll( 1 ); results.scroll( 1 );
animal = (Animal) results.get(); animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "scroll(1) did not return expected row" ); assertEquals( data.root2Id, animal.getId(), "scroll(1) did not return expected row" );
results.scroll( -1 ); results.scroll( -1 );
animal = (Animal) results.get(); animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "scroll(-1) did not return expected row" ); assertEquals( data.root1Id, animal.getId(), "scroll(-1) did not return expected row" );
results.setRowNumber( 1 ); results.setRowNumber( 1 );
animal = (Animal) results.get(); animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "setRowNumber(1) did not return expected row" ); assertEquals( data.root1Id, animal.getId(), "setRowNumber(1) did not return expected row" );
results.setRowNumber( 2 ); results.setRowNumber( 2 );
animal = (Animal) results.get(); animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" ); assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" );
}
} }
); );
} }

View File

@ -55,9 +55,10 @@ public class ScrollableResultsObjectArrayCastingTest {
scope.inTransaction( scope.inTransaction(
(entityManager) -> { (entityManager) -> {
TypedQuery<byte[]> typedQuery = entityManager.createQuery( "select p.binaryValue from Product p", byte[].class ); TypedQuery<byte[]> typedQuery = entityManager.createQuery( "select p.binaryValue from Product p", byte[].class );
Stream<byte[]> stream = typedQuery.getResultStream(); try (Stream<byte[]> stream = typedQuery.getResultStream()) {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
stream.findFirst(); stream.findFirst();
}
} }
); );
} }

View File

@ -51,18 +51,19 @@ public class ScrollTest {
scope.inTransaction( scope.inTransaction(
s -> { s -> {
ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll(); try (ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll()) {
assertTrue( sr.next() ); assertTrue( sr.next() );
Item i1 = (Item) sr.get(); Item i1 = (Item) sr.get();
assertTrue( sr.next() ); assertTrue( sr.next() );
Item i2 = (Item) sr.get(); Item i2 = (Item) sr.get();
assertTrue( Hibernate.isInitialized( i1 ) ); assertTrue( Hibernate.isInitialized( i1 ) );
assertTrue( Hibernate.isInitialized( i2 ) ); assertTrue( Hibernate.isInitialized( i2 ) );
assertThat( i1.getName(), is( "foo" ) ); assertThat( i1.getName(), is( "foo" ) );
assertThat( i2.getName(), is( "bar" ) ); assertThat( i2.getName(), is( "bar" ) );
assertFalse( sr.next() ); assertFalse( sr.next() );
s.delete( i1 ); s.delete( i1 );
s.delete( i2 ); s.delete( i2 );
}
} }
); );

View File

@ -113,24 +113,26 @@ public class IdentityJoinedSubclassBatchingTest {
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
e.setTitle( "Unknown" ); e.setTitle( "Unknown" );
}
} }
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
s.delete( e ); s.delete( e );
}
} }
} ); } );
} }
@ -158,13 +160,14 @@ public class IdentityJoinedSubclassBatchingTest {
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
s.delete( e ); s.delete( e );
}
} }
} ); } );
@ -191,13 +194,14 @@ public class IdentityJoinedSubclassBatchingTest {
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
s.delete( e ); s.delete( e );
}
} }
} ); } );

View File

@ -106,24 +106,26 @@ public class JoinedSubclassBatchingTest {
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
e.setTitle( "Unknown" ); e.setTitle( "Unknown" );
}
} }
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
s.delete( e ); s.delete( e );
}
} }
} ); } );
} }

View File

@ -111,24 +111,26 @@ public class SequenceJoinedSubclassBatchingTest {
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
e.setTitle( "Unknown" ); e.setTitle( "Unknown" );
}
} }
} ); } );
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
s.delete( e ); s.delete( e );
}
} }
} ); } );
} }
@ -153,13 +155,14 @@ public class SequenceJoinedSubclassBatchingTest {
scope.inTransaction( s -> { scope.inTransaction( s -> {
ScrollableResults sr = s.createQuery( try (ScrollableResults sr = s.createQuery(
"select e from Employee e" ) "select e from Employee e" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
Employee e = (Employee) sr.get(); Employee e = (Employee) sr.get();
s.delete( e ); s.delete( e );
}
} }
} ); } );

View File

@ -218,9 +218,9 @@ public class CriteriaCompilingTest extends BaseEntityManagerFunctionalTestCase {
TypedQuery<Order> jpaQuery = session.createQuery( query ); TypedQuery<Order> jpaQuery = session.createQuery( query );
Query<?> hibQuery = jpaQuery.unwrap( Query.class ); Query<?> hibQuery = jpaQuery.unwrap( Query.class );
ScrollableResults sr = hibQuery.scroll( ScrollMode.FORWARD_ONLY ); hibQuery.scroll( ScrollMode.FORWARD_ONLY ).close();
hibQuery.setCacheMode( CacheMode.IGNORE ).scroll( ScrollMode.FORWARD_ONLY ); hibQuery.setCacheMode( CacheMode.IGNORE ).scroll( ScrollMode.FORWARD_ONLY ).close();
Query<Order> anotherQuery = session.createQuery( Query<Order> anotherQuery = session.createQuery(
"select o from Order o where totalPrice in :totalPrices", "select o from Order o where totalPrice in :totalPrices",

View File

@ -85,12 +85,13 @@ public class CriteriaToScrollableResultsFetchTest extends BaseEntityManagerFunct
hibernateQuery.setCacheable( false ); hibernateQuery.setCacheable( false );
List<OrderLine> lines = new ArrayList<>(); List<OrderLine> lines = new ArrayList<>();
ScrollableResults scrollableResults = hibernateQuery.scroll(); try (ScrollableResults scrollableResults = hibernateQuery.scroll()) {
scrollableResults.last(); scrollableResults.last();
int rows = scrollableResults.getRowNumber() + 1; int rows = scrollableResults.getRowNumber() + 1;
scrollableResults.beforeFirst(); scrollableResults.beforeFirst();
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
lines.add( (OrderLine) scrollableResults.get( ) ); lines.add( (OrderLine) scrollableResults.get() );
}
} }
assertNotNull( lines ); assertNotNull( lines );
assertEquals( "Expected one order line", 1, lines.size() ); assertEquals( "Expected one order line", 1, lines.size() );

View File

@ -25,8 +25,9 @@ public class ProcedureCallImplTest {
scope.inTransaction( em -> { scope.inTransaction( em -> {
em.createNativeQuery("CREATE ALIAS GET_RANDOM_VALUE FOR \"java.lang.Math.random\";").executeUpdate(); em.createNativeQuery("CREATE ALIAS GET_RANDOM_VALUE FOR \"java.lang.Math.random\";").executeUpdate();
Query query = em.createStoredProcedureQuery("GET_RANDOM_VALUE"); Query query = em.createStoredProcedureQuery("GET_RANDOM_VALUE");
Stream stream = query.getResultStream(); try (Stream stream = query.getResultStream()) {
Assert.assertEquals(1, stream.count()); Assert.assertEquals( 1, stream.count() );
}
} ); } );
} }
} }

View File

@ -47,8 +47,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isInstanceOf( Product.class ); assertThat( result ).isInstanceOf( Product.class );
} }
{ try (ScrollableResults<?> results = query.scroll()) {
final ScrollableResults<?> results = query.scroll();
assertThat( results.next() ).isTrue(); assertThat( results.next() ).isTrue();
final Object result = results.get(); final Object result = results.get();
assertThat( result ).isInstanceOf( Product.class ); assertThat( result ).isInstanceOf( Product.class );
@ -69,8 +68,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isNotNull(); assertThat( result ).isNotNull();
} }
{ try (ScrollableResults<Product> results = query.scroll()) {
final ScrollableResults<Product> results = query.scroll();
assertThat( results.next() ).isTrue(); assertThat( results.next() ).isTrue();
final Product result = results.get(); final Product result = results.get();
assertThat( result ).isNotNull(); assertThat( result ).isNotNull();
@ -94,8 +92,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result[ 1 ] ).isNotNull(); assertThat( result[ 1 ] ).isNotNull();
} }
{ try (final ScrollableResults<Object[]> results = query.scroll()) {
final ScrollableResults<Object[]> results = query.scroll();
assertThat( results.next() ).isTrue(); assertThat( results.next() ).isTrue();
final Object[] result = results.get(); final Object[] result = results.get();
assertThat( results.next() ).isFalse(); assertThat( results.next() ).isFalse();
@ -123,8 +120,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result[ 0 ] ).isInstanceOf( Product.class ); assertThat( result[ 0 ] ).isInstanceOf( Product.class );
} }
{ try (ScrollableResults<Object[]> results = query.scroll()) {
final ScrollableResults<Object[]> results = query.scroll();
assertThat( results.next() ).isTrue(); assertThat( results.next() ).isTrue();
final Object[] result = results.get(); final Object[] result = results.get();
assertThat( results.next() ).isFalse(); assertThat( results.next() ).isFalse();
@ -148,8 +144,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isNotNull(); assertThat( result ).isNotNull();
} }
{ try (ScrollableResults<Product> results = query.scroll()) {
final ScrollableResults<Product> results = query.scroll();
assertThat( results.next() ).isTrue(); assertThat( results.next() ).isTrue();
final Product result = results.get(); final Product result = results.get();
assertThat( result ).isNotNull(); assertThat( result ).isNotNull();

View File

@ -68,54 +68,54 @@ public class ScrollableResultsTests {
scope.inTransaction( scope.inTransaction(
session -> { session -> {
final QueryImplementor<String> query = session.createQuery( SINGLE_SELECTION_QUERY, String.class ); final QueryImplementor<String> query = session.createQuery( SINGLE_SELECTION_QUERY, String.class );
final ScrollableResultsImplementor<String> results = query.scroll( ScrollMode.SCROLL_INSENSITIVE ); try (ScrollableResultsImplementor<String> results = query.scroll( ScrollMode.SCROLL_INSENSITIVE )) {
// try to initially read in reverse - should be false // try to initially read in reverse - should be false
assertThat( results.previous(), is( false ) ); assertThat( results.previous(), is( false ) );
// position at the first row // position at the first row
assertThat( results.next(), is( true ) ); assertThat( results.next(), is( true ) );
String data = results.get(); String data = results.get();
assertThat( data, is( "other" ) ); assertThat( data, is( "other" ) );
// position at the second (last) row // position at the second (last) row
assertThat( results.next(), is( true ) ); assertThat( results.next(), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "value" ) ); assertThat( data, is( "value" ) );
// position after the second (last) row // position after the second (last) row
assertThat( results.next(), is( false ) ); assertThat( results.next(), is( false ) );
// position back to the second row // position back to the second row
assertThat( results.previous(), is( true ) ); assertThat( results.previous(), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "value" ) ); assertThat( data, is( "value" ) );
// position back to the first row // position back to the first row
assertThat( results.previous(), is( true ) ); assertThat( results.previous(), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "other" ) ); assertThat( data, is( "other" ) );
// position before the first row // position before the first row
assertThat( results.previous(), is( false ) ); assertThat( results.previous(), is( false ) );
assertThat( results.previous(), is( false ) ); assertThat( results.previous(), is( false ) );
assertThat( results.last(), is( true ) ); assertThat( results.last(), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "value" ) ); assertThat( data, is( "value" ) );
assertThat( results.first(), is( true ) ); assertThat( results.first(), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "other" ) ); assertThat( data, is( "other" ) );
assertThat( results.scroll( 1 ), is( true ) ); assertThat( results.scroll( 1 ), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "value" ) ); assertThat( data, is( "value" ) );
assertThat( results.scroll( -1 ), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
assertThat( results.scroll( -1 ), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
}
} }
); );
} }
@ -262,7 +262,7 @@ public class ScrollableResultsTests {
} }
private static <R> void verifyScroll(Query<R> query, Consumer<R> validator) { private static <R> void verifyScroll(Query<R> query, Consumer<R> validator) {
try ( final ScrollableResults<R> results = query.scroll( ScrollMode.FORWARD_ONLY ) ) { try (final ScrollableResults<R> results = query.scroll( ScrollMode.FORWARD_ONLY ) ) {
assertThat( results.next(), is( true ) ); assertThat( results.next(), is( true ) );
validator.accept( results.get() ); validator.accept( results.get() );
} }

View File

@ -109,15 +109,16 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
t = s.beginTransaction(); t = s.beginTransaction();
s.setDefaultReadOnly( true ); s.setDefaultReadOnly( true );
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
s.setDefaultReadOnly( false ); s.setDefaultReadOnly( false );
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get(); DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) { if ( ++i == 50 ) {
s.setReadOnly( dp, false ); s.setReadOnly( dp, false );
}
dp.setDescription( "done!" );
} }
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();
@ -148,15 +149,16 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
t = s.beginTransaction(); t = s.beginTransaction();
s.setDefaultReadOnly( true ); s.setDefaultReadOnly( true );
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false ) .setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get(); DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) { if ( ++i == 50 ) {
s.setReadOnly( dp, true ); s.setReadOnly( dp, true );
}
dp.setDescription( "done!" );
} }
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();
@ -187,15 +189,16 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
t = s.beginTransaction(); t = s.beginTransaction();
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true ) .setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get(); DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) { if ( ++i == 50 ) {
s.setReadOnly( dp, false ); s.setReadOnly( dp, false );
}
dp.setDescription( "done!" );
} }
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();
@ -228,14 +231,15 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
int i = 0; int i = 0;
Query query = s.createQuery( "from DataPoint dp order by dp.x asc" ); Query query = s.createQuery( "from DataPoint dp order by dp.x asc" );
s.setDefaultReadOnly( true ); s.setDefaultReadOnly( true );
ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY ); try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
s.setDefaultReadOnly( false ); s.setDefaultReadOnly( false );
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get(); DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) { if ( ++i == 50 ) {
s.setReadOnly( dp, false ); s.setReadOnly( dp, false );
}
dp.setDescription( "done!" );
} }
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();
@ -287,32 +291,33 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
assertTrue( query.isReadOnly() ); assertTrue( query.isReadOnly() );
s.setDefaultReadOnly( false ); s.setDefaultReadOnly( false );
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY );
assertFalse( s.isDefaultReadOnly() );
assertTrue( query.isReadOnly() );
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertFalse( s.isReadOnly( dpLast ) );
query.setReadOnly( false );
assertFalse( query.isReadOnly() );
int nExpectedChanges = 0; int nExpectedChanges = 0;
assertFalse( s.isDefaultReadOnly() ); try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) {
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
dp = (DataPoint) sr.get(); assertTrue( query.isReadOnly() );
if ( dp.getId() == dpLast.getId() ) { DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
//dpLast existed in the session before executing the read-only query assertFalse( s.isReadOnly( dpLast ) );
assertFalse( s.isReadOnly( dp ) ); query.setReadOnly( false );
assertFalse( query.isReadOnly() );
assertFalse( s.isDefaultReadOnly() );
while ( sr.next() ) {
assertFalse( s.isDefaultReadOnly() );
dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
//dpLast existed in the session before executing the read-only query
assertFalse( s.isReadOnly( dp ) );
}
else {
assertTrue( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, false );
nExpectedChanges = ( dp == dpLast ? 1 : 2 );
}
dp.setDescription( "done!" );
} }
else { assertFalse( s.isDefaultReadOnly() );
assertTrue( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, false );
nExpectedChanges = ( dp == dpLast ? 1 : 2 );
}
dp.setDescription( "done!" );
} }
assertFalse( s.isDefaultReadOnly() );
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -363,31 +368,32 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
assertFalse( query.isReadOnly() ); assertFalse( query.isReadOnly() );
s.setDefaultReadOnly( true ); s.setDefaultReadOnly( true );
assertTrue( s.isDefaultReadOnly() ); assertTrue( s.isDefaultReadOnly() );
ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY );
assertFalse( query.isReadOnly() );
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertTrue( s.isReadOnly( dpLast ) );
query.setReadOnly( true );
assertTrue( query.isReadOnly() );
int nExpectedChanges = 0; int nExpectedChanges = 0;
assertTrue( s.isDefaultReadOnly() ); try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { assertFalse( query.isReadOnly() );
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertTrue( s.isReadOnly( dpLast ) );
query.setReadOnly( true );
assertTrue( query.isReadOnly() );
assertTrue( s.isDefaultReadOnly() ); assertTrue( s.isDefaultReadOnly() );
dp = (DataPoint) sr.get(); while ( sr.next() ) {
if ( dp.getId() == dpLast.getId() ) { assertTrue( s.isDefaultReadOnly() );
//dpLast existed in the session before executing the read-only query dp = (DataPoint) sr.get();
assertTrue( s.isReadOnly( dp ) ); if ( dp.getId() == dpLast.getId() ) {
//dpLast existed in the session before executing the read-only query
assertTrue( s.isReadOnly( dp ) );
}
else {
assertFalse( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, true );
nExpectedChanges = ( dp == dpLast ? 99 : 98 );
}
dp.setDescription( "done!" );
} }
else { assertTrue( s.isDefaultReadOnly() );
assertFalse( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, true );
nExpectedChanges = ( dp == dpLast ? 99 : 98 );
}
dp.setDescription( "done!" );
} }
assertTrue( s.isDefaultReadOnly() );
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -690,24 +696,25 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
assertFalse( s.isReadOnly( dpLast ) ); assertFalse( s.isReadOnly( dpLast ) );
s.setDefaultReadOnly( true ); s.setDefaultReadOnly( true );
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
s.setDefaultReadOnly( false );
int nExpectedChanges = 0; int nExpectedChanges = 0;
while ( sr.next() ) { try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
dp = (DataPoint) sr.get(); .scroll( ScrollMode.FORWARD_ONLY )) {
if ( dp.getId() == dpLast.getId() ) { s.setDefaultReadOnly( false );
//dpLast existed in the session before executing the read-only query while ( sr.next() ) {
assertFalse( s.isReadOnly( dp ) ); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
//dpLast existed in the session before executing the read-only query
assertFalse( s.isReadOnly( dp ) );
}
else {
assertTrue( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, false );
nExpectedChanges = ( dp == dpLast ? 1 : 2 );
}
dp.setDescription( "done!" );
} }
else {
assertTrue( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, false );
nExpectedChanges = ( dp == dpLast ? 1 : 2 );
}
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();
@ -741,24 +748,25 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertTrue( s.isReadOnly( dpLast ) ); assertTrue( s.isReadOnly( dpLast ) );
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0; int nExpectedChanges = 0;
while ( sr.next() ) { try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
dp = (DataPoint) sr.get(); .setReadOnly( false )
if ( dp.getId() == dpLast.getId() ) { .scroll( ScrollMode.FORWARD_ONLY )) {
//dpLast existed in the session before executing the read-only query while ( sr.next() ) {
assertTrue( s.isReadOnly( dp ) ); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
//dpLast existed in the session before executing the read-only query
assertTrue( s.isReadOnly( dp ) );
}
else {
assertFalse( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, true );
nExpectedChanges = ( dp == dpLast ? 99 : 98 );
}
dp.setDescription( "done!" );
} }
else {
assertFalse( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, true );
nExpectedChanges = ( dp == dpLast ? 99 : 98 );
}
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();

View File

@ -115,15 +115,16 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
try { try {
session.getTransaction().begin(); session.getTransaction().begin();
int i = 0; int i = 0;
ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true ) .setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get(); DataPoint dp = (DataPoint) sr.get();
if ( ++i == 50 ) { if ( ++i == 50 ) {
session.setReadOnly( dp, false ); session.setReadOnly( dp, false );
}
dp.setDescription( "done!" );
} }
dp.setDescription( "done!" );
} }
session.getTransaction().commit(); session.getTransaction().commit();
@ -172,18 +173,19 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
assertInsertCount( 0, scope ); assertInsertCount( 0, scope );
assertUpdateCount( 0, scope ); assertUpdateCount( 0, scope );
ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" ) try (ScrollableResults sr = session.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true ) .setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
assertInsertCount( 100, scope ); assertInsertCount( 100, scope );
assertUpdateCount( 0, scope ); assertUpdateCount( 0, scope );
clearCounts( scope ); clearCounts( scope );
while ( sr.next() ) { while ( sr.next() ) {
DataPoint dp = (DataPoint) sr.get(); DataPoint dp = (DataPoint) sr.get();
assertFalse( session.isReadOnly( dp ) ); assertFalse( session.isReadOnly( dp ) );
session.delete( dp ); session.delete( dp );
}
} }
} }
); );
@ -462,24 +464,25 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() ); DataPoint dpLast = (DataPoint) s.get( DataPoint.class, dp.getId() );
assertFalse( s.isReadOnly( dpLast ) ); assertFalse( s.isReadOnly( dpLast ) );
int i = 0; int i = 0;
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0; int nExpectedChanges = 0;
while ( sr.next() ) { try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
dp = (DataPoint) sr.get(); .setReadOnly( true )
if ( dp.getId() == dpLast.getId() ) { .scroll( ScrollMode.FORWARD_ONLY )) {
//dpLast existed in the session before executing the read-only query while ( sr.next() ) {
assertFalse( s.isReadOnly( dp ) ); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
//dpLast existed in the session before executing the read-only query
assertFalse( s.isReadOnly( dp ) );
}
else {
assertTrue( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, false );
nExpectedChanges = ( dp == dpLast ? 1 : 2 );
}
dp.setDescription( "done!" );
} }
else {
assertTrue( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, false );
nExpectedChanges = ( dp == dpLast ? 1 : 2 );
}
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();
@ -529,24 +532,25 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
assertUpdateCount( 0, scope ); assertUpdateCount( 0, scope );
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY );
int nExpectedChanges = 0; int nExpectedChanges = 0;
while ( sr.next() ) { try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
dp = (DataPoint) sr.get(); .setReadOnly( false )
if ( dp.getId() == dpLast.getId() ) { .scroll( ScrollMode.FORWARD_ONLY )) {
//dpLast existed in the session before executing the read-only query while ( sr.next() ) {
assertTrue( s.isReadOnly( dp ) ); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) {
//dpLast existed in the session before executing the read-only query
assertTrue( s.isReadOnly( dp ) );
}
else {
assertFalse( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, true );
nExpectedChanges = ( dp == dpLast ? 99 : 98 );
}
dp.setDescription( "done!" );
} }
else {
assertFalse( s.isReadOnly( dp ) );
}
if ( ++i == 50 ) {
s.setReadOnly( dp, true );
nExpectedChanges = ( dp == dpLast ? 99 : 98 );
}
dp.setDescription( "done!" );
} }
t.commit(); t.commit();
s.clear(); s.clear();

View File

@ -60,21 +60,21 @@ public class ResultTransformerTest {
} }
); );
ScrollableResults sr = q.scroll(); try (ScrollableResults sr = q.scroll()) {
// HANA supports only ResultSet.TYPE_FORWARD_ONLY and // HANA supports only ResultSet.TYPE_FORWARD_ONLY and
// does not support java.sql.ResultSet.first() // does not support java.sql.ResultSet.first()
if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof AbstractHANADialect ) { if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof AbstractHANADialect ) {
sr.next(); sr.next();
} }
else { else {
sr.first(); sr.first();
} }
Object obj = sr.get();
assertTrue(obj instanceof PartnerA);
PartnerA obj2 = (PartnerA) obj;
assertEquals("Partner A", obj2.getName());
Object obj = sr.get();
assertTrue( obj instanceof PartnerA );
PartnerA obj2 = (PartnerA) obj;
assertEquals( "Partner A", obj2.getName() );
}
} ); } );
} }
} }

View File

@ -75,11 +75,11 @@ public class StatelessSessionTest {
assertEquals( "Blahs", doc2.getName() ); assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() ); assertEquals( doc.getText(), doc2.getText() );
ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" ) try (ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" )
.scroll( ScrollMode.FORWARD_ONLY ); .scroll( ScrollMode.FORWARD_ONLY )) {
sr.next(); sr.next();
doc2 = (Document) sr.get(); doc2 = (Document) sr.get();
sr.close(); }
assertEquals( "Blahs", doc2.getName() ); assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() ); assertEquals( doc.getText(), doc2.getText() );
@ -100,10 +100,10 @@ public class StatelessSessionTest {
criteria = criteriaBuilder.createQuery( Document.class ); criteria = criteriaBuilder.createQuery( Document.class );
criteria.from( Document.class ); criteria.from( Document.class );
sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY ); try (ScrollableResults sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) {
sr.next(); sr.next();
doc2 = (Document) sr.get(); doc2 = (Document) sr.get();
sr.close(); }
assertEquals( "Blahs", doc2.getName() ); assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() ); assertEquals( doc.getText(), doc2.getText() );

View File

@ -105,13 +105,14 @@ public class StatelessSessionFetchingTest {
scope.inStatelessTransaction( scope.inStatelessTransaction(
session -> { session -> {
final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" ); final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" );
final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); try (ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY )) {
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
Task taskRef = (Task) scrollableResults.get(); Task taskRef = (Task) scrollableResults.get();
assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef ) );
assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) );
assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) );
assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
}
} }
} }
); );
@ -148,13 +149,14 @@ public class StatelessSessionFetchingTest {
scope.inStatelessTransaction( scope.inStatelessTransaction(
session -> { session -> {
final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" ); final Query query = session.createQuery( "from Task t join fetch t.resource join fetch t.user" );
final ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); try (ScrollableResults scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY )) {
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
Task taskRef = (Task) scrollableResults.get(); Task taskRef = (Task) scrollableResults.get();
assertTrue( Hibernate.isInitialized( taskRef ) ); assertTrue( Hibernate.isInitialized( taskRef ) );
assertTrue( Hibernate.isInitialized( taskRef.getUser() ) ); assertTrue( Hibernate.isInitialized( taskRef.getUser() ) );
assertTrue( Hibernate.isInitialized( taskRef.getResource() ) ); assertTrue( Hibernate.isInitialized( taskRef.getResource() ) );
assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
}
} }
} }
); );
@ -189,21 +191,22 @@ public class StatelessSessionFetchingTest {
scope.inStatelessTransaction( scope.inStatelessTransaction(
session -> { session -> {
final Query query = session.createQuery( "select p from Producer p join fetch p.products" ); final Query query = session.createQuery( "select p from Producer p join fetch p.products" );
final ScrollableResults scrollableResults = getScrollableResults( try (ScrollableResults scrollableResults = getScrollableResults(
query, query,
scope.getSessionFactory() scope.getSessionFactory()
.getJdbcServices() .getJdbcServices()
.getDialect() .getDialect()
); )) {
while ( scrollableResults.next() ) { while ( scrollableResults.next() ) {
Producer producer = (Producer) scrollableResults.get(); Producer producer = (Producer) scrollableResults.get();
assertTrue( Hibernate.isInitialized( producer ) ); assertTrue( Hibernate.isInitialized( producer ) );
assertTrue( Hibernate.isInitialized( producer.getProducts() ) ); assertTrue( Hibernate.isInitialized( producer.getProducts() ) );
for ( Product product : producer.getProducts() ) { for ( Product product : producer.getProducts() ) {
assertTrue( Hibernate.isInitialized( product ) ); assertTrue( Hibernate.isInitialized( product ) );
assertFalse( Hibernate.isInitialized( product.getVendor() ) ); assertFalse( Hibernate.isInitialized( product.getVendor() ) );
}
} }
} }
} }

View File

@ -60,26 +60,32 @@ public class JpaStreamTest {
scope.inTransaction( session -> { scope.inTransaction( session -> {
// Test stream query without type. // Test stream query without type.
Object result = session.createQuery( "From MyEntity" ).getResultStream().findFirst().orElse( null ); Object result;
try (Stream stream = session.createQuery( "From MyEntity" ).getResultStream()) {
result = stream.findFirst().orElse( null );
}
assertTyping( MyEntity.class, result ); assertTyping( MyEntity.class, result );
// Test stream query with type. // Test stream query with type.
result = session.createQuery( "From MyEntity", MyEntity.class ) try (Stream stream = session.createQuery( "From MyEntity", MyEntity.class ).getResultStream()) {
.getResultStream() result = stream.findFirst().orElse( null );
.findFirst() }
.orElse( null );
assertTyping( MyEntity.class, result ); assertTyping( MyEntity.class, result );
// Test stream query using forEach // Test stream query using forEach
session.createQuery( "From MyEntity", MyEntity.class ).getResultStream().forEach( i -> { try (Stream<MyEntity> stream = session.createQuery( "From MyEntity", MyEntity.class ).getResultStream()) {
assertTyping( MyEntity.class, i ); stream.forEach( i -> {
} ); assertTyping( MyEntity.class, i );
} );
}
Stream<Object[]> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me" ).getResultStream(); try (Stream<Object[]> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me" )
data.forEach( i -> { .getResultStream()) {
assertTyping( Integer.class, i[0] ); data.forEach( i -> {
assertTyping( String.class, i[1] ); assertTyping( Integer.class, i[0] );
} ); assertTyping( String.class, i[1] );
} );
}
} ); } );
} }