/** * Creates a new {@link HibernateScrollableResultsIterator} for the given {@link Query}. * * @param jpaQuery must not be {@literal null}. */ HibernateScrollableResultsIterator(Query jpaQuery) { org.hibernate.query.Query<?> query = jpaQuery.unwrap(org.hibernate.query.Query.class); this.scrollableResults = query.setReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly())// .scroll(ScrollMode.FORWARD_ONLY); }
@Test public void testIncompleteScrollFirstResultInTransaction() { Session s = openSession(); Transaction tx = s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll(); results.next(); Parent p = (Parent) results.get( 0 ); assertResultFromOneUser( p ); tx.commit(); s.close(); }
@Test public void testScroll() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc, c.name asc" ).scroll(); List list = new ArrayList(); while ( results.next() ) { list.add( results.get( 0 ) ); } assertResultFromAllUsers( list ); s.getTransaction().commit(); s.close(); }
@Test public void testIncompleteScrollFirstResult() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll(); results.next(); Parent p = (Parent) results.get( 0 ); assertResultFromOneUser( p ); s.getTransaction().commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1283" ) public void testScrollOrderParentAscChildrenDesc() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc, c.name desc" ).scroll(); List list = new ArrayList(); while ( results.next() ) { list.add( results.get( 0 ) ); } assertResultFromAllUsers( list ); s.getTransaction().commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1283" ) public void testScrollOrderParentAsc() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll(); List list = new ArrayList(); while ( results.next() ) { list.add( results.get( 0 ) ); } assertResultFromAllUsers( list ); s.getTransaction().commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1283" ) public void testScrollOrderParentDesc() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name desc" ).scroll(); List list = new ArrayList(); while ( results.next() ) { list.add( results.get( 0 ) ); } assertResultFromAllUsers( list ); s.getTransaction().commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1283" ) public void testScrollOrderParentAscChildrenAsc() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc, c.name asc" ).scroll(); List list = new ArrayList(); while ( results.next() ) { list.add( results.get( 0 ) ); } assertResultFromAllUsers( list ); s.getTransaction().commit(); s.close(); }
@Test public void testTupleReturnFails() { Session s = openSession(); Transaction txn = s.beginTransaction(); try { s.createQuery( "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll(); fail( "scroll allowed with collection fetch and reurning tuples" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( HibernateException e ) { // expected result... } txn.commit(); s.close(); }
@Test public void testSetParameters() { final List params = new ArrayList(); params.add( new BigInteger( "2" ) ); params.add( new BigInteger( "3" ) ); try (Session s = openSession()) { final Query query = s.createNativeQuery( "select e.big from MY_ENTITY e where e.big in (:bigValues)" ) .setParameter( "bigValues", params ); try (ScrollableResults scroll = query.scroll()) { while ( scroll.next() ) { assertThat( scroll.get()[0], not( nullValue()) ); } } } }
@Test @TestForIssue( jiraKey = "HHH-1283" ) public void testIncompleteScrollSecondResultInTransaction() { Session s = openSession(); Transaction tx = s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll(); results.next(); Parent p = (Parent) results.get( 0 ); assertResultFromOneUser( p ); results.next(); p = (Parent) results.get( 0 ); assertResultFromOneUser( p ); tx.commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1283" ) public void testIncompleteScrollSecondResult() { Session s = openSession(); s.beginTransaction(); ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll(); results.next(); Parent p = (Parent) results.get( 0 ); assertResultFromOneUser( p ); results.next(); p = (Parent) results.get( 0 ); assertResultFromOneUser( p ); s.getTransaction().commit(); s.close(); }
@Test @SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors") public void testScrollMethod() { final String entityName = "expected"; insertTestEntity( entityName ); try (Session session = openSession()) { final CriteriaQuery<TestEntity> query = createTestEntityCriteriaQuery( entityName, session ); try (final ScrollableResults scroll = session.createQuery( query ).scroll()) { assertThat( scroll.first(), is( true ) ); } } }
@Test @TestForIssue(jiraKey = "HHH-10860") public void testScrollableResults() { final List params = new ArrayList(); params.add( 1L ); params.add( 2L ); try (Session s = openSession()) { final Query query = s.createQuery( "from MyEntity e where e.id in (:ids)" ) .setParameter( "ids", params ) .setFetchSize( 10 ); try (ScrollableResults scroll = query.scroll( ScrollMode.FORWARD_ONLY )) { int i = 0; while ( scroll.next() ) { if ( i == 0 ) { assertThat( ((MyEntity) scroll.get()[0]).getDescription(), is( "entity_1" ) ); } else { assertThat( ((MyEntity) scroll.get()[0]).getDescription(), is( "entity_2" ) ); } i++; } } } }
@Test @RequiresDialectFeature( value = DialectChecks.SupportsResultSetPositioningOnForwardOnlyCursorCheck.class, comment = "Driver does not support result set positioning methods on forward-only cursors" ) public void testScrollingJoinFetchesForward() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction txn = s.beginTransaction(); ScrollableResults results = s .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" ) .setString( "desc", "root%" ) .scroll( ScrollMode.FORWARD_ONLY ); int counter = 0; while ( results.next() ) { counter++; Animal animal = ( Animal ) results.get( 0 ); checkResult( animal ); } assertEquals( "unexpected result count", 2, counter ); txn.commit(); s.close(); data.cleanup(); }
@Test public void test_hql_api_scroll_open_example() { ScrollableResults scrollableResults = doInJPA( this::entityManagerFactory, entityManager -> { Session session = entityManager.unwrap( Session.class ); return session.createQuery( "select p " + "from Person p " + "where p.name like :name" ) .setParameter( "name", "J%" ) .scroll(); }); try { scrollableResults.next(); fail("Should throw exception because the ResultSet must be closed by now!"); } catch ( Exception expected ) { } }
@Test @TestForIssue(jiraKey = "HHH-10860") public void testScrollableResults2() { final List params = new ArrayList(); params.add( 1L ); params.add( 2L ); try (Session s = openSession()) { final Query query = s.createQuery( "from MyEntity e where e.id in (:ids)" ) .setParameter( "ids", params ) .setFetchSize( 10 ); try (ScrollableResults scroll = query.scroll( )) { int i = 0; while ( scroll.next() ) { if ( i == 0 ) { assertThat( ((MyEntity) scroll.get()[0]).getDescription(), is( "entity_1" ) ); } else { assertThat( ((MyEntity) scroll.get()[0]).getDescription(), is( "entity_2" ) ); } i++; } } } }
@Test @SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables") @SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors.") public void testScrollingJoinFetchesReverse() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction txn = s.beginTransaction(); ScrollableResults results = s .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" ) .setString( "desc", "root%" ).scroll(); results.afterLast(); int counter = 0; while ( results.previous() ) { counter++; Animal animal = ( Animal ) results.get( 0 ); checkResult( animal ); } assertEquals( "unexpected result count", 2, counter ); txn.commit(); s.close(); data.cleanup(); }
@Test public void test_hql_api_scroll_projection_example() { doInJPA( this::entityManagerFactory, entityManager -> { Session session = entityManager.unwrap( Session.class ); //tag::hql-api-scroll-example[] try ( ScrollableResults scrollableResults = session.createQuery( "select p " + "from Person p " + "where p.name like :name" ) .setParameter( "name", "J%" ) .scroll() ) { while(scrollableResults.next()) { Person person = (Person) scrollableResults.get()[0]; process(person); } } //end::hql-api-scroll-example[] }); }
@Test public void testAssertSubclassInsertedSuccessfullyAfterFlush() { doInHibernate( this::sessionFactory, s -> { Employee e = new Employee(); e.setName( "Mark" ); e.setTitle( "internal sales" ); e.setSex( 'M' ); e.setAddress( "buckhead" ); e.setZip( "30305" ); e.setCountry( "USA" ); s.save( e ); s.flush(); long numberOfInsertedEmployee = (long) s.createQuery( "select count(e) from Employee e" ).uniqueResult(); Assert.assertEquals( 1L, numberOfInsertedEmployee ); } ); doInHibernate( this::sessionFactory, s -> { int i = 0; ScrollableResults sr = s.createQuery( "select e from Employee e" ) .scroll( ScrollMode.FORWARD_ONLY ); while ( sr.next() ) { Employee e = (Employee) sr.get( 0 ); s.delete( e ); } } ); }