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

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

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

Query.setFirstResult介绍

暂无

代码示例

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

@Test
public void testQuerySetFirstResult() {
  final Session session = sessionFactory().openSession();
  final Query qry = session.createQuery( "select i from Item i" );
  session.close();
  assertThat( session.isOpen(), CoreMatchers.is ( false ) );
  try {
    qry.setFirstResult( 1 );
    fail( "Expecting call to fail" );
  }
  catch (IllegalStateException expected) {
  }
}

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

@SuppressWarnings( {"unchecked"})
  @Test
  public void testDistinctSelectWithJoin() {
    feedDatabase();

    Session s = openSession();

    List<Entry> entries = s.createQuery("select distinct e from Entry e join e.tags t where t.surrogate != null order by e.name").setFirstResult(10).setMaxResults(5).list();

    // System.out.println(entries);
    Entry firstEntry = entries.remove(0);
    assertFalse("The list of entries should not contain dublicated Entry objects as we've done a distinct select", entries.contains(firstEntry));

    s.close();
  }
}

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

@Test
@RequiresDialectFeature(
    value = DialectChecks.SupportLimitAndOffsetCheck.class,
    comment = "dialect does not support offset and limit combo"
)
public void testSimpleSelectWithLimitAndOffset() throws Exception {
  // just checking correctness of param binding code...
  Session session = openSession();
  Transaction t = session.beginTransaction();
  session.createQuery( "from Animal" )
      .setFirstResult( 2 )
      .setMaxResults( 1 )
      .list();
  t.commit();
  session.close();
}

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

@Test
public void testPaginationWithHQLProjection() {
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 10; i < 20; i++ ) {
      session.persist( new Product2( i, "Kit" + i ) );
    }
    session.flush();
    session.clear();
    List list = session.createQuery(
        "select id, description as descr, (select max(id) from Product2) as maximum from Product2"
    ).setFirstResult( 2 ).setMaxResults( 2 ).list();
    assertEquals( 19, ( (Object[]) list.get( 1 ) )[2] );
    list = session.createQuery( "select id, description, (select max(id) from Product2) from Product2 order by id" )
        .setFirstResult( 2 ).setMaxResults( 2 ).list();
    assertEquals( 2, list.size() );
    assertArrayEquals( new Object[] {12, "Kit12", 19}, (Object[]) list.get( 0 ));
    assertArrayEquals( new Object[] {13, "Kit13", 19}, (Object[]) list.get( 1 ));
  } );
}

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

@Test
public void testLimitZero() throws Exception {
  TransactionUtil.doInHibernate( this::sessionFactory, s -> {
    Iterator iter = s.createQuery( "from Person p" )
        .setMaxResults( 0 )
        .iterate();
    int count = 0;
    while ( iter.hasNext() ) {
      iter.next();
      count++;
    }
    assertEquals( 0, count );
    final List list = s.createQuery( "select p from Person p" )
        .setMaxResults( 0 )
        .setFirstResult( 2 )
        .list();
    assertTrue( list.isEmpty() );
  } );
}

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

@Test
public void testPessimisticLockWithFirstResultsThenFollowOnLocking() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  List<Product> products =
      session.createQuery(
          "select p from Product p", Product.class )
          .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
          .setFirstResult( 40 )
          .setMaxResults( 10 )
          .getResultList();
  assertEquals( 10, products.size() );
  assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
  session.getTransaction().commit();
  session.close();
}

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

Iterator iter = s.createQuery("from Foo foo")
  .setMaxResults(4)
  .setFirstResult(2)
  .iterate();
int count=0;
iter = s.createQuery("select foo from Foo foo")
  .setMaxResults(2)
  .setFirstResult(2)
  .list()
  .iterator();

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

@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  List<Product> products =
      session.createQuery(
          "select p from Product p", Product.class )
          .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
                       .setFollowOnLocking( true ) )
          .setFirstResult( 40 )
          .setMaxResults( 10 )
          .getResultList();
  assertEquals( 10, products.size() );
  assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
  session.getTransaction().commit();
  session.close();
}

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

