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

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

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

Query.setMaxResults介绍

[英]Set the maximum number of rows to retrieve. If not set, there is no limit to the number of rows retrieved.
[中]设置要检索的最大行数。如果未设置,则检索的行数没有限制。

代码示例

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

  1. Modification findLatestModification(final MaterialInstance expandedInstance) {
  2. Modifications modifications = cachedModifications(expandedInstance);
  3. if (modifications != null && !modifications.isEmpty()) {
  4. return modifications.get(0);
  5. }
  6. String cacheKey = latestMaterialModificationsKey(expandedInstance);
  7. synchronized (cacheKey) {
  8. Modification modification = (Modification) getHibernateTemplate().execute((HibernateCallback) session -> {
  9. Query query = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC");
  10. query.setMaxResults(1);
  11. query.setLong(0, expandedInstance.getId());
  12. return query.uniqueResult();
  13. });
  14. goCache.put(cacheKey, new Modifications(modification));
  15. return modification;
  16. }
  17. }

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

  1. @Test
  2. public void testPSCache() throws Exception {
  3. Session s = openSession();
  4. Transaction txn = s.beginTransaction();
  5. for ( int i=0; i<10; i++ ) s.save( new Foo() );
  6. Query q = s.createQuery("from Foo");
  7. q.setMaxResults(2);
  8. q.setFirstResult(5);
  9. assertTrue( q.list().size()==2 );
  10. q = s.createQuery("from Foo");
  11. assertTrue( q.list().size()==10 );
  12. assertTrue( q.list().size()==10 );
  13. q.setMaxResults(3);
  14. q.setFirstResult(3);
  15. assertTrue( q.list().size()==3 );
  16. q = s.createQuery("from Foo");
  17. assertTrue( q.list().size()==10 );
  18. txn.commit();
  19. s.close();
  20. s = openSession();
  21. txn = s.beginTransaction();
  22. q = s.createQuery("from Foo");
  23. assertTrue( q.list().size()==10 );
  24. q.setMaxResults(5);
  25. assertTrue( q.list().size()==5 );
  26. doDelete( s, "from Foo" );
  27. txn.commit();
  28. s.close();
  29. }

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

  1. s.save(simple );
  2. s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list();
  3. s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list();
  4. s.createQuery( "from Simple s where repeat( lower(s.name), (3 + (1-1)) / 2) = 'foofoofoo'" ).list();
  5. Query q = s.createQuery("from Simple s");
  6. q.setMaxResults( 10 );
  7. assertTrue( q.list().size()==3 );
  8. q = s.createQuery("from Simple s");
  9. q.setMaxResults( 1 );
  10. assertTrue( q.list().size()==1 );
  11. q = s.createQuery("from Simple s");
  12. assertTrue( q.list().size() == 3 );
  13. q = s.createQuery("from Simple s where s.name = ?");
  14. q.setString( 0, "Simple 1" );
  15. assertTrue( q.list().size()==1 );
  16. q = s.createQuery("from Simple s where s.name = ? and upper(s.name) = ?");
  17. q.setString(1, "SIMPLE 1");
  18. q.setString( 0, "Simple 1" );
  19. q.setFirstResult(0);
  20. assertTrue( q.iterate().hasNext() );
  21. q = s.createQuery("select s.id from Simple s");
  22. q.setFirstResult(1);
  23. q.setMaxResults( 2 );
  24. iter = q.iterate();
  25. int i=0;

代码示例来源:origin: org.ow2.bonita/bonita-pvm

  1. public ClientExecution findExecutionById(String executionId) {
  2. // query definition can be found at the bottom of resource
  3. // org/ow2/bonita/pvm/hibernate.execution.hbm.xml
  4. Query query = session.getNamedQuery("findExecutionById");
  5. query.setString("id", executionId);
  6. query.setMaxResults(1);
  7. return (ClientExecution) query.uniqueResult();
  8. }

