代码之家  ›  专栏  ›  技术社区  ›  Richie

Hibernate支持MySQL中的limit语句吗?

  •  9
  • Richie  · 技术社区  · 15 年前

    我正在研究一个使用Java、MySQL、Struts 2 MVC和Hibernate的项目。我尝试在HQL查询中使用limit语句,但它不能正常工作。

    Select t from table1 t where t.column1 = :someVal limit 0,5
    

    编辑:我将其用作namedquery,并使用jpa模板调用此namedquery

    这在MySQL中工作正常,但是当我作为HQL查询运行它时,它返回所有记录,而不考虑限制语句。有人面临同样的问题吗??感谢您的帮助!你说什么?

    问候,RDJ

    4 回复  |  直到 9 年前
        1
  •  5
  •   Mobs    15 年前

    使用休眠功能进行分页!setFirstResult(),setMaxResults())

    http://www.javalobby.org/java/forums/t63849.html

        2
  •  1
  •   Bozho    15 年前

    使用 Query.setMaxResults(..)

        3
  •  0
  •   normalUser    9 年前

    我想, 如果我们使用标准,我们可以指定

    criteria c=session.createReciteria(emp.class);
    c.设置第一个结果(0);
    c.设置最大结果(5);
    list=c.list();

    这里,0和5作为0,5的极限

    来源: http://www.javatpoint.com/hcql

        4
  •  -1
  •   Lalit Nandan    12 年前
        List<Employee> dataList = null;
        Query hQuery = null;
    
        try {
            // Getting Session
    
            hSession = HibernateSessionFactory.getSession();
            hTransaction = hSession.beginTransaction();
            hQuery = hSession.getNamedQuery(query);
    
                        Iterator<Object> hIterator = paramMap.entrySet().iterator();
    
                        while (hIterator.hasNext()) {
    
         Map.Entry<String, Object> paramPair = (Map.Entry<String,Object>) hIterator.next();    
    
     hQuery.setParameter(paramPair.getKey(), paramPair.getValue());
    
            }
    
            hQuery.setFirstResult(1);
            hQuery.setMaxResults(100);
    
            System.out.println(hQuery);
            dataList = hQuery.list();
    
            hTransaction.commit();
    
        } catch (Exception e) {
            hTransaction.rollback();
    
    
        } finally {
            try {
                hSession.flush();
                HibernateSessionFactory.closeSession();
            } catch (Exception hExp) {}