org.hibernate.ScrollableResults.get()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(108)

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

ScrollableResults.get介绍

[英]Get the current row of results.
[中]获取当前的结果行。

代码示例

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

@Override
public Object next() {
  if (scrollableResults == null) {
    throw new NoSuchElementException("No ScrollableResults");
  }
  // Cast needed for Hibernate 6 compatibility
  Object[] row = (Object[]) scrollableResults.get();
  return row.length == 1 ? row[0] : row;
}

代码示例来源: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 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
@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: stackoverflow.com

StatelessSession session = ((Session) entityManager.getDelegate()).getSessionFactory().openStatelessSession();
   Query query = session
       .createQuery("SELECT a FROM Address a WHERE .... ORDER BY a.id");
   query.setFetchSize(Integer.valueOf(1000));
   query.setReadOnly(true);
   query.setLockMode("a", LockMode.NONE);
   ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
   while (results.next()) {
     Address addr = (Address) results.get(0);
     // Do stuff
   }
   results.close();
   session.close();

代码示例来源: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
public void testScrollOrderChildrenDesc() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  Parent p0 = new Parent( "parent0" );
  s.save( p0 );
  t.commit();
  s.close();
  s = openSession();
  ScrollableResults results = s.createQuery( QUERY + " order by c.name desc" ).scroll();
  List list = new ArrayList();
  while ( results.next() ) {
    list.add( results.get( 0 ) );
  }
  try {
    assertResultFromAllUsers( list );
    fail( "should have failed because data is ordered incorrectly." );
  }
  catch ( AssertionError ex ) {
    // expected
  }
  finally {
    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-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
@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 testScrollCriteria() {
  Session session = openSession();
  Transaction t = session.beginTransaction();
  Course course = new Course();
  course.setCourseCode("HIB");
  course.setDescription("Hibernate Training");
  session.persist(course);
  session.flush();
  session.clear();
  ScrollableResults sr = session.createCriteria(Course.class).setReadOnly( true ).scroll();
  assertTrue( sr.next() );
  course = (Course) sr.get(0);
  assertNotNull(course);
  assertTrue( session.isReadOnly( course ) );
  sr.close();
  session.delete(course);
  t.commit();
  session.close();
}

代码示例来源: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 );
    }
  } );
}

相关文章