0
votes

tx=session.beginTransaction();
Query q=session.createQuery("update Product as p set p.quantity=:n where p.id=:i");
q.setParameter("n",1000);
q.setParameter("i",11);

    int status=q.executeUpdate();  
    System.out.println(status);

I am getting following Exception when I execute above code in hibernate

Sep 02, 2015 5:23:46 PM org.hibernate.hql.internal.ast.ErrorCounter reportError ERROR: line 1:8: unexpected token: in Sep 02, 2015 5:23:46 PM org.hibernate.hql.internal.ast.ErrorCounter reportError ERROR: line 1:8: unexpected token: in line 1:8: unexpected token: in at org.hibernate.hql.internal.antlr.HqlBaseParser.updateStatement(HqlBaseParser.java:239) at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:169) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:131) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:93) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836) at in.edu.pondiuni.service.ProductIssuedService.deleteFromProduct(ProductIssuedService.java:41) at in.edu.pondiuni.controller.ProductIssuedServlet.processRequest(ProductIssuedServlet.java:78) at in.edu.pondiuni.controller.ProductIssuedServlet.doGet(ProductIssuedServlet.java:90) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)

1
Thats strange error says "unexpected token: in at ", but your query doesn't have any token "in", you have somewhere "in" token but you cant use it as its a keyword. And also check if your tokens are typed correctly like if "i" is long you must do like this q.setParameter("i",(long)11);Anudeep Gade
Mr. Anudeep I tried q.setParameter("i",(long)11); but it's not working too..Aashish Kumar
I am not getting any problem in insert and select but getting the problem in updation... Do you know what does this exception means...Aashish Kumar
yes its unable to find the token : which is strange, may be you have different hibernate version..Try without alias "update Product set quantity = :prodQty where id = :productId" , set your parameters types correctly..if productId is mapped as long, then setParameter("productId",(long)11)....Anudeep Gade
If it still doesn't work try binding parameters with positions like "update Product set quantity = ? where id = ? " and setParameter(0, 1000); setParameter(1, (long)11);Anudeep Gade

1 Answers

0
votes

This is an open bug, HQL query fails if entity is inside a package which starts with "in" or "by". For your reference view this link .

You can work around by adding the entity-name attribute in your hbm.xml as below.

<class name="in.package.Entity" entity-name="Entity" table="MY_TABLE">