用hibernate分页异常报 com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误。 5
publicList<Goods>findByPage(intpageSize,intpageNo){List<Goods>list=null;try{getSessio...
public List<Goods> findByPage(int pageSize,int pageNo){
List<Goods> list = null;
try{
getSession().beginTransaction();
String hql = "from Goods";
Query query = getSession().createQuery(hql);
int showPage = (pageNo-1)*pageSize; query.setFirstResult(showPage); query.setMaxResults(pageSize);
list = query.list();
getSession().beginTransaction().commit();
}catch(Exception e){
if(getSession().beginTransaction() != null){
getSession().beginTransaction().rollback();
}
e.printStackTrace();
}
return list;
}
分页代码如上:测试代码如下:
GoodsDAO dao = new GoodsDAO();
List<Goods> goods = dao.findByPage(2, 1);
for(Goods g:goods){
System.out.println(g.getGoodsName());
}
测试数据足够:
但就是一运行就报如下异常:
Hibernate: select top ? goods0_.goodsID as goodsID0_, goods0_.goodsName as goodsName0_, goods0_.goodsPrice as goodsPrice0_, goods0_.goodsStockSum as goodsSto4_0_, goods0_.goodsJoinDate as goodsJoi5_0_ from goods goods0_
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2297)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.orders.dao.GoodsDAO.findByPage(GoodsDAO.java:123)
at text.main(text.java:24)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteQuery(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1849)
at org.hibernate.loader.Loader.doQuery(Loader.java:718)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
... 9 more
Exception in thread "main" java.lang.NullPointerException
at text.main(text.java:25)
代码一再检察应没问题;不知道是不是少了什么包!
另:只要不设置 query.setMaxResults(pageSize);(页面显示条数)
这个就没有异常!
按它报的第二个异常:com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误。
应是指hibernate生成的sql语句是含TOP的预编译在MSSQL是不支持的....难道是因数据库的原因?但为什么别人也是这样用却没问题........
请高手支招啊....急 展开
List<Goods> list = null;
try{
getSession().beginTransaction();
String hql = "from Goods";
Query query = getSession().createQuery(hql);
int showPage = (pageNo-1)*pageSize; query.setFirstResult(showPage); query.setMaxResults(pageSize);
list = query.list();
getSession().beginTransaction().commit();
}catch(Exception e){
if(getSession().beginTransaction() != null){
getSession().beginTransaction().rollback();
}
e.printStackTrace();
}
return list;
}
分页代码如上:测试代码如下:
GoodsDAO dao = new GoodsDAO();
List<Goods> goods = dao.findByPage(2, 1);
for(Goods g:goods){
System.out.println(g.getGoodsName());
}
测试数据足够:
但就是一运行就报如下异常:
Hibernate: select top ? goods0_.goodsID as goodsID0_, goods0_.goodsName as goodsName0_, goods0_.goodsPrice as goodsPrice0_, goods0_.goodsStockSum as goodsSto4_0_, goods0_.goodsJoinDate as goodsJoi5_0_ from goods goods0_
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2297)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.orders.dao.GoodsDAO.findByPage(GoodsDAO.java:123)
at text.main(text.java:24)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteQuery(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1849)
at org.hibernate.loader.Loader.doQuery(Loader.java:718)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
... 9 more
Exception in thread "main" java.lang.NullPointerException
at text.main(text.java:25)
代码一再检察应没问题;不知道是不是少了什么包!
另:只要不设置 query.setMaxResults(pageSize);(页面显示条数)
这个就没有异常!
按它报的第二个异常:com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误。
应是指hibernate生成的sql语句是含TOP的预编译在MSSQL是不支持的....难道是因数据库的原因?但为什么别人也是这样用却没问题........
请高手支招啊....急 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询