org.hibernate.Criteria.setMaxResults()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(207)

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

Criteria.setMaxResults介绍

[英]Set a limit upon the number of objects to be retrieved.
[中]设置要检索的对象数量的限制。

代码示例

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

@Override
@SuppressWarnings({"unchecked", "deprecation"})
public <T> List<T> findByExample(
    @Nullable final String entityName, final T exampleEntity, final int firstResult, final int maxResults)
    throws DataAccessException {
  Assert.notNull(exampleEntity, "Example entity must not be null");
  return nonNull(executeWithNativeSession((HibernateCallback<List<T>>) session -> {
    Criteria executableCriteria = (entityName != null ?
        session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass()));
    executableCriteria.add(Example.create(exampleEntity));
    prepareCriteria(executableCriteria);
    if (firstResult >= 0) {
      executableCriteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      executableCriteria.setMaxResults(maxResults);
    }
    return executableCriteria.list();
  }));
}

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

public ServerBackup lastBackup() {
  List results = (List) getHibernateTemplate().execute((HibernateCallback) session -> {
    Criteria criteria = session.createCriteria(ServerBackup.class);
    criteria.setMaxResults(1);
    criteria.addOrder(Order.desc("id"));
    return criteria.list();
  });
  return results.isEmpty() ? null : (ServerBackup) results.get(0);
}

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

@Override
@SuppressWarnings("unchecked")
public List<?> findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults)
    throws DataAccessException {
  Assert.notNull(criteria, "DetachedCriteria must not be null");
  return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
    Criteria executableCriteria = criteria.getExecutableCriteria(session);
    prepareCriteria(executableCriteria);
    if (firstResult >= 0) {
      executableCriteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      executableCriteria.setMaxResults(maxResults);
    }
    return executableCriteria.list();
  }));
}

代码示例来源:origin: stackoverflow.com

Criteria c = session.createCriteria(Thingy.class);
c.addOrder(Order.desc("id"));
c.setMaxResults(1);
return (Thingy)c.uniqueResult();

代码示例来源:origin: kaaproject/kaa

@Override
public EndpointProfileSchema findLatestByAppId(String appId) {
 EndpointProfileSchema latestSchema = null;
 LOG.debug("Searching latest profile schema by application id [{}] ", appId);
 if (isNotBlank(appId)) {
  Criteria criteria = getCriteria().createAlias(APPLICATION_PROPERTY, APPLICATION_ALIAS)
    .add(Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(appId)))
    .addOrder(Order.desc(VERSION_PROPERTY)).setMaxResults(FIRST);
  latestSchema = findOneByCriteria(criteria);
 }
 if (LOG.isTraceEnabled()) {
  LOG.trace("[{}] Search result: {}.", appId, latestSchema);
 } else {
  LOG.debug("[{}] Search result: {}.", appId, latestSchema != null);
 }
 return latestSchema;
}

代码示例来源:origin: kaaproject/kaa

@Override
public LogSchema findLatestLogSchemaByAppId(String applicationId) {
 LOG.debug("Searching latest log schema  by application id [{}]", applicationId);
 LogSchema logSchema = null;
 if (isNotBlank(applicationId)) {
  Criteria criteria = getCriteria();
  criteria.createAlias(APPLICATION_PROPERTY, APPLICATION_ALIAS);
  Criterion criterion = Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(applicationId));
  logSchema = (LogSchema) criteria.add(criterion).addOrder(Order.desc(VERSION_PROPERTY))
    .setMaxResults(FIRST).uniqueResult();
 }
 if (LOG.isTraceEnabled()) {
  LOG.trace("[{}] Search result: {}.", applicationId, logSchema);
 } else {
  LOG.debug("[{}] Search result: {}.", applicationId, logSchema != null);
 }
 return logSchema;
}

代码示例来源:origin: kaaproject/kaa

@Override
public ConfigurationSchema findLatestByApplicationId(String appId) {
 ConfigurationSchema latestSchema = null;
 LOG.debug("Searching latest configuration schema by application id [{}] ", appId);
 if (isNotBlank(appId)) {
  Criteria criteria = getCriteria().createAlias(APPLICATION_PROPERTY, APPLICATION_ALIAS)
    .add(Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(appId)))
    .addOrder(Order.desc(VERSION_PROPERTY))
    .setMaxResults(FIRST);
  latestSchema = findOneByCriteria(criteria);
 }
 if (LOG.isTraceEnabled()) {
  LOG.trace("[{}] Search result: {}.", appId, latestSchema);
 } else {
  LOG.debug("[{}] Search result: {}.", appId, latestSchema != null);
 }
 return latestSchema;
}

