-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: problem with HQL parametrized query
PostPosted: Wed Jun 25, 2008 10:48 am 
Newbie

Joined: Wed Jun 25, 2008 10:35 am
Posts: 3
Name and version of the database you are using:MySQL 5.0
I have a problem when trying the following: I want to search with 2 drop down boxes and a submit button. Here's the code and do not be scarry of it's length because for you it may be very simple :

Code:
public static List<Car> getAllCars(Session hibSession, Integer upprice, Integer downprice) {
      Query query = hibSession.createQuery ("from Car where price between (upprice = :upprice) and (downprice = :downprice)");
   query.setInteger("upprice", upprice);
   query.setInteger("downprice", downprice);
   return  (List<Car>) query.list();
   }

And this is the Servlet:
Code:
public class Search extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String  uppr = request.getParameter("upprice");
      Integer upprice = Integer.parseInt(uppr);
      String  downpr = request.getParameter("downprice");
      Integer downprice = Integer.parseInt(downpr);
      Session hibSession = HibernateUtils.startSession();
      List<Car> price = AccessCars.getAllCars(hibSession, upprice, downprice);
      hibSession.flush();
      hibSession.close();
      HttpSession session = request.getSession();
      session.setAttribute("price", price);
      response.sendRedirect("Search.jsp");
   }             
}

and this is the Search.jsp where the results should be //this is only a part of it:

<table border="1">
<tr>
<td>Brand</td>
<td>Model</td>
<td>Color</td>
<td>Engine</td>
<td>Price</td>
</tr>
<c:forEach var="car" items="${price}">
<tr>
<td><c:out value="${car.marka}"></c:out></td>
<td><c:out value="${car.model}"></c:out></td>
<td><c:out value="${car.color}" ></c:out></td>
<td><c:out value="${car.engine}"></c:out></td>
<td><c:out value="${car.price}" ></c:out></td>
</tr>
</c:forEach>
</table>
Here is the error:
description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: = near line 1, column 48 [from entities.Car where price between (upprice = :upprice) and (downprice = :downprice)]
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:225)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
access.AccessCars.getAllCars(AccessCars.java:12)
servlets.Search.doGet(Search.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

<AST>:1:48: unexpected AST node: =
org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4114)
org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3781)
org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1758)
org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 2:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Unless things have changed recently you need to assign an alias to the "Car" entity in the HQL. Eg.

Code:
from Car c where price between (c.upprice = :upprice) and (c.downprice = :downprice)


Top
 Profile  
 
 Post subject: Thanks for the support but doesn't help
PostPosted: Thu Jun 26, 2008 10:12 am 
Newbie

Joined: Wed Jun 25, 2008 10:35 am
Posts: 3
I' ve done it as you wrote but the same mistake still appears :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 2:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Ahh... now I see that the query looks a bit strange be a bit strange. Do you really have all three properties 'price', 'upprice' and 'downprice' defined on the Car object?

Maybe it should be:

Code:
from Car c where c.price between :upprice and :downprice


Top
 Profile  
 
 Post subject: hmm
PostPosted: Fri Jun 27, 2008 3:22 am 
Newbie

Joined: Wed Jun 25, 2008 10:35 am
Posts: 3
Actually I don't have upprice and downprice as properties of Car object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.