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,16 +92,17 @@ 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();
s.close(); s.close();
@ -109,16 +110,17 @@ 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();
s.close(); s.close();
} }

View File

@ -79,7 +79,7 @@ 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 );
@ -95,6 +95,7 @@ public class EagerManyToOneStreamTest {
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,7 +107,7 @@ 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
@ -130,6 +131,7 @@ public class EagerManyToOneStreamTest {
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,10 +142,12 @@ 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()) {
resultStream.forEach(
child -> assertThat( child.getParent() ).isNotNull() child -> assertThat( child.getParent() ).isNotNull()
); );
} }
}
); );
} }
@ -154,11 +158,12 @@ 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,7 +80,7 @@ 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 );
@ -95,6 +95,7 @@ public class LazyManyToOneStreamTest {
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,7 +107,7 @@ 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 );
@ -130,6 +131,7 @@ public class LazyManyToOneStreamTest {
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,7 +142,7 @@ 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();
@ -150,6 +152,7 @@ public class LazyManyToOneStreamTest {
assertThat( Hibernate.isInitialized( parent ) ).isFalse(); assertThat( Hibernate.isInitialized( parent ) ).isFalse();
} }
}
); );
} }

View File

@ -138,7 +138,7 @@ 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" ) );
@ -147,6 +147,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
} }
} }
}
); );
inSession( inSession(
@ -231,7 +232,7 @@ 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();
@ -239,6 +240,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) ); assertTrue( Hibernate.isPropertyInitialized( dEntity, "nonLazyString" ) );
} }
} }
}
); );
inStatelessSession( inStatelessSession(
@ -251,7 +253,7 @@ 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++;
@ -260,6 +262,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
} }
assertThat( i, is( 1 ) ); assertThat( i, is( 1 ) );
} }
}
); );
inStatelessSession( inStatelessSession(
@ -267,11 +270,12 @@ 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,7 +300,7 @@ 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++;
@ -307,6 +311,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
} }
assertThat( i, is( 1 ) ); assertThat( i, is( 1 ) );
} }
}
); );
} }

View File

