新人求救!spring全注解写法中hql语句错误

guoyilin1 2016-09-27 08:24:11
说是数据库语句错误,但是实体已经查出来了啊,很费解,新人求救啊!
语句如下:from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"'
错误信息如下:Struts Problem Report

Struts has detected an unhandled exception:

Messages:
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 'from Giving where die=0 and gid='1'' at line 1
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 'from Giving where die=0 and gid='1'' at line 1
File: org/hibernate/exception/internal/SQLExceptionTypeDelegate.java
Line number: 82
Stacktraces

org.hibernate.exception.SQLGrammarException: 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 'from Giving where die=0 and gid='1'' at line 1
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
$Proxy38.executeQuery(Unknown Source)
org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
org.hibernate.loader.Loader.doQuery(Loader.java:829)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
org.hibernate.loader.Loader.doList(Loader.java:2438)
org.hibernate.loader.Loader.doList(Loader.java:2424)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2254)
org.hibernate.loader.Loader.list(Loader.java:2249)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:331)
org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1784)
org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:229)
org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)
com.guoyilin.dao.daoImpl.GivingDaoImpl.findByGivingId(GivingDaoImpl.java:46)
com.guoyilin.service.serviceImpl.GivingServiceImpl.findByGivingId(GivingServiceImpl.java:47)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
...全文
620 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoyilin1 2016-09-29
  • 打赏
  • 举报
回复
引用 12 楼 qq_26508409 的回复:
topUp.getGiving().getGid()这个值不会带引号吧,看看最终的hql什么样子
HQL就是这个啊:from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"' 但是报错之后,给我打印出来的不是这句,hql语句执行出来了,list里面都有值,然后就错了
guoyilin1 2016-09-29
  • 打赏
  • 举报
回复
引用 13 楼 xiakepan 的回复:
[quote=引用 11 楼 guoyilin1 的回复:] [quote=引用 10 楼 xiakepan 的回复:] and gid='1'',这sql有问题,好像前面单引号后面双引号啊,是不是这个导致的错误。
不是的,后面是两个单引号,一个是引字段,一个是引报错的hql语句的,仔细看,前面还有一个单引号的,而且引号问题我也试过好多次了,没有效果[/quote] 但是打印的日志里面明明是1左面单引号,右面双引号。[/quote] 修改完引号之后,错误信息是这样的了 Struts Problem Report Struts has detected an unhandled exception: Messages: 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 'from Giving where die=0 and gid='1'' at line 1 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 'from Giving where die=0 and gid='1'' at line 1
番茄鲨鱼面 2016-09-29
  • 打赏
  • 举报
回复
引用 11 楼 guoyilin1 的回复:
[quote=引用 10 楼 xiakepan 的回复:]
and gid='1'',这sql有问题,好像前面单引号后面双引号啊,是不是这个导致的错误。

不是的,后面是两个单引号,一个是引字段,一个是引报错的hql语句的,仔细看,前面还有一个单引号的,而且引号问题我也试过好多次了,没有效果[/quote]

但是打印的日志里面明明是1左面单引号,右面双引号。
nikyotensai 2016-09-29
  • 打赏
  • 举报
回复
topUp.getGiving().getGid()这个值不会带引号吧,看看最终的hql什么样子
guoyilin1 2016-09-29
  • 打赏
  • 举报
回复
引用 10 楼 xiakepan 的回复:
and gid='1'',这sql有问题,好像前面单引号后面双引号啊,是不是这个导致的错误。
不是的,后面是两个单引号,一个是引字段,一个是引报错的hql语句的,仔细看,前面还有一个单引号的,而且引号问题我也试过好多次了,没有效果
番茄鲨鱼面 2016-09-29
  • 打赏
  • 举报
回复
and gid='1'',这sql有问题,好像前面单引号后面双引号啊,是不是这个导致的错误。
jinpengaigo929 2016-09-29
  • 打赏
  • 举报
回复
params.put("pickingCenter", Long.valueOf(params.get("pickingCenter").toString())); jpql += "and bo.garage.pickingCenter.id =:pickingCenter\n"; 用这种方式
guoyilin1 2016-09-29
  • 打赏
  • 举报