代码示例来源:origin: org.ow2.bonita/bonita-server

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public List<InternalProcessInstance> getParentProcessInstancesWithInvolvedUser(final String userId,
  4. final int fromIndex, final int pageSize) {
  5. final Query query = getSession().getNamedQuery("getParentProcessInstancesWithInvolvedUser");
  6. query.setFirstResult(fromIndex);
  7. query.setMaxResults(pageSize);
  8. query.setString("userId", userId);
  9. return formatList(query.list());
  10. }

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

  1. @Override
  2. public Calendar getMostRecentQueueScanTime(Integer channelId) {
  3. return (Calendar) sessionFactory
  4. .getCurrentSession()
  5. .createQuery("select scanDate from JobStatus status " +
  6. "where applicationChannel = :channelId and hasStartedProcessing is false " +
  7. "order by scanDate desc")
  8. .setInteger("channelId", channelId).setMaxResults(1).uniqueResult();
  9. }

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

  1. /**
  2. * @see org.openmrs.hl7.db.HL7DAO#getNextHL7InQueue()
  3. */
  4. @Override
  5. public HL7InQueue getNextHL7InQueue() throws DAOException {
  6. Query query = sessionFactory.getCurrentSession().createQuery(
  7. "from HL7InQueue as hiq where hiq.messageState = ? order by HL7InQueueId").setParameter(0,
  8. HL7Constants.HL7_STATUS_PENDING, StandardBasicTypes.INTEGER).setMaxResults(1);
  9. if (query == null) {
  10. return null;
  11. }
  12. return (HL7InQueue) query.uniqueResult();
  13. }

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

  1. public Modifications getModificationsFor(final MaterialInstance materialInstance, final Pagination pagination) {
  2. String key = materialModificationsWithPaginationKey(materialInstance);
  3. String subKey = materialModificationsWithPaginationSubKey(pagination);
  4. Modifications modifications = (Modifications) goCache.get(key, subKey);
  5. if (modifications == null) {
  6. synchronized (key) {
  7. modifications = (Modifications) goCache.get(key, subKey);
  8. if (modifications == null) {
  9. List<Modification> modificationsList = (List<Modification>) getHibernateTemplate().execute((HibernateCallback) session -> {
  10. Query q = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC");
  11. q.setFirstResult(pagination.getOffset());
  12. q.setMaxResults(pagination.getPageSize());
  13. q.setLong(0, materialInstance.getId());
  14. return q.list();
  15. });
  16. if (!modificationsList.isEmpty()) {
  17. modifications = new Modifications(modificationsList);
  18. goCache.put(key, subKey, modifications);
  19. }
  20. }
  21. }
  22. }
  23. return modifications;
  24. }

代码示例来源:origin: org.jbpm/pvm

  1. public ClientExecution findExecutionById(String executionId) {
  2. // query definition can be found at the bottom of resource org/jbpm/pvm/hibernate.execution.hbm.xml
  3. Query query = session.getNamedQuery("findExecutionById");
  4. query.setString("id", executionId);
  5. query.setMaxResults(1);
  6. return (ClientExecution) query.uniqueResult();
  7. }

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

  1. s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list();
  2. s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list();
  3. s.createQuery( "from Simple s where repeat( lower(s.name), 3 + (1-1) / 2) = 'foofoofoo'" ).list();
  4. Query q = s.createQuery("from Simple s");
  5. q.setMaxResults(10);
  6. assertTrue( q.list().size()==3 );
  7. q = s.createQuery("from Simple s");
  8. q.setMaxResults(1);
  9. assertTrue( q.list().size()==1 );
  10. q = s.createQuery("from Simple s");
  11. assertTrue( q.list().size()==3 );
  12. q = s.createQuery("from Simple s where s.name = ?");
  13. q.setString(0, "Simple 1");
  14. assertTrue( q.list().size()==1 );
  15. q = s.createQuery("from Simple s where s.name = ? and upper(s.name) = ?");
  16. q.setString(1, "SIMPLE 1");
  17. q.setString(0, "Simple 1");
  18. q.setFirstResult(0);
  19. assertTrue( q.iterate().hasNext() );
  20. q = s.createQuery("select s.id from Simple s");
  21. q.setFirstResult(1);
  22. q.setMaxResults(2);
  23. iter = q.iterate();
  24. int i=0;

代码示例来源:origin: org.ow2.bonita/bonita-server

  1. @SuppressWarnings("unchecked")
  2. @Override
  3. public List<Document> getDocuments(final ProcessInstanceUUID instanceUUID, final int fromResult, final int maxResults) {
  4. final Query query = getSession().getNamedQuery("getDocumentsOfProcessInstance");
  5. query.setString("processInstanceUUID", instanceUUID.getValue());
  6. query.setFirstResult(fromResult);
  7. query.setMaxResults(maxResults);
  8. return query.list();
  9. }

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

  1. @Override
  2. public WafRule retrieveByVulnerabilityAndWafAndDirective(
  3. Vulnerability vuln, Waf waf, WafRuleDirective directive) {
  4. return (WafRule) sessionFactory
  5. .getCurrentSession()
  6. .createQuery( "from WafRule wafRule where wafRule.vulnerability = :vulnId " +
  7. "and wafRule.waf = :wafId and wafRule.wafRuleDirective = :directiveId")
  8. .setInteger("vulnId", vuln.getId())
  9. .setInteger("wafId", waf.getId())
  10. .setInteger("directiveId", directive.getId())
  11. .setMaxResults(1).uniqueResult();
  12. }