@ -111,14 +111,14 @@ 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 ) { catch (IllegalStateException e) {
// expected behavior // expected behavior
} }
@ -128,7 +128,7 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
// Closing the ScrollableResults does currently force batching to // Closing the ScrollableResults does currently force batching to
// aggressively release the connection // 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();
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll();
try (ScrollableResults sr = s.createQuery( "from 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,14 +135,15 @@ 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 {
@ -149,6 +151,7 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
done(); done();
} }
} }
}
/** /**
* Tests to validate that a session holding JDBC resources will not * Tests to validate that a session holding JDBC resources will not
@ -247,7 +250,7 @@ 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 );
@ -260,6 +263,7 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
release( sessionUnderTest ); release( sessionUnderTest );
done(); done();
} }
}
/** /**
* Test that the basic session usage template works in all environment * Test that the basic session usage template works in all environment

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,7 +74,7 @@ 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.isFirst() );
assertFalse( results.isLast() ); assertFalse( results.isLast() );
@ -125,6 +125,7 @@ public class ScrollableCollectionFetchingTest {
assertFalse( results.isLast() ); assertFalse( results.isLast() );
} }
} }
}
); );
} }
@ -158,10 +159,10 @@ 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() );
@ -239,6 +240,7 @@ public class ScrollableCollectionFetchingTest {
} }
} }
} }
}
); );
scope.inTransaction( scope.inTransaction(
@ -260,11 +262,11 @@ 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() ) {
@ -273,7 +275,7 @@ public class ScrollableCollectionFetchingTest {
checkResult( animal ); checkResult( animal );
} }
assertEquals( 2, counter, "unexpected result count" ); assertEquals( 2, counter, "unexpected result count" );
}
} }
); );
} }
@ -287,10 +289,10 @@ 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();
@ -301,7 +303,7 @@ public class ScrollableCollectionFetchingTest {
checkResult( animal ); checkResult( animal );
} }
assertEquals( 2, counter, "unexpected result count" ); assertEquals( 2, counter, "unexpected result count" );
}
} }
); );
} }
@ -315,11 +317,11 @@ 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();
@ -341,6 +343,7 @@ public class ScrollableCollectionFetchingTest {
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,10 +55,11 @@ 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,7 +51,7 @@ 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() );
@ -64,6 +64,7 @@ public class ScrollTest {
s.delete( i1 ); s.delete( i1 );
s.delete( i2 ); s.delete( i2 );
} }
}
); );
assertThat( scope.getSessionFactory().getStatistics().getEntityFetchCount(), is( 0L ) ); assertThat( scope.getSessionFactory().getStatistics().getEntityFetchCount(), is( 0L ) );

View File

@ -113,25 +113,27 @@ 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,14 +160,15 @@ 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,14 +194,15 @@ 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,25 +106,27 @@ 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,25 +111,27 @@ 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,14 +155,15 @@ 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,7 +68,7 @@ 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 ) );
@ -115,7 +115,7 @@ public class ScrollableResultsTests {
assertThat( results.scroll( -1 ), is( true ) ); assertThat( results.scroll( -1 ), is( true ) );
data = results.get(); data = results.get();
assertThat( data, is( "other" ) ); 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,8 +109,8 @@ 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();
@ -119,6 +119,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -148,9 +149,9 @@ 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 ) {
@ -158,6 +159,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -187,9 +189,9 @@ 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 ) {
@ -197,6 +199,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -228,7 +231,7 @@ 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();
@ -237,6 +240,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -287,14 +291,14 @@ 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 ); int nExpectedChanges = 0;
try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
assertTrue( query.isReadOnly() ); assertTrue( query.isReadOnly() );
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 ) );
query.setReadOnly( false ); query.setReadOnly( false );
assertFalse( query.isReadOnly() ); assertFalse( query.isReadOnly() );
int nExpectedChanges = 0;
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
while ( sr.next() ) { while ( sr.next() ) {
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
@ -313,6 +317,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
assertFalse( s.isDefaultReadOnly() ); assertFalse( s.isDefaultReadOnly() );
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -363,13 +368,13 @@ 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 ); int nExpectedChanges = 0;
try (ScrollableResults sr = query.scroll( ScrollMode.FORWARD_ONLY )) {
assertFalse( query.isReadOnly() ); assertFalse( query.isReadOnly() );
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 ) );
query.setReadOnly( true ); query.setReadOnly( true );
assertTrue( query.isReadOnly() ); assertTrue( query.isReadOnly() );
int nExpectedChanges = 0;
assertTrue( s.isDefaultReadOnly() ); assertTrue( s.isDefaultReadOnly() );
while ( sr.next() ) { while ( sr.next() ) {
assertTrue( s.isDefaultReadOnly() ); assertTrue( s.isDefaultReadOnly() );
@ -388,6 +393,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
assertTrue( s.isDefaultReadOnly() ); assertTrue( s.isDefaultReadOnly() );
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -690,10 +696,10 @@ 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;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY )) {
s.setDefaultReadOnly( false );
while ( sr.next() ) { while ( sr.next() ) {
dp = (DataPoint) sr.get(); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) { if ( dp.getId() == dpLast.getId() ) {
@ -709,6 +715,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();
@ -741,10 +748,10 @@ 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;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
dp = (DataPoint) sr.get(); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) { if ( dp.getId() == dpLast.getId() ) {
@ -760,6 +767,7 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
t = s.beginTransaction(); t = s.beginTransaction();

View File

@ -115,9 +115,9 @@ 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 ) {
@ -125,6 +125,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
session.getTransaction().commit(); session.getTransaction().commit();
assertUpdateCount( 1, scope ); assertUpdateCount( 1, scope );
@ -172,9 +173,9 @@ 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 );
@ -186,6 +187,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
session.delete( dp ); session.delete( dp );
} }
} }
}
); );
assertUpdateCount( 0, scope ); assertUpdateCount( 0, scope );
@ -462,10 +464,10 @@ 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;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( true )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
dp = (DataPoint) sr.get(); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) { if ( dp.getId() == dpLast.getId() ) {
@ -481,6 +483,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();
@ -529,10 +532,10 @@ 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;
try (ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.setReadOnly( false )
.scroll( ScrollMode.FORWARD_ONLY )) {
while ( sr.next() ) { while ( sr.next() ) {
dp = (DataPoint) sr.get(); dp = (DataPoint) sr.get();
if ( dp.getId() == dpLast.getId() ) { if ( dp.getId() == dpLast.getId() ) {
@ -548,6 +551,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest {
} }
dp.setDescription( "done!" ); dp.setDescription( "done!" );
} }
}
t.commit(); t.commit();
s.clear(); s.clear();

View File

@ -60,7 +60,7 @@ 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 ) {
@ -71,10 +71,10 @@ public class ResultTransformerTest {
} }
Object obj = sr.get(); Object obj = sr.get();
assertTrue(obj instanceof PartnerA); assertTrue( obj instanceof PartnerA );
PartnerA obj2 = (PartnerA) obj; PartnerA obj2 = (PartnerA) obj;
assertEquals("Partner A", obj2.getName()); 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,7 +105,7 @@ 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 ) );
@ -114,6 +114,7 @@ public class StatelessSessionFetchingTest {
assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
} }
} }
}
); );
} }
@ -148,7 +149,7 @@ 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 ) );
@ -157,6 +158,7 @@ public class StatelessSessionFetchingTest {
assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) ); assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
} }
} }
}
); );
} }
@ -189,12 +191,12 @@ 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();
@ -207,6 +209,7 @@ public class StatelessSessionFetchingTest {
} }
} }
} }
}
); );
} }

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()) {
stream.forEach( i -> {
assertTyping( MyEntity.class, 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" )
.getResultStream()) {
data.forEach( i -> { data.forEach( i -> {
assertTyping( Integer.class, i[0] ); assertTyping( Integer.class, i[0] );
assertTyping( String.class, i[1] ); assertTyping( String.class, i[1] );
} ); } );
}
} ); } );
} }