论坛首页 Java企业应用论坛

参考中的例子关于HQL语句的问题

浏览 10543 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-09-29  
我在Hibernate2的参考Blog例子中有这样一段查询语句:
public List listAllBlogNamesAndItemCounts(int max) throws HibernateException {
   Session session = _sessions.openSession();
   Transaction tx = null;
   List result = null;
   try {
   tx = session.beginTransaction();
   Query q = session.createQuery("select blog.id, blog.name, count  (blogItem) " +" from Blog as blog " +" left outer join blog.items as blogItem  " +"  group by blog.name, blog.id " +" order by  max(blogItem.datetime)

");

     q.setMaxResults(max);//??
     result = q.list();
     tx.commit();
   }
   catch (HibernateException he) {
     if (tx!=null) tx.rollback();
     throw he;
   }
   finally {
     session.close();
   }
   return result;
}
我运行时报错:

Hibernate: select blog0_.BLOG_ID as x0_0_, blog0_.NAME as x1_0_, count(items1_.B
LOG_ITEM_ID) as x2_0_ from BLOGS blog0_ left outer join BLOG_ITEMS items1_ on bl
og0_.BLOG_ID=items1_.BLOG_ID group by  blog0_.NAME , blog0_.BLOG_ID order by  ma
x(items1_.DATE_TIME) limit ?, ?
2003-9-29 13:08:44 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 170, SQLState: HY000
2003-9-29 13:08:44 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
严重: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]第 1 行: 'limit' 附
近有语法错误。
2003-9-29 13:08:44 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 16945, SQLState: HY000
2003-9-29 13:08:44 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
严重: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]游标未声明。
2003-9-29 13:08:44 net.sf.hibernate.JDBCException <init>
严重: Could not execute query
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]第
1 行: 'limit' 附近有语法错误。
        at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source
)
        at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
        at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown
Source)
        at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown
Source)
        at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unkn
own Source)
        at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Sour
ce)
        at com.microsoft.jdbc.sqlserver.tds.TDSCursorRequest.openCursor(Unknown
Source)
        at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown S
ource)
        at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
        at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown So
urce)
        at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown So
urce)
        at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:71)
        at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:551)
        at net.sf.hibernate.loader.Loader.doFind(Loader.java:140)
        at net.sf.hibernate.loader.Loader.find(Loader.java:620)
        at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:928)
        at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1343)
        at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:76)
        at eg.BlogMain.listAllBlogNamesAndItemCounts(BlogMain.java:136)
        at eg.BlogMain.main(BlogMain.java:211)
===============================================
如果我把     q.setMaxResults(max);一句删除却能运行!此函数是什么意思?我该如何设置?
   发表时间:2003-10-01  
你用的是sql server数据库,在hibernate.properties中却设置成了
MySqlDialect!!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics