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

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

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

Query.setFirstResult介绍

[英]Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0.
[中]设置要检索的第一行。如果未设置,将从第0行开始检索行。

代码示例

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

/**
 * Read a page of data, clearing the existing session (if necessary) first,
 * and creating a new session before executing the query.
 *
 * @param page the page to read (starting at 0)
 * @param pageSize the size of the page or maximum number of items to read
 * @param fetchSize the fetch size to use
 * @param parameterValues the parameter values to use (if any, otherwise
 * null)
 * @return a collection of items
 */
public Collection<? extends T> readPage(int page, int pageSize, int fetchSize, Map<String, Object> parameterValues) {
  clear();
  Query query = createQuery();
  if (parameterValues != null) {
    query.setProperties(parameterValues);
  }
  @SuppressWarnings("unchecked")
  List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize).list();
  return result;
}

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

@Test
public void testPSCache() throws Exception {
  Session s = openSession();
  Transaction txn = s.beginTransaction();
  for ( int i=0; i<10; i++ ) s.save( new Foo() );
  Query q = s.createQuery("from Foo");
  q.setMaxResults(2);
  q.setFirstResult(5);
  assertTrue( q.list().size()==2 );
  q = s.createQuery("from Foo");
  assertTrue( q.list().size()==10 );
  assertTrue( q.list().size()==10 );
  q.setMaxResults(3);
  q.setFirstResult(3);
  assertTrue( q.list().size()==3 );
  q = s.createQuery("from Foo");
  assertTrue( q.list().size()==10 );
  txn.commit();
  s.close();
  s = openSession();
  txn = s.beginTransaction();
  q = s.createQuery("from Foo");
  assertTrue( q.list().size()==10 );
  q.setMaxResults(5);
  assertTrue( q.list().size()==5 );
  doDelete( s, "from Foo" );
  txn.commit();
  s.close();
}

代码示例来源:origin: gocd/gocd

public Modifications getModificationsFor(final MaterialInstance materialInstance, final Pagination pagination) {
  String key = materialModificationsWithPaginationKey(materialInstance);
  String subKey = materialModificationsWithPaginationSubKey(pagination);
  Modifications modifications = (Modifications) goCache.get(key, subKey);
  if (modifications == null) {
    synchronized (key) {
      modifications = (Modifications) goCache.get(key, subKey);
      if (modifications == null) {
        List<Modification> modificationsList = (List<Modification>) getHibernateTemplate().execute((HibernateCallback) session -> {
          Query q = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC");
          q.setFirstResult(pagination.getOffset());
          q.setMaxResults(pagination.getPageSize());
          q.setLong(0, materialInstance.getId());
          return q.list();
        });
        if (!modificationsList.isEmpty()) {
          modifications = new Modifications(modificationsList);
          goCache.put(key, subKey, modifications);
        }
      }
    }
  }
  return modifications;
}

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