@Test
@TestForIssue(jiraKey = "HHH-7781")
public void testPaginationWithCastOperator() {
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 40; i < 50; i++ ) {
      session.persist( new Product2( i, "Kit" + i ) );
    }
    session.flush();
    session.clear();
    List<Object[]> list = session.createQuery( "select p.id, cast(p.id as string) as string_id from Product2 p order by p.id" )
        .setFirstResult( 1 ).setMaxResults( 2 ).list();
    assertEquals( 2, list.size() );
    assertArrayEquals( new Object[] { 41, "41" }, list.get( 0 ) );
    assertArrayEquals( new Object[] { 42, "42" }, list.get( 1 ) );
  } );
}

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

@Test
public void testPaginationWithHQL() {
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 20; i < 30; i++ ) {
      session.persist( new Product2( i, "Kit" + i ) );
    }
    session.flush();
    session.clear();
    List list = session.createQuery( "from Product2 order by id" ).setFirstResult( 3 ).setMaxResults( 2 ).list();
    assertEquals( Arrays.asList( new Product2( 23, "Kit23" ), new Product2( 24, "Kit24" ) ), list );
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-7370")
public void testPaginationWithMaxOnly() {
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 30; i < 40; i++ ) {
      session.persist( new Product2( i, "Kit" + i ) );
    }
    session.flush();
    session.clear();
    List list = session.createQuery( "from Product2 order by id" ).setFirstResult( 0 ).setMaxResults( 2 ).list();
    assertEquals( Arrays.asList( new Product2( 30, "Kit30" ), new Product2( 31, "Kit31" ) ), list );
    list = session.createQuery( "select distinct p from Product2 p order by p.id" ).setMaxResults( 1 ).list();
    assertEquals( Collections.singletonList( new Product2( 30, "Kit30" ) ), list );
  } );
}

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

@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  try {
    List<Product> products =
        session.createQuery(
            "select p from Product p", Product.class )
            .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
                         .setFollowOnLocking( false ) )
            .setFirstResult( 40 )
            .setMaxResults( 10 )
            .getResultList();
    fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" );
  }
  catch ( PersistenceException expected ) {
    assertEquals(
        SQLGrammarException.class,
        expected.getCause().getClass()
    );
  }
}

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

.setFirstResult( 5 )
.setMaxResults( 20 )
.list();

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

protected void setQueryProperties(Query query) {
  if ( maxResults != null ) {
    query.setMaxResults( maxResults );
  }
  if ( firstResult != null ) {
    query.setFirstResult( firstResult );
  }
  if ( cacheable != null ) {
    query.setCacheable( cacheable );
  }
  if ( cacheRegion != null ) {
    query.setCacheRegion( cacheRegion );
  }
  if ( comment != null ) {
    query.setComment( comment );
  }
  if ( flushMode != null ) {
    query.setFlushMode( flushMode );
  }
  if ( cacheMode != null ) {
    query.setCacheMode( cacheMode );
  }
  if ( timeout != null ) {
    query.setTimeout( timeout );
  }
  if ( lockOptions != null && lockOptions.getLockMode() != LockMode.NONE ) {
    query.setLockMode( REFERENCED_ENTITY_ALIAS, lockOptions.getLockMode() );
  }
}

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

