hibernate中limit实现方法

转:http://zisetoufa.iteye.com/blog/1753029

折腾了半天,原来是hibernate不支持limit,还有有能实现的方法: 

Java代码   收藏代码
  1. public List<IpSource> getIpList(final int count) {  
  2.         final String sql = "from IpSource order by crtTm desc";  
  3.         return this .getHibernateTemplate().executeFind( new  HibernateCallback() {     
  4.              public  Object doInHibernate(Session session)     
  5.              throws  HibernateException, SQLException {     
  6.                  return  session.createQuery(sql).setMaxResults(count).list();     
  7.               }     
  8.         });    
  9.     }  



Java代码   收藏代码
  1.  try{    
  2. 10.    
  3. 11.           final String hql = "form KnowledgeExpertArticle as model where model.'"+keyName+"' like '%"+value+"%' order by model.expertArticleId DESC";    
  4. 12.    
  5. 13.           log.info(hql);    
  6. 14.    
  7. 15.           List resultList = getHibernateTemplate().executeFind(    
  8. 16.    
  9. 17.              new HibernateCallback() {    
  10. 18.    
  11. 19.                  public Object doInHibernate(Session arg0)    
  12. 20.    
  13. 21.                         throws HibernateException, SQLException {    
  14. 22.    
  15. 23.                     Query query = arg0.createQuery(hql);    
  16. 24.    
  17. 25.                     query.setFirstResult(start);    
  18. 26.    
  19. 27.                     query.setMaxResults(limit);    
  20. 28.    
  21. 29.                     return query.list();    
  22. 30.    
  23. 31.                  }    
  24. 32.    
  25. 33.     
  26. 34.    
  27. 35.              });    
  28. 36.    
  29. 37.           return resultList;    
  30. 38.    
  31. 39.               
  32. 40.    
  33. 41.       }catch(RuntimeException re){    
  34. 42.    
  35. 43.           throw re;    
  36. 44.    
  37. 45.       }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值