Does Hibernate support the limit statement in MySql?
I am working on a project which uses Java,MySql,Struts2 MVC and Hibernate. I tried using limit statement in hql query but its not working properly.
Select t from ta开发者_JAVA百科ble1 t where t.column1 = :someVal limit 0,5
EDIT: I am using this as a namedQuery and calling this namedQuery using JPA Template
This works correctly in MySql but when I ran this as a hql query this returns all records without regard to limit statement. Has anyone faced the same problem?? Any help appreciated!!
Regards, RDJ
Use hibernates functions for paging! setFirstResult(), setMaxResults()
http://www.javalobby.org/java/forums/t63849.html
use Query.setMaxResults(..)
I think, If we use Criteria we can specify
Crietria c=session.createCriteria(Emp.class);
c.setFirstResult(0);
c.setMaxResult(5);
List list=c.list();
Here 0 and 5 works as limit 0,5
Source : http://www.javatpoint.com/hcql
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) {}
精彩评论