代码示例来源:origin: org.springframework/spring-orm

@Override
@SuppressWarnings({"unchecked", "deprecation"})
public <T> List<T> findByExample(
    @Nullable final String entityName, final T exampleEntity, final int firstResult, final int maxResults)
    throws DataAccessException {
  Assert.notNull(exampleEntity, "Example entity must not be null");
  return nonNull(executeWithNativeSession((HibernateCallback<List<T>>) session -> {
    Criteria executableCriteria = (entityName != null ?
        session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass()));
    executableCriteria.add(Example.create(exampleEntity));
    prepareCriteria(executableCriteria);
    if (firstResult >= 0) {
      executableCriteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      executableCriteria.setMaxResults(maxResults);
    }
    return executableCriteria.list();
  }));
}

代码示例来源:origin: kaaproject/kaa

@Override
 public NotificationSchema findLatestNotificationSchemaByAppId(
     String appId, NotificationTypeDto type) {
  LOG.debug("Searching latest notification schema by application id [{}] type [{}]", appId, type);
  NotificationSchema latestSchema = null;
  if (isNotBlank(appId)) {
   Criteria criteria = getCriteria().add(Restrictions.and(
     Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(appId)),
     Restrictions.eq(NOTIFICATION_SCHEMA_TYPE_PROPERTY, type)
   )).addOrder(Order.desc(VERSION_PROPERTY)).setMaxResults(FIRST);
   latestSchema = findOneByCriteria(criteria);
  }
  if (LOG.isTraceEnabled()) {
   LOG.trace("[{},{}] Search result: {}.", appId, type, latestSchema);
  } else {
   LOG.debug("[{},{}] Search result: {}.", appId, type, latestSchema != null);
  }
  return latestSchema;
 }
}

代码示例来源:origin: kaaproject/kaa

@Override
public CtlSchema findLatestByMetaInfoId(String metaInfoId) {
 CtlSchema latestSchema = null;
 LOG.debug("Searching latest version of ctl schema by meta info id [{}]",
   metaInfoId);
 if (isNotBlank(metaInfoId)) {
  Criteria criteria = getCriteria()
    .createAlias(CTL_SCHEMA_META_INFO_PROPERTY, CTL_SCHEMA_META_INFO_ALIAS)
    .add(Restrictions.eq(CTL_SCHEMA_META_INFO_ALIAS_ID, Long.valueOf(metaInfoId)))
    .addOrder(Order.desc(CTL_SCHEMA_VERSION))
    .setMaxResults(FIRST);
  latestSchema = findOneByCriteria(criteria);
 }
 if (LOG.isTraceEnabled()) {
  LOG.trace("[{}] Search result: [{}].", metaInfoId, latestSchema);
 } else {
  LOG.debug("[{}] Search result: [{}].", metaInfoId, latestSchema != null);
 }
 return latestSchema;
}

代码示例来源:origin: kaaproject/kaa

@Override
public ServerProfileSchema findLatestByAppId(String appId) {
 ServerProfileSchema latestSchema = null;
 LOG.debug("Searching latest server profile schema by application id [{}] ", appId);
 if (isNotBlank(appId)) {
  Criteria criteria = getCriteria().createAlias(APPLICATION_PROPERTY, APPLICATION_ALIAS)
    .add(Restrictions.eq(APPLICATION_REFERENCE, getLongId(appId)))
    .addOrder(Order.desc(VERSION_PROPERTY)).setMaxResults(FIRST);
  latestSchema = findOneByCriteria(criteria);
 }
 if (LOG.isTraceEnabled()) {
  LOG.trace("[{}] Search result: {}.", appId, latestSchema);
 } else {
  LOG.debug("[{}] Search result: {}.", appId, latestSchema != null);
 }
 return latestSchema;
}

代码示例来源:origin: kaaproject/kaa