@Test
public void testHql() {
  Session session = openSession();
  session.beginTransaction();
  Query qry = session.createQuery( "from Door" );
  qry.getLockOptions().setLockMode( LockMode.PESSIMISTIC_WRITE );
  qry.setFirstResult( 2 );
  qry.setMaxResults( 2 );
  @SuppressWarnings("unchecked") List<Door> results = qry.list();
  assertEquals( 2, results.size() );
  for ( Door door : results ) {
    assertEquals( LockMode.PESSIMISTIC_WRITE, session.getCurrentLockMode( door ) );
  }
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void testFirstResult() {
  doInHibernate(
      this::sessionFactory,
      session -> {
        Query query = session.createQuery( "from Employee" );
        // not initialized yet
        assertNull( query.getHibernateFirstResult() );
        // the following is special case; when initialized to -1, getHibernateFirstResult returns 0
        assertEquals( Integer.valueOf( 0 ), query.setHibernateFirstResult( -1 ).getHibernateFirstResult() );
        assertEquals( Integer.valueOf( 0 ), query.setHibernateFirstResult( 0 ).getHibernateFirstResult() );
        assertEquals( Integer.valueOf( 1 ), query.setHibernateFirstResult( 1 ).getHibernateFirstResult() );
        assertEquals( Integer.valueOf( 10 ), query.setFirstResult( 10 ).getHibernateFirstResult() );
      }
  );
}

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

q.setString(1, "SIMPLE 1");
q.setString( 0, "Simple 1" );
q.setFirstResult(0);
assertTrue( q.iterate().hasNext() );
q = s.createQuery("from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1");
q.setString( "foo", "Simple 1" );
q.setInteger("count", 69);
q.setFirstResult(0);
assertTrue( q.iterate().hasNext() );
q = s.createQuery("select s.id from Simple s");
q.setFirstResult(1);
q.setMaxResults( 2 );
iter = q.iterate();

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

q.setString(1, "SIMPLE 1");
q.setString(0, "Simple 1");
q.setFirstResult(0);
assertTrue( q.iterate().hasNext() );
q = s.createQuery("from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1");
q.setString("foo", "Simple 1");
q.setInteger("count", 69);
q.setFirstResult(0);
assertTrue( q.iterate().hasNext() );
q = s.createQuery("select s.id from Simple s");
q.setFirstResult(1);
q.setMaxResults(2);
iter = q.iterate();

代码示例来源:origin: bill1012/AdminEAP

@SuppressWarnings("unchecked")
public <T> List<T> find(String hql, Map<String, Object> params, int page, int rows) {
  Query query = this.getCurrentSession().createQuery(hql);
  if (params != null && !params.isEmpty()) {
    for (String key : params.keySet()) {
      query.setParameter(key, params.get(key));
    }
  }
  return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}

代码示例来源:origin: bill1012/AdminEAP

@SuppressWarnings("unchecked")
public <T> List<T> find(String hql, int page, int rows) {
  Query query = this.getCurrentSession().createQuery(hql);
  return query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}

代码示例来源:origin: openmrs/openmrs-core

/**
 * @see UserDAO#getUsers(String, List, boolean, Integer, Integer)
 */
@Override
@SuppressWarnings("unchecked")
public List<User> getUsers(String name, List<Role> roles, boolean includeRetired, Integer start, Integer length) {
  
  String hqlSelectStart = "select distinct user from User as user inner join user.person.names as name ";
  Query query = createUserSearchQuery(name, roles, includeRetired, hqlSelectStart);
  
  if (start != null) {
    query.setFirstResult(start);
  }
  if (length != null && length > 0) {
    query.setMaxResults(length);
  }
  
  List<User> returnList = query.list();
  
  if (!CollectionUtils.isEmpty(returnList)) {
    returnList.sort(new UserByNameComparator());
  }
  
  return returnList;
}

代码示例来源:origin: my2iu/Jinq

if (offset > 0) q.setFirstResult(offset);
long pageSize = Long.MAX_VALUE;
if (hints.automaticResultsPagingSize > 0)

代码示例来源:origin: bill1012/AdminEAP

@Override
public List findMapBySql(String sql, int firstResult, int maxResult, Object[] params, Type[] types, Class clazz) {
  if (clazz != null)
    return this.getCurrentSession().createSQLQuery(sql).setParameters(params, types).setFirstResult(firstResult)
        .setMaxResults(maxResult).setResultTransformer(Transformers.aliasToBean(clazz)).list();
  else
    return this.getCurrentSession().createSQLQuery(sql).setParameters(params, types).setFirstResult(firstResult)
        .setMaxResults(maxResult).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
}

代码示例来源:origin: Impetus/Kundera

/**
 * Find.
 * 
 * @param query
 *            the native fquery
 * @param parameterMap
 *            the parameter map
 * @param maxResult 
 * @param firstResult 
 * @return the list
 */
public List findByQuery(String query, Map<Parameter, Object> parameterMap, int firstResult, int maxResult)
{
  s = getStatelessSession();
  Query q = s.createQuery(query);
  q.setFirstResult(firstResult);
  q.setMaxResults(maxResult);
  setParameters(parameterMap, q);
  return q.list();
}

代码示例来源:origin: yahoo/elide

@Override
public Query setFirstResult(int num) {
  this.query = query.setFirstResult(num);
  return this;
}

代码示例来源:origin: yahoo/elide

@Override
public Query setFirstResult(int num) {
  this.query = query.setFirstResult(num);
  return this;
}

代码示例来源:origin: Impetus/Kundera

/**
 * Find.
 * 
 * @param query
 *            the native fquery
 * @param parameterMap
 *            the parameter map
 * @param maxResult 
 * @param firstResult 
 * @return the list
 */
public int onExecuteUpdate(String query, Map<Parameter, Object> parameterMap, int firstResult, int maxResult)
{
  s = getStatelessSession();
  Query q = s.createQuery(query);
  
  q.setFirstResult(firstResult);
  q.setMaxResults(maxResult);
  
  setParameters(parameterMap, q);
  Transaction tx = onBegin();
  int i = q.executeUpdate();
  onCommit(tx);
  // tx.commit();
  return i;
}

代码示例来源:origin: sakaiproject/sakai

private List<Long> itemIdsNeedingHashBackfill(Map<String, String> hqlQueries, int pageSize, int pageStart, Session session) {
  return session
      .createQuery(hqlQueries.get(ALL_HASH_BACKFILLABLE_ITEM_IDS_HQL))
      .setFirstResult(pageStart)
      .setMaxResults(pageSize)
      .list();
}

代码示例来源:origin: debop/hibernate-redis

public static Query setPaging(Query query, int firstResult, int maxResults) {
  if (firstResult >= 0)
   query.setFirstResult(firstResult);
  if (maxResults > 0)
   query.setMaxResults(maxResults);

  return query;
 }
}

代码示例来源:origin: denimgroup/threadfix

@Override
@SuppressWarnings("unchecked")
public List<ExceptionLog> retrievePage(int page, int numberToShow) {
  return sessionFactory.getCurrentSession()
      .createQuery("from ExceptionLog log order by log.time desc")
      .setMaxResults(numberToShow)
      .setFirstResult((page - 1) * numberToShow)
      .list();
}

代码示例来源:origin: xiaolongzuo/niubi-job

@SuppressWarnings("unchecked")
private <T extends AbstractEntity> List<T> getDataList(List<Object> valueList, StringBuffer sqlBuffer, Pager<T> pager) {
  String querySql = sqlBuffer.toString() + " order by createDate desc";
  Query query = getHibernateSession().createQuery(querySql);
  setParameters(query, valueList);
  query.setFirstResult(pager.getFirstIndex());
  query.setMaxResults(pager.getPageSize());
  return query.list();
}

相关文章