回复
引用 16 楼 qq_26508409 的回复:
[quote=引用 15 楼 guoyilin1 的回复:] [quote=引用 12 楼 qq_26508409 的回复:] topUp.getGiving().getGid()这个值不会带引号吧,看看最终的hql什么样子
HQL就是这个啊:from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"' 但是报错之后,给我打印出来的不是这句,hql语句执行出来了,list里面都有值,然后就错了[/quote] 你是不是在逗我,问你hql到Query query = sessionFactory.getCurrentSession().createQuery(hql);这句的时候值是什么? 你说就是这个[/quote] 我debug单独执行这一句(inspect),显示就是如下两句话: Query cannot be resolved to a type Duplicate local variable query
nikyotensai 2016-09-29
  • 打赏
  • 举报
回复
引用 15 楼 guoyilin1 的回复:
[quote=引用 12 楼 qq_26508409 的回复:] topUp.getGiving().getGid()这个值不会带引号吧,看看最终的hql什么样子
HQL就是这个啊:from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"' 但是报错之后,给我打印出来的不是这句,hql语句执行出来了,list里面都有值,然后就错了[/quote] 你是不是在逗我,问你hql到Query query = sessionFactory.getCurrentSession().createQuery(hql);这句的时候值是什么? 你说就是这个
guoyilin1 2016-09-28
  • 打赏
  • 举报
回复
引用 5 楼 fangmingshijie 的回复:
[quote=引用 4 楼 花谢尊前不敢香的回复:]不要直接拼接,使用:设置方式
=:当时设置,你那样应该是值有特殊字母[/quote] 没有这样写过hql语句啊,我的被查询的数据表里面貌似并没有特殊字符,只有时间和数字格式的数值......求大神写一下设置方式的例子
guoyilin1 2016-09-28
  • 打赏
  • 举报
回复
没有这样写过hql语句啊,我的被查询的数据表里面貌似并没有特殊字符,只有时间和数字格式的数值......求大神写一下设置方式的例子~
guoyilin1 2016-09-28
  • 打赏
  • 举报
回复
引用 8 楼 qnmdcsdn 的回复:
语句如下:from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"' 错误信息如下:Struts Problem Report Struts has detected an unhandled exception: Messages: 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 'from Giving where die=0 and gid='1'' at line 1 报错跟你贴的是不是一个地方的
是一个地方啊,我debug走的就是一个地方,而且查询结果已经出来了,之后就报错
  • 打赏
  • 举报
回复
语句如下:from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"' 错误信息如下:Struts Problem Report Struts has detected an unhandled exception: Messages: 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 'from Giving where die=0 and gid='1'' at line 1 报错跟你贴的是不是一个地方的
  • 打赏
  • 举报
回复
引用 4 楼 花谢尊前不敢香的回复:
不要直接拼接,使用:设置方式
=:当时设置,你那样应该是值有特殊字母
  • 打赏
  • 举报
回复
不要直接拼接,使用:设置方式
guoyilin1 2016-09-27
  • 打赏
  • 举报
回复
调用dao层方法的action代码如下:(其中findByGivingId就是dao层方法,findByGivingId1是实现分页方法) TopUp topUp0 = new TopUp(); topUp0.setGiving(giving); int pageNumNow = 1;//设置页码初始值为1 if (page!= null){ pageNumNow = page.getPageNum(); } List<TopUp> list = topUpService.findByGivingId(topUp0); page=new Page(list.size(),pageNumNow,6); TopUp topUp1 = new TopUp();; topUp1.setPage(page); topUp1.setGiving(giving); topUpList = topUpService.findByGivingId1(topUp1); for(TopUp topUp2:topUpList){ if(topUp2.getGiving()!=null){ Giving giving1 = new Giving(); giving1 = givingService.findByGivingId(topUp2.getGiving()); topUp2.setGiving(giving1); } if(topUp2.getIccard()!=null){ Iccard iccard1 = new Iccard(); iccard1 = iCcardService.pay(topUp2.getIccard()); topUp2.setIccard(iccard1); } } Map<String,Object> session1 = null; session1 = ActionContext.getContext().getSession(); session1.put("giving", giving); return SUCCESS;
guoyilin1 2016-09-27
  • 打赏
  • 举报
回复
使用该数据库语句的dao层实现类如下: String hql="from TopUp where die=0 and giving.gid='"+topUp.getGiving().getGid()+"'"; @SuppressWarnings("unchecked") Page page = topUp.getPage(); Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setFirstResult(page.getRowStart()); query.setMaxResults(page.getPageSize()); List<TopUp> list = query.list(); return list;
  • 打赏
  • 举报
回复
看下这个SQL对应的代码

67,513

社区成员

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

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