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 95efb07da7
commit b2fda37773
26 changed files with 712 additions and 680 deletions

View File

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

View File

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

View File

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

View File

@ -80,20 +80,21 @@ public class LazyManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( children.size() ).isEqualTo( 1 );
List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( children.size() ).isEqualTo( 1 );
Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull();
Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isFalse();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( Hibernate.isInitialized( parent ) ).isFalse();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
}
);
}
@ -106,29 +107,30 @@ public class LazyManyToOneStreamTest {
session -> {
QueryImplementor<Child> query = session
.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() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
List<Child> children = resultStream.collect( Collectors.toList() );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( children.size() ).isEqualTo( 2 );
assertThat( children.size() ).isEqualTo( 2 );
Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isFalse();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
Parent parent = children.get( 0 ).getParent();
assertThat( parent ).isNotNull();
assertThat( Hibernate.isInitialized( parent ) ).isFalse();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 1 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( parent.getSomeField() ).isEqualTo( FIELD_VALUE );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
Parent parent1 = children.get( 1 ).getParent();
assertThat( parent1 ).isNotNull();
// parent2 has been batch loaded
assertThat( Hibernate.isInitialized( parent1 ) ).isTrue();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
Parent parent1 = children.get( 1 ).getParent();
assertThat( parent1 ).isNotNull();
// parent2 has been batch loaded
assertThat( Hibernate.isInitialized( parent1 ) ).isTrue();
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
assertThat( parent1.getSomeField() ).isEqualTo( FIELD_VALUE_2 );
assertThat( sqlStatementInterceptor.getSqlQueries().size() ).isEqualTo( 2 );
}
}
);
}
@ -140,15 +142,16 @@ public class LazyManyToOneStreamTest {
QueryImplementor<Child> query = session
.createQuery( "select c from Child as c where c.parent.someField=:someField", Child.class )
.setParameter( "someField", FIELD_VALUE );
Stream<Child> resultStream = query.getResultStream();
try (Stream<Child> resultStream = query.getResultStream()) {
Optional<Child> child = resultStream.findFirst();
assertThat( child.isEmpty() ).isFalse();
Optional<Child> child = resultStream.findFirst();
assertThat( child.isEmpty() ).isFalse();
Parent parent = child.get().getParent();
assertThat( parent ).isNotNull();
Parent parent = child.get().getParent();
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 Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
while ( scrollableResults.next() ) {
final DEntity dEntity = (DEntity) scrollableResults.get();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyString" ) );
assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyStringBlobGroup" ) );
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) {
final DEntity dEntity = (DEntity) scrollableResults.get();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyString" ) );
assertFalse( Hibernate.isPropertyInitialized( dEntity, "lazyStringBlobGroup" ) );
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 Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
while ( scrollableResults.next() ) {
final EEntity eEntity = (EEntity) scrollableResults.get();
final DEntity dEntity = eEntity.getD();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) {
final EEntity eEntity = (EEntity) scrollableResults.get();
final DEntity dEntity = eEntity.getD();
assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) );
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
}
}
}
);
@ -251,14 +253,15 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
"join fetch d.g";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
int i = 0;
while ( scrollableResults.next() ) {
i++;
final DEntity dEntity = (DEntity) scrollableResults.get();
assertThat( dEntity.getBs().size(), is( 2 ) );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
int i = 0;
while ( scrollableResults.next() ) {
i++;
final DEntity dEntity = (DEntity) scrollableResults.get();
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 Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
while ( scrollableResults.next() ) {
final Object o = scrollableResults.get();
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
while ( scrollableResults.next() ) {
final Object o = scrollableResults.get();
}
}
}
);
@ -296,16 +300,17 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
"join fetch d.g";
final Query query = session.createQuery( qry );
final ScrollableResults scrollableResults = getScrollableResults( query );
int i = 0;
while ( scrollableResults.next() ) {
i++;
final Object[] result = (Object[]) scrollableResults.get();
final DEntity dEntity = (DEntity) result[0];
assertThat( dEntity.getBs().size(), is( 2 ) );
assertThat( result[1], is( "bla" ) );
try (ScrollableResults scrollableResults = getScrollableResults( query )) {
int i = 0;
while ( scrollableResults.next() ) {
i++;
final Object[] result = (Object[]) scrollableResults.get();
final DEntity dEntity = (DEntity) result[0];
assertThat( dEntity.getBs().size(), is( 2 ) );
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
// to resources, which should make aggressive-release not release
// the connection (and thus cause serialization to fail)
ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
sr.next();
try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) {
sr.next();
try {
SerializationHelper.serialize( s );
fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
try {
SerializationHelper.serialize( s );
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 );
s.delete( silly );
@ -146,22 +146,19 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
s.save( silly );
s.flush();
ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
assertTrue( sr.next() );
Silly silly2 = ( Silly ) sr.get();
assertEquals( silly, silly2 );
sr.close();
try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll()) {
assertTrue( sr.next() );
Silly silly2 = (Silly) sr.get();
assertEquals( silly, silly2 );
}
sr = s.createQuery( "from Silly" ).scroll();
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll();
assertTrue( sr.next() );
assertEquals( silly, sr.get() );
assertTrue( sr2.next() );
assertEquals( silly, sr2.get() );
sr.close();
sr2.close();
try (ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll()) {
assertTrue( sr.next() );
assertEquals( silly, sr.get() );
assertTrue( sr2.next() );
assertEquals( silly, sr2.get() );
}
s.delete( silly );
s.flush();

View File

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

View File

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

View File

@ -958,20 +958,6 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
.collect(Collectors.toList());
}
//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

@ -53,11 +53,12 @@ public class ScrollableCollectionFetchingTest {
session -> {
session.createMutationQuery("insert Mammal (description, bodyWeight, pregnant) values ('Human', 80.0, false)").executeUpdate();
assertEquals( 1L, session.createSelectionQuery("select count(*) from Mammal").getSingleResult() );
ScrollableResults results = session.createQuery("select a, a.bodyWeight from Animal a left join fetch a.offspring").scroll();
assertTrue( results.next() );
Object[] result = (Object[]) results.get();
assertTrue(Hibernate.isInitialized(((Animal) result[0]).getOffspring()));
session.createMutationQuery("delete Mammal").executeUpdate();
try (ScrollableResults results = session.createQuery("select a, a.bodyWeight from Animal a left join fetch a.offspring").scroll()) {
assertTrue( results.next() );
Object[] result = (Object[]) results.get();
assertTrue( Hibernate.isInitialized( ( (Animal) result[0] ).getOffspring() ) );
session.createMutationQuery( "delete Mammal" ).executeUpdate();
}
}
);
}
@ -66,8 +67,9 @@ public class ScrollableCollectionFetchingTest {
public void testTupleReturnWithFetchFailure(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
try {
session.createQuery( "select a.description, a.bodyWeight from Animal a inner join fetch a.offspring" ).scroll();
try (ScrollableResults<?> sr = session.createQuery(
"select a.description, a.bodyWeight from Animal a inner join fetch a.offspring" )
.scroll()) {
fail( "scroll allowed with fetch and projection result" );
}
catch (IllegalArgumentException e) {
@ -82,8 +84,8 @@ public class ScrollableCollectionFetchingTest {
public void testUknownPathFailure(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
try {
session.createQuery( "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll();
try (ScrollableResults<?> sr = session.createQuery(
"select a, a.weight from Animal a inner join fetch a.offspring" ).scroll()) {
fail( "scroll allowed with unknown path" );
}
catch (IllegalArgumentException e) {
@ -106,55 +108,56 @@ public class ScrollableCollectionFetchingTest {
assertEquals( 0, size );
// 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.isLast() );
assertFalse( results.scroll( -i ) );
assertFalse( results.next() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( i ) );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( -i ) );
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.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() );
}
}
}
);
@ -190,84 +193,85 @@ public class ScrollableCollectionFetchingTest {
.setParameter( "desc", "root%" )
.uniqueResult() );
ScrollableResults results = session
try (ScrollableResults results = session
.createQuery(
"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.isLast() );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
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.next() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.scroll( i ) );
assertFalse( results.next() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertTrue( results.setRowNumber( 1 ) );
assertTrue( results.previous() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.scroll( -i ) );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
if ( i != 1 ) {
assertFalse( results.setRowNumber( i ) );
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.isLast() );
assertFalse( results.scroll( i ) );
assertFalse( results.isFirst() );
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.isLast() );
if ( i != 1 ) {
assertFalse( results.setRowNumber( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( -i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
}
}
}
}
@ -292,20 +296,20 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction(
s -> {
ScrollableResults results = s
try (ScrollableResults results = s
.createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" )
.scroll( ScrollMode.FORWARD_ONLY );
.scroll( ScrollMode.FORWARD_ONLY )) {
int counter = 0;
while ( results.next() ) {
counter++;
Animal animal = (Animal) results.get();
checkResult( animal );
int counter = 0;
while ( results.next() ) {
counter++;
Animal animal = (Animal) results.get();
checkResult( animal );
}
assertEquals( 2, counter, "unexpected result count" );
}
assertEquals( 2, counter, "unexpected result count" );
}
);
}
@ -319,21 +323,21 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction(
s -> {
ScrollableResults results = s
try (ScrollableResults results = s
.createQuery(
"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;
while ( results.previous() ) {
counter++;
Animal animal = (Animal) results.get();
checkResult( animal );
int counter = 0;
while ( results.previous() ) {
counter++;
Animal animal = (Animal) results.get();
checkResult( animal );
}
assertEquals( 2, counter, "unexpected result count" );
}
assertEquals( 2, counter, "unexpected result count" );
}
);
}
@ -347,31 +351,32 @@ public class ScrollableCollectionFetchingTest {
scope.inTransaction(
session -> {
ScrollableResults results = session
try (ScrollableResults results = session
.createQuery(
"from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setParameter( "desc", "root%" )
.scroll();
.scroll()) {
results.first();
Animal animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "first() did not return expected row" );
results.first();
Animal animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "first() did not return expected row" );
results.scroll( 1 );
animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "scroll(1) did not return expected row" );
results.scroll( 1 );
animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "scroll(1) did not return expected row" );
results.scroll( -1 );
animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "scroll(-1) did not return expected row" );
results.scroll( -1 );
animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "scroll(-1) did not return expected row" );
results.setRowNumber( 1 );
animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "setRowNumber(1) did not return expected row" );
results.setRowNumber( 1 );
animal = (Animal) results.get();
assertEquals( data.root1Id, animal.getId(), "setRowNumber(1) did not return expected row" );
results.setRowNumber( 2 );
animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" );
results.setRowNumber( 2 );
animal = (Animal) results.get();
assertEquals( data.root2Id, animal.getId(), "setRowNumber(2) did not return expected row" );
}
}
);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -218,9 +218,9 @@ public class CriteriaCompilingTest extends BaseEntityManagerFunctionalTestCase {
TypedQuery<Order> jpaQuery = session.createQuery( query );
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(
"select o from Order o where totalPrice in :totalPrices",

View File

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

View File

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

View File

@ -49,8 +49,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isInstanceOf( Product.class );
}
{
final ScrollableResults<?> results = query.scroll();
try (ScrollableResults<?> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object result = results.get();
assertThat( result ).isInstanceOf( Product.class );
@ -71,8 +70,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isNotNull();
}
{
final ScrollableResults<Product> results = query.scroll();
try (ScrollableResults<Product> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Product result = results.get();
assertThat( result ).isNotNull();
@ -96,8 +94,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( (Object[]) result ).hasExactlyElementsOfTypes(Product.class, Vendor.class, Product.class, Vendor.class);
}
{
final ScrollableResults<?> results = query.scroll();
try (ScrollableResults<?> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object result = results.get();
assertThat( result ).isNotNull();
@ -153,8 +150,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).hasExactlyElementsOfTypes(Product.class, Vendor.class, Product.class, Vendor.class);
}
{
final ScrollableResults<Object[]> results = query.scroll();
try (ScrollableResults<Object[]> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object[] result = results.get();
assertThat( results.next() ).isFalse();
@ -182,8 +178,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).hasExactlyElementsOfTypes(Product.class, Vendor.class);
}
{
final ScrollableResults<Object[]> results = query.scroll();
try (final ScrollableResults<Object[]> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object[] result = results.get();
assertThat( results.next() ).isFalse();
@ -212,8 +207,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result[ 0 ] ).isInstanceOf( Product.class );
}
{
final ScrollableResults<Object[]> results = query.scroll();
try (ScrollableResults<Object[]> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Object[] result = results.get();
assertThat( results.next() ).isFalse();
@ -237,8 +231,7 @@ public class ImplicitSelectWithJoinTests {
assertThat( result ).isNotNull();
}
{
final ScrollableResults<Product> results = query.scroll();
try (ScrollableResults<Product> results = query.scroll()) {
assertThat( results.next() ).isTrue();
final Product result = results.get();
assertThat( result ).isNotNull();

View File

@ -68,54 +68,54 @@ public class ScrollableResultsTests {
scope.inTransaction(
session -> {
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
assertThat( results.previous(), is( false ) );
// try to initially read in reverse - should be false
assertThat( results.previous(), is( false ) );
// position at the first row
assertThat( results.next(), is( true ) );
String data = results.get();
assertThat( data, is( "other" ) );
// position at the first row
assertThat( results.next(), is( true ) );
String data = results.get();
assertThat( data, is( "other" ) );
// position at the second (last) row
assertThat( results.next(), is( true ) );
data = results.get();
assertThat( data, is( "value" ) );
// position at the second (last) row
assertThat( results.next(), is( true ) );
data = results.get();
assertThat( data, is( "value" ) );
// position after the second (last) row
assertThat( results.next(), is( false ) );
// position after the second (last) row
assertThat( results.next(), is( false ) );
// position back to the second row
assertThat( results.previous(), is( true ) );
data = results.get();
assertThat( data, is( "value" ) );
// position back to the second row
assertThat( results.previous(), is( true ) );
data = results.get();
assertThat( data, is( "value" ) );
// position back to the first row
assertThat( results.previous(), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
// position back to the first row
assertThat( results.previous(), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
// position before the first row
assertThat( results.previous(), is( false ) );
assertThat( results.previous(), is( false ) );
// position before the first row
assertThat( results.previous(), is( false ) );
assertThat( results.previous(), is( false ) );
assertThat( results.last(), is( true ) );
data = results.get();
assertThat( data, is( "value" ) );
assertThat( results.last(), is( true ) );
data = results.get();
assertThat( data, is( "value" ) );
assertThat( results.first(), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
assertThat( results.first(), is( true ) );
data = results.get();
assertThat( data, is( "other" ) );
assertThat( results.scroll( 1 ), is( true ) );
data = results.get();
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( "value" ) );
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) {
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 ) );
validator.accept( results.get() );
}

View File

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

View File

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

View File

@ -60,21 +60,21 @@ public class ResultTransformerTest {
}
);
ScrollableResults sr = q.scroll();
// HANA supports only ResultSet.TYPE_FORWARD_ONLY and
// does not support java.sql.ResultSet.first()
if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof AbstractHANADialect ) {
sr.next();
}
else {
sr.first();
}
Object obj = sr.get();
assertTrue(obj instanceof PartnerA);
PartnerA obj2 = (PartnerA) obj;
assertEquals("Partner A", obj2.getName());
try (ScrollableResults sr = q.scroll()) {
// HANA supports only ResultSet.TYPE_FORWARD_ONLY and
// does not support java.sql.ResultSet.first()
if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof AbstractHANADialect ) {
sr.next();
}
else {
sr.first();
}
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( doc.getText(), doc2.getText() );
ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" )
.scroll( ScrollMode.FORWARD_ONLY );
sr.next();
doc2 = (Document) sr.get();
sr.close();
try (ScrollableResults sr = statelessSession.createQuery( "from Document where text is not null" )
.scroll( ScrollMode.FORWARD_ONLY )) {
sr.next();
doc2 = (Document) sr.get();
}
assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() );
@ -100,10 +100,10 @@ public class StatelessSessionTest {
criteria = criteriaBuilder.createQuery( Document.class );
criteria.from( Document.class );
sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY );
sr.next();
doc2 = (Document) sr.get();
sr.close();
try (ScrollableResults sr = statelessSession.createQuery( criteria ).scroll( ScrollMode.FORWARD_ONLY )) {
sr.next();
doc2 = (Document) sr.get();
}
assertEquals( "Blahs", doc2.getName() );
assertEquals( doc.getText(), doc2.getText() );

View File

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

View File

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