HQL查询问题

dreamporsche 2008-05-11 01:59:51
Hibernate对数据库操作的程序如下:
public List getMatchProcessOrders(String keyword) throws HibernateException {
//创建一个查询语句,按关键字查询
String strSql = "from Order where tag=1 and orderno like '%" + keyword + "%' ";
List list=getList(strSql);
return list; //返回数据集
}
运行后的提示:
org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.doList(Loader.java:2148)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
.....................
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'order order0_ where order0_.tag=1 and (order0_.orderno like '%%')' at line 1
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
我将程序中的查询语句改为:
String strSql = "from Order as o where o.tag=1 and (o.orderno like '%"+keyword+"%')";
运行后提示:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'order order0_ where order0_.tag=1 and (order0_.orderno like '%%')' at line 1
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
..........................
不知道什么原因.
...全文
69 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Shine_Panda 2008-05-11
  • 打赏
  • 举报
回复
String strSql = "from Order as o where o.tag=1 and o.orderno like '%"+keyword+"%'";
试试。。。
gongyali2005 2008-05-11
  • 打赏
  • 举报
回复
String hql="from Order where tag=? and orderno like ?"
Query q=session.create..........
q.setInteger(0,1);
q.setString(1,"%"+keyword+"%");

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