@Test
@TestForIssue(jiraKey = "HHH-7752")
public void testPaginationWithFormulaSubquery() {
  doInHibernate( this::sessionFactory, session -> {
    // populating test data
    Folder folder1 = new Folder( 1L, "Folder1" );
    Folder folder2 = new Folder( 2L, "Folder2" );
    Folder folder3 = new Folder( 3L, "Folder3" );
    session.persist( folder1 );
    session.persist( folder2 );
    session.persist( folder3 );
    session.flush();
    session.persist( new Contact( 1L, "Lukasz", "Antoniak", "owner", folder1 ) );
    session.persist( new Contact( 2L, "Kinga", "Mroz", "co-owner", folder2 ) );
    session.flush();
    session.clear();
    session.refresh( folder1 );
    session.refresh( folder2 );
    session.clear();
    List<Long> folderCount = session.createQuery( "select count(distinct f) from Folder f" ).setMaxResults( 1 ).list();
    assertEquals( Arrays.asList( 3L ), folderCount );
    List<Folder> distinctFolders = session.createQuery( "select distinct f from Folder f order by f.id desc" )
        .setFirstResult( 1 ).setMaxResults( 2 ).list();
    assertEquals( Arrays.asList( folder2, folder1 ), distinctFolders );
  } );
}

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

protected void initQueryFromNamedDefinition(Query query, NamedQueryDefinition nqd) {
  // todo : cacheable and readonly should be Boolean rather than boolean...
  query.setCacheable( nqd.isCacheable() );
  query.setCacheRegion( nqd.getCacheRegion() );
  query.setReadOnly( nqd.isReadOnly() );
  if ( nqd.getTimeout() != null ) {
    query.setTimeout( nqd.getTimeout() );
  }
  if ( nqd.getFetchSize() != null ) {
    query.setFetchSize( nqd.getFetchSize() );
  }
  if ( nqd.getCacheMode() != null ) {
    query.setCacheMode( nqd.getCacheMode() );
  }
  if ( nqd.getComment() != null ) {
    query.setComment( nqd.getComment() );
  }
  if ( nqd.getFirstResult() != null ) {
    query.setFirstResult( nqd.getFirstResult() );
  }
  if ( nqd.getMaxResults() != null ) {
    query.setMaxResults( nqd.getMaxResults() );
  }
  if ( nqd.getFlushMode() != null ) {
    query.setHibernateFlushMode( nqd.getFlushMode() );
  }
}

代码示例来源:origin: sanluan/PublicCMS

public Query<?> getQuery(Session session, String sql) {
  Query<?> query = session.createQuery(sql);
  if (null != map) {
    for (String key : map.keySet()) {
      query.setParameter(key, map.get(key));
    }
  }
  if (null != arrayMap) {
    for (String key : arrayMap.keySet()) {
      query.setParameterList(key, arrayMap.get(key));
    }
  }
  if (null != firstResult) {
    query.setFirstResult(firstResult);
  }
  if (null != maxResults) {
    query.setMaxResults(maxResults);
  }
  if (null != cacheable) {
    query.setCacheable(cacheable);
  } else {
    query.setCacheable(true);
  }
  return query;
}

代码示例来源:origin: sanluan/PublicCMS

public Query<?> getQuery(Session session, String sql) {
  Query<?> query = session.createQuery(sql);
  if (null != map) {
    for (String key : map.keySet()) {
      query.setParameter(key, map.get(key));
    }
  }
  if (null != arrayMap) {
    for (String key : arrayMap.keySet()) {
      query.setParameterList(key, arrayMap.get(key));
    }
  }
  if (null != firstResult) {
    query.setFirstResult(firstResult);
  }
  if (null != maxResults) {
    query.setMaxResults(maxResults);
  }
  if (null != cacheable) {
    query.setCacheable(cacheable);
  } else {
    query.setCacheable(true);
  }
  return query;
}

代码示例来源:origin: com.atlassian.hibernate/hibernate.adapter

@Override
public Query setFirstResult(final int firstResult) {
  if (queryV2ForCompare != null) {
    queryV2ForCompare.setFirstResult(firstResult);
  }
  query.setFirstResult(firstResult);
  return this;
}

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

@Test
public void testFirstResultAndMaxRows() throws Exception {
  List<?> result = session.createQuery( "from Hypothesis h where h.description IS NOT null ORDER BY id" )
      .setFirstResult( 2 )
      .setMaxResults( 3 )
      .list();
  assertThat( result ).onProperty( "id" ).containsOnly( "15", "16", "17" );
}

相关文章