posts - 4, comments - 10, trackbacks - 0, articles - 2

请教一个hibernate对mysql的分页问题

Posted on 2007-04-23 15:11 samfree 阅读(2199) 评论(5)  编辑  收藏

我在用hibernate对mysql进行分页的方法
/**
  * 函数说明:获得所有的信息
  * 参数说明:
  * 返回值:信息的集合
  */
 public List getProducts(final int pageSize, final int startRow) throws HibernateException { 
     return this.getHibernateTemplate().executeFind(new HibernateCallback(){

         public Object doInHibernate(Session session) throws HibernateException, SQLException {
        
             Query query=session.createQuery("FROM Products as p");
             query.setMaxResults(pageSize);
             query.setFirstResult(startRow);    
    
             List list = query.list();
       
             return list;
         }
        });

 }

运行时怎么老给出异常阿?请帮帮我看看好吗?
org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLGrammarException: could not execute query
java.sql.SQLException: Syntax error or access violation,  message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '40 products0_.game_id as game1_0_, products0_.game_name_en as game2_0_, products' at line 1"

Feedback

# re: 请教一个hibernate对mysql的分页问题  回复  更多评论   

2007-04-26 15:57 by ddd
没看到你的HQL里面有关于limited的记述,

你就直接set 2个变量进去?

你是否该有2个?的记述,然后才set?

# re: 请教一个hibernate对mysql的分页问题  回复  更多评论   

2007-04-26 16:08 by ddd
我孤陋寡闻了, 抱歉。。

请其他兄弟回答。。。

# re: 请教一个hibernate对mysql的分页问题  回复  更多评论   

2007-04-30 17:00 by BeanSoft
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '40 products0_.game_id as game1_0_, products0_.game_name_en as game2_0_, products' at line 1"

mysql 不支持这种语法格式的 SQL(Hibernate自动生成的), 检查一下 Mysql 版本是不是 Hibernate 所支持的版本, 一般来说可能需要 4.1 或者 5.0.

# re: 请教一个hibernate对mysql的分页问题  回复  更多评论   

2010-06-03 19:11 by 最厚的支付宝员工
hibernate 发出的分页查询sql为
select top ? .....
这个是mysql不支持的,所以你不能用这样子的分页
可以用
certeria.list.subList(index,endex);

# re: 请教一个hibernate对mysql的分页问题[未登录]  回复  更多评论   

2014-11-04 14:11 by aa
这个是应mysql不支持top关键字,所以将查询语句改成select * from table limit 0,100 这样

只有注册用户登录后才能发表评论。


网站导航: