org.hibernate.query.Query.scroll()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(278)

本文整理了Java中org.hibernate.query.Query.scroll方法的一些代码示例,展示了Query.scroll的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.scroll方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称:Query
方法名:scroll

Query.scroll介绍

[英]Return the query results as ScrollableResults. The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.
[中]将查询结果作为可滚动结果返回。返回结果的可滚动性取决于JDBC驱动程序对可滚动结果集的支持。

代码示例

代码示例来源:origin: spring-projects/spring-data-jpa

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

@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 ) );
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@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++;
      }
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

@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 ) {
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@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++;
      }
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

@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[]
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@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 );
    }
  } );
}

相关文章