转:http://zisetoufa.iteye.com/blog/1753029
折腾了半天,原来是hibernate不支持limit,还有有能实现的方法:
- public List<IpSource> getIpList(final int count) {
- final String sql = "from IpSource order by crtTm desc";
- return this .getHibernateTemplate().executeFind( new HibernateCallback() {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException {
- return session.createQuery(sql).setMaxResults(count).list();
- }
- });
- }
- try{
- 10.
- 11. final String hql = "form KnowledgeExpertArticle as model where model.'"+keyName+"' like '%"+value+"%' order by model.expertArticleId DESC";
- 12.
- 13. log.info(hql);
- 14.
- 15. List resultList = getHibernateTemplate().executeFind(
- 16.
- 17. new HibernateCallback() {
- 18.
- 19. public Object doInHibernate(Session arg0)
- 20.
- 21. throws HibernateException, SQLException {
- 22.
- 23. Query query = arg0.createQuery(hql);
- 24.
- 25. query.setFirstResult(start);
- 26.
- 27. query.setMaxResults(limit);
- 28.
- 29. return query.list();
- 30.
- 31. }
- 32.
- 33.
- 34.
- 35. });
- 36.
- 37. return resultList;
- 38.
- 39.
- 40.
- 41. }catch(RuntimeException re){
- 42.
- 43. throw re;
- 44.
- 45. }