代码示例来源:origin: OpenNMS/opennms

  1. public OnmsNode doInHibernate(Session session) throws HibernateException, SQLException {
  2. Integer nodeId = (Integer)session.createQuery(query2).setMaxResults(1).uniqueResult();
  3. return getNode(nodeId, session);
  4. }
  5. });

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

  1. @Test
  2. public void testHql() {
  3. Session session = openSession();
  4. session.beginTransaction();
  5. Query qry = session.createQuery( "from Door" );
  6. qry.getLockOptions().setLockMode( LockMode.PESSIMISTIC_WRITE );
  7. qry.setFirstResult( 2 );
  8. qry.setMaxResults( 2 );
  9. @SuppressWarnings("unchecked") List<Door> results = qry.list();
  10. assertEquals( 2, results.size() );
  11. for ( Door door : results ) {
  12. assertEquals( LockMode.PESSIMISTIC_WRITE, session.getCurrentLockMode( door ) );
  13. }
  14. session.getTransaction().commit();
  15. session.close();
  16. }

代码示例来源:origin: org.jbpm/pvm

  1. public ClientProcessDefinition findLatestProcessDefinitionByName(String name) {
  2. // query definition can be found at the bottom of resource org/jbpm/pvm/hibernate.definition.hbm.xml
  3. Query query = session.getNamedQuery("findProcessDefinitionsByName");
  4. query.setString("name", name);
  5. query.setMaxResults(1);
  6. ClientProcessDefinition processDefinition = (ClientProcessDefinition) query.uniqueResult();
  7. return processDefinition;
  8. }

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

  1. public LogUsageStatsBean findLatestUsageStatParamValue(String param_key) {
  2. // logger.debug("UsageStatsServiceDAO -> findLatestUsageStatParamValue");
  3. String query =
  4. "from " + getDomainClassName() + " usageStatParams where param_key = :param_key order by update_timestamp desc";
  5. List<LogUsageStatsBean> logUsageStatsBeanLst = new ArrayList<LogUsageStatsBean>();
  6. LogUsageStatsBean logUsageStatsBeanRet = new LogUsageStatsBean();
  7. org.hibernate.Query q = getCurrentSession().createQuery(query);
  8. q.setString("param_key", param_key);
  9. q.setMaxResults(1);
  10. logUsageStatsBeanLst = q.list();
  11. if ((null != logUsageStatsBeanLst) && (logUsageStatsBeanLst.size() != 0)) {
  12. logUsageStatsBeanRet = logUsageStatsBeanLst.get(0);
  13. }
  14. return logUsageStatsBeanRet;
  15. }

代码示例来源:origin: org.ow2.bonita/bonita-server

  1. @SuppressWarnings("unchecked")
  2. @Override
  3. public List<DocumentDescriptor> getDocumentDescriptors(final ProcessDefinitionUUID processDefinitionUUID,
  4. final int fromIndex, final int maxResults) {
  5. final Query query = getSession().getNamedQuery("getDocumentDescriptorsOfProcessDefinition");
  6. query.setString("processDefinitionUUID", processDefinitionUUID.getValue());
  7. query.setFirstResult(fromIndex);
  8. query.setMaxResults(maxResults);
  9. return query.list();
  10. }

代码示例来源:origin: riotfamily/riot

  1. private boolean isSetupRequired(Session session) {
  2. if (condition == null) {
  3. return true;
  4. }
  5. Query query = session.createQuery(condition).setMaxResults(1);
  6. Object test = query.uniqueResult();
  7. if (test instanceof Number) {
  8. return ((Number) test).intValue() == 0;
  9. }
  10. if (test instanceof Boolean) {
  11. return ((Boolean) test).booleanValue();
  12. }
  13. return test == null;
  14. }

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

  1. @Test
  2. public void testMaxResults() {
  3. doInHibernate(
  4. this::sessionFactory,
  5. session -> {
  6. Query query = session.createQuery( "from Employee" );
  7. // not initialized yet
  8. assertNull( query.getHibernateMaxResults() );
  9. // values <= 0 are considered uninitialized;
  10. assertNull( query.setHibernateMaxResults( -1 ).getHibernateMaxResults() );
  11. assertNull( query.setHibernateMaxResults( 0 ).getHibernateMaxResults() );
  12. assertEquals( Integer.valueOf( 1 ), query.setHibernateMaxResults( 1 ).getHibernateMaxResults() );
  13. assertEquals( Integer.valueOf( 0 ), query.setMaxResults( 0 ).getHibernateMaxResults() );
  14. assertEquals( Integer.valueOf( 2 ), query.setMaxResults( 2 ).getHibernateMaxResults() );
  15. }
  16. );
  17. }

代码示例来源:origin: org.jbpm/pvm

  1. public ClientProcessDefinition findProcessDefinitionById(String processDefinitionId) {
  2. // query definition can be found at the bottom of resource org/jbpm/pvm/hibernate.definition.hbm.xml
  3. Query query = session.getNamedQuery("findProcessDefinitionsById");
  4. query.setString("id", processDefinitionId);
  5. query.setMaxResults(1);
  6. ClientProcessDefinition processDefinition = (ClientProcessDefinition) query.uniqueResult();
  7. return processDefinition;
  8. }

相关文章