@Override
public Configuration findLatestDeprecated(String schemaId, String groupId) {
 Configuration configuration = null;
 LOG.debug("Searching latest deprecated configurations by configuration schema id [{}] "
     + "and group id [{}] ",
   schemaId, groupId);
 if (isNotBlank(schemaId) && isNotBlank(groupId)) {
  Criteria criteria = getCriteria();
  criteria.createAlias(CONFIGURATION_SCHEMA_PROPERTY, CONFIGURATION_SCHEMA_ALIAS);
  criteria.createAlias(ENDPOINT_GROUP_PROPERTY, ENDPOINT_GROUP_ALIAS);
  criteria.add(Restrictions.and(
    Restrictions.eq(ENDPOINT_GROUP_REFERENCE, Long.valueOf(groupId)),
    Restrictions.eq(CONFIGURATION_SCHEMA_REFERENCE, Long.valueOf(schemaId)),
    Restrictions.eq(STATUS_PROPERTY, UpdateStatus.DEPRECATED)));
  criteria.addOrder(Order.desc(SEQUENCE_NUMBER_PROPERTY)).setMaxResults(FIRST);
  configuration = findOneByCriteria(criteria);
 }
 if (LOG.isTraceEnabled()) {
  LOG.trace("[{},{}] Search result: {}.", schemaId, groupId, configuration);
 } else {
  LOG.debug("[{},{}] Search result: {}.", schemaId, groupId, configuration != null);
 }
 return configuration;
}

代码示例来源:origin: stackoverflow.com

Criteria criteria=session.createCriteria(Student.class);
  criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
  criteria.add(Restrictions.ne("enquiryStatus", ENQUIRY.JOINED));
  criteria.setMaxResults(10);
  criteria.setFirstResult((paginate.getStartIndex()-1)*10);
  List<Student> students = criteria.list();
 criteria.setProjection(null);
 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
 Long resultCount = (Long)criteria.uniqueResult();

代码示例来源:origin: org.springframework/spring-orm

@Override
@SuppressWarnings("unchecked")
public List<?> findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults)
    throws DataAccessException {
  Assert.notNull(criteria, "DetachedCriteria must not be null");
  return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
    Criteria executableCriteria = criteria.getExecutableCriteria(session);
    prepareCriteria(executableCriteria);
    if (firstResult >= 0) {
      executableCriteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      executableCriteria.setMaxResults(maxResults);
    }
    return executableCriteria.list();
  }));
}

代码示例来源:origin: kaaproject/kaa

Restrictions.eq(STATUS_PROPERTY, UpdateStatus.DEPRECATED));
filter = (ProfileFilter) criteria.add(crit).addOrder(Order.desc(SEQUENCE_NUMBER_PROPERTY))
    .setMaxResults(FIRST).uniqueResult();

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

@Test
public void testCriteria() {
  Session session = openSession();
  session.beginTransaction();
  Criteria criteria = session.createCriteria( Door.class );
  criteria.setLockMode( LockMode.PESSIMISTIC_WRITE );
  criteria.setFirstResult( 2 );
  criteria.setMaxResults( 2 );
  @SuppressWarnings("unchecked") List<Door> results = criteria.list();
  assertEquals( 2, results.size() );
  for ( Door door : results ) {
    assertEquals( LockMode.PESSIMISTIC_WRITE, session.getCurrentLockMode( door ) );
  }
  session.getTransaction().commit();
  session.close();
}

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

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout.
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareCriteria(Criteria criteria) {
  if (isCacheQueries()) {
    criteria.setCacheable(true);
    if (getQueryCacheRegion() != null) {
      criteria.setCacheRegion(getQueryCacheRegion());
    }
  }
  if (getFetchSize() > 0) {
    criteria.setFetchSize(getFetchSize());
  }
  if (getMaxResults() > 0) {
    criteria.setMaxResults(getMaxResults());
  }
  ResourceHolderSupport sessionHolder =
      (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory());
  if (sessionHolder != null && sessionHolder.hasTimeout()) {
    criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
  }
}

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

.setMaxResults(3)
.setReadOnly( true )
.list();

代码示例来源:origin: org.springframework/spring-orm

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout.
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareCriteria(Criteria criteria) {
  if (isCacheQueries()) {
    criteria.setCacheable(true);
    if (getQueryCacheRegion() != null) {
      criteria.setCacheRegion(getQueryCacheRegion());
    }
  }
  if (getFetchSize() > 0) {
    criteria.setFetchSize(getFetchSize());
  }
  if (getMaxResults() > 0) {
    criteria.setMaxResults(getMaxResults());
  }
  ResourceHolderSupport sessionHolder =
      (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory());
  if (sessionHolder != null && sessionHolder.hasTimeout()) {
    criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
  }
}

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

.setFirstResult( 1 ).setMaxResults( 3 ).list();

相关文章