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

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

本文整理了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

  1. @Test
  2. public void testQuerySetFirstResult() {
  3. final Session session = sessionFactory().openSession();
  4. final Query qry = session.createQuery( "select i from Item i" );
  5. session.close();
  6. assertThat( session.isOpen(), CoreMatchers.is ( false ) );
  7. try {
  8. qry.setFirstResult( 1 );
  9. fail( "Expecting call to fail" );
  10. }
  11. catch (IllegalStateException expected) {
  12. }
  13. }

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

  1. @SuppressWarnings( {"unchecked"})
  2. @Test
  3. public void testDistinctSelectWithJoin() {
  4. feedDatabase();
  5. Session s = openSession();
  6. 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();
  7. // System.out.println(entries);
  8. Entry firstEntry = entries.remove(0);
  9. assertFalse("The list of entries should not contain dublicated Entry objects as we've done a distinct select", entries.contains(firstEntry));
  10. s.close();
  11. }
  12. }

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

  1. @Test
  2. @RequiresDialectFeature(
  3. value = DialectChecks.SupportLimitAndOffsetCheck.class,
  4. comment = "dialect does not support offset and limit combo"
  5. )
  6. public void testSimpleSelectWithLimitAndOffset() throws Exception {
  7. // just checking correctness of param binding code...
  8. Session session = openSession();
  9. Transaction t = session.beginTransaction();
  10. session.createQuery( "from Animal" )
  11. .setFirstResult( 2 )
  12. .setMaxResults( 1 )
  13. .list();
  14. t.commit();
  15. session.close();
  16. }

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

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

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

  1. @Test
  2. public void testLimitZero() throws Exception {
  3. TransactionUtil.doInHibernate( this::sessionFactory, s -> {
  4. Iterator iter = s.createQuery( "from Person p" )
  5. .setMaxResults( 0 )
  6. .iterate();
  7. int count = 0;
  8. while ( iter.hasNext() ) {
  9. iter.next();
  10. count++;
  11. }
  12. assertEquals( 0, count );
  13. final List list = s.createQuery( "select p from Person p" )
  14. .setMaxResults( 0 )
  15. .setFirstResult( 2 )
  16. .list();
  17. assertTrue( list.isEmpty() );
  18. } );
  19. }

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

  1. @Test
  2. public void testPessimisticLockWithFirstResultsThenFollowOnLocking() {
  3. final Session session = openSession();
  4. session.beginTransaction();
  5. sqlStatementInterceptor.getSqlQueries().clear();
  6. List<Product> products =
  7. session.createQuery(
  8. "select p from Product p", Product.class )
  9. .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
  10. .setFirstResult( 40 )
  11. .setMaxResults( 10 )
  12. .getResultList();
  13. assertEquals( 10, products.size() );
  14. assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
  15. session.getTransaction().commit();
  16. session.close();
  17. }

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

  1. Iterator iter = s.createQuery("from Foo foo")
  2. .setMaxResults(4)
  3. .setFirstResult(2)
  4. .iterate();
  5. int count=0;
  6. iter = s.createQuery("select foo from Foo foo")
  7. .setMaxResults(2)
  8. .setFirstResult(2)
  9. .list()
  10. .iterator();

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

  1. @Test
  2. public void testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
  3. final Session session = openSession();
  4. session.beginTransaction();
  5. sqlStatementInterceptor.getSqlQueries().clear();
  6. List<Product> products =
  7. session.createQuery(
  8. "select p from Product p", Product.class )
  9. .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
  10. .setFollowOnLocking( true ) )
  11. .setFirstResult( 40 )
  12. .setMaxResults( 10 )
  13. .getResultList();
  14. assertEquals( 10, products.size() );
  15. assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
  16. session.getTransaction().commit();
  17. session.close();
  18. }

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

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

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

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

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

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

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

  1. @Test
  2. public void testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails() {
  3. final Session session = openSession();
  4. session.beginTransaction();
  5. sqlStatementInterceptor.getSqlQueries().clear();
  6. try {
  7. List<Product> products =
  8. session.createQuery(
  9. "select p from Product p", Product.class )
  10. .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
  11. .setFollowOnLocking( false ) )
  12. .setFirstResult( 40 )
  13. .setMaxResults( 10 )
  14. .getResultList();
  15. fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" );
  16. }
  17. catch ( PersistenceException expected ) {
  18. assertEquals(
  19. SQLGrammarException.class,
  20. expected.getCause().getClass()
  21. );
  22. }
  23. }

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

  1. .setFirstResult( 5 )
  2. .setMaxResults( 20 )
  3. .list();

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

  1. protected void setQueryProperties(Query query) {
  2. if ( maxResults != null ) {
  3. query.setMaxResults( maxResults );
  4. }
  5. if ( firstResult != null ) {
  6. query.setFirstResult( firstResult );
  7. }
  8. if ( cacheable != null ) {
  9. query.setCacheable( cacheable );
  10. }
  11. if ( cacheRegion != null ) {
  12. query.setCacheRegion( cacheRegion );
  13. }
  14. if ( comment != null ) {
  15. query.setComment( comment );
  16. }
  17. if ( flushMode != null ) {
  18. query.setFlushMode( flushMode );
  19. }
  20. if ( cacheMode != null ) {
  21. query.setCacheMode( cacheMode );
  22. }
  23. if ( timeout != null ) {
  24. query.setTimeout( timeout );
  25. }
  26. if ( lockOptions != null && lockOptions.getLockMode() != LockMode.NONE ) {
  27. query.setLockMode( REFERENCED_ENTITY_ALIAS, lockOptions.getLockMode() );
  28. }
  29. }

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

  1. @Test
  2. @TestForIssue(jiraKey = "HHH-7752")
  3. public void testPaginationWithFormulaSubquery() {
  4. doInHibernate( this::sessionFactory, session -> {
  5. // populating test data
  6. Folder folder1 = new Folder( 1L, "Folder1" );
  7. Folder folder2 = new Folder( 2L, "Folder2" );
  8. Folder folder3 = new Folder( 3L, "Folder3" );
  9. session.persist( folder1 );
  10. session.persist( folder2 );
  11. session.persist( folder3 );
  12. session.flush();
  13. session.persist( new Contact( 1L, "Lukasz", "Antoniak", "owner", folder1 ) );
  14. session.persist( new Contact( 2L, "Kinga", "Mroz", "co-owner", folder2 ) );
  15. session.flush();
  16. session.clear();
  17. session.refresh( folder1 );
  18. session.refresh( folder2 );
  19. session.clear();
  20. List<Long> folderCount = session.createQuery( "select count(distinct f) from Folder f" ).setMaxResults( 1 ).list();
  21. assertEquals( Arrays.asList( 3L ), folderCount );
  22. List<Folder> distinctFolders = session.createQuery( "select distinct f from Folder f order by f.id desc" )
  23. .setFirstResult( 1 ).setMaxResults( 2 ).list();
  24. assertEquals( Arrays.asList( folder2, folder1 ), distinctFolders );
  25. } );
  26. }

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

  1. protected void initQueryFromNamedDefinition(Query query, NamedQueryDefinition nqd) {
  2. // todo : cacheable and readonly should be Boolean rather than boolean...
  3. query.setCacheable( nqd.isCacheable() );
  4. query.setCacheRegion( nqd.getCacheRegion() );
  5. query.setReadOnly( nqd.isReadOnly() );
  6. if ( nqd.getTimeout() != null ) {
  7. query.setTimeout( nqd.getTimeout() );
  8. }
  9. if ( nqd.getFetchSize() != null ) {
  10. query.setFetchSize( nqd.getFetchSize() );
  11. }
  12. if ( nqd.getCacheMode() != null ) {
  13. query.setCacheMode( nqd.getCacheMode() );
  14. }
  15. if ( nqd.getComment() != null ) {
  16. query.setComment( nqd.getComment() );
  17. }
  18. if ( nqd.getFirstResult() != null ) {
  19. query.setFirstResult( nqd.getFirstResult() );
  20. }
  21. if ( nqd.getMaxResults() != null ) {
  22. query.setMaxResults( nqd.getMaxResults() );
  23. }
  24. if ( nqd.getFlushMode() != null ) {
  25. query.setHibernateFlushMode( nqd.getFlushMode() );
  26. }
  27. }

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

  1. public Query<?> getQuery(Session session, String sql) {
  2. Query<?> query = session.createQuery(sql);
  3. if (null != map) {
  4. for (String key : map.keySet()) {
  5. query.setParameter(key, map.get(key));
  6. }
  7. }
  8. if (null != arrayMap) {
  9. for (String key : arrayMap.keySet()) {
  10. query.setParameterList(key, arrayMap.get(key));
  11. }
  12. }
  13. if (null != firstResult) {
  14. query.setFirstResult(firstResult);
  15. }
  16. if (null != maxResults) {
  17. query.setMaxResults(maxResults);
  18. }
  19. if (null != cacheable) {
  20. query.setCacheable(cacheable);
  21. } else {
  22. query.setCacheable(true);
  23. }
  24. return query;
  25. }

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

  1. public Query<?> getQuery(Session session, String sql) {
  2. Query<?> query = session.createQuery(sql);
  3. if (null != map) {
  4. for (String key : map.keySet()) {
  5. query.setParameter(key, map.get(key));
  6. }
  7. }
  8. if (null != arrayMap) {
  9. for (String key : arrayMap.keySet()) {
  10. query.setParameterList(key, arrayMap.get(key));
  11. }
  12. }
  13. if (null != firstResult) {
  14. query.setFirstResult(firstResult);
  15. }
  16. if (null != maxResults) {
  17. query.setMaxResults(maxResults);
  18. }
  19. if (null != cacheable) {
  20. query.setCacheable(cacheable);
  21. } else {
  22. query.setCacheable(true);
  23. }
  24. return query;
  25. }

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

  1. @Override
  2. public Query setFirstResult(final int firstResult) {
  3. if (queryV2ForCompare != null) {
  4. queryV2ForCompare.setFirstResult(firstResult);
  5. }
  6. query.setFirstResult(firstResult);
  7. return this;
  8. }

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

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

相关文章