在hibernate查询Mysql中的limit问题
public List findArticlesByBlogID(long blogID) {
List list = this.getHibernateTemplate().find(BY_BLOGID,new Long(blogID));
return list;
}
String BY_BLOGID = “from Article where blogid = ? order by posttime desc limit 3”;
为什么没有按预期返回3条记录,而是返回了全部记录。
1 楼 hgq0011 2007-05-13
hql不支持limit
2 楼 movingboy 2007-05-13
使用Hibernate其实不需要针对特定的数据库写特定语法的SQL语句,参考如下方法:
public List findArticlesByBlogID(final long blogID, final int maxCount) { final String BY_BLOGID = "from Article where blogId = ?"; return this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { return session.createQuery(BY_BLOGID).setParameter(0, new Long(blogID)).setMaxResults(maxCount).list(); } }); }
3 楼 卒子99 2007-05-13
Hibernate会是使用特定的Dialet执行分页查询,如MySQL的limit。
如果DBMS不支持这样的查询,就接着检查是否支持Scroable,如果支持主,则直接定位游标的位置,最近再在PS中设置抓取的记录数。
如果不支持Scroable,那就惨了,只有一步步rs.next()了