我遇到了从jsp页面通过javaBean调用相对简单的HQL查询的问题。我现在正在进行测试,以确保在我对我的页面感到疯狂之前知道如何做所有事情。
我遇到的问题(除了不知道我当然在做什么)当然是函数getHighBidder无法正常工作。如下所述,Bid是User和Item之间的连接。我想拥有一个itemID并使用它来从Bid表中查找最大bidAmt,并返回与该出价相关联的UserID或User对象。但是,当我在当前设置中运行该函数时,它返回null并且我总是以我的错误“找不到项目的高出价者”结束。
我在NetBeans的HQL查询编辑器中的getHighBidder函数中尝试了两个查询(一个已注释掉),并且它成功返回与指定项目的最高出价匹配的User实例。这就是为什么当我在jsp中实际测试查询时,为什么我得到一个空值的原因让我很困惑。
我现在已经处理了这几天了,似乎无处可去。任何关于良好学习工具或参考的帮助,提示,指示或建议都将非常感激。我觉得一旦我解决了这个问题,我将能够创建我项目所需的几乎所有功能。
--- --- EDIT 这是netBeans向我吐的错误:
at org.hibernate.hql.classic.PathExpressionParser.token(PathExpressionParser.java:130)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)
at org.hibernate.hql.classic.SelectParser.token(SelectParser.java:176)
at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86)
at org.hibernate.hql.classic.ClauseParser.end(ClauseParser.java:113)
at org.hibernate.hql.classic.PreprocessingParser.end(PreprocessingParser.java:122)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:29)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:216)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:185)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy59.createQuery(Unknown Source)
at GavelDB.GavelDBHelper.getHighBidder(GavelDBHelper.java:99)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:109)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1600)
以下是我用来调用bean的JSP页面的一部分:
<jsp:useBean id="test" class="GavelDB.GavelDBHelper" scope="page">
<%
...
user = test.getHighBidder(item.getItemId());
if(user == null)
out.println("Failed to find high bidder for Item");
else
out.println("Successfully selected User Number " + user.getUserId() + ": " + user.getUserName());
%>
这是被调用的bean。请注意,getItemByID与我当前的设置一样正常,返回Item对象以便在jsp页面中使用。
public class GavelDBHelper
{
Session session = null;
...
public Item getItemByID(int itemID)
{
Item item = null;
try
{
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery (
"from Item as item where item.id = '" + itemID + "'");
item = (Item)q.uniqueResult();
} catch (Exception e)
{
e.printStackTrace();
}
return item;
}
public User getHighBidder(int itemID)
{
User highBidder = null;
try
{
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery (
"select user" +
"from User as user, Bid as bid" +
"where user.id = bid.user.id and bid.bidAmt in(" +
"select max(b.bidAmt)" +
"from Bid as b" +
"where b.item.id = '" + itemID + "')");
/*Query q = session.createQuery (
"select bid.user"+
"from Bid as bid"+
"where bid.bidAmt in("+
"select max(b.bidAmt)"+
"from Bid as b"+
"where b.item.id = '"+ itemID +"')");*/
highBidder = (User)q.uniqueResult();
} catch (Exception e)
{
e.printStackTrace();
}
return highBidder;
}
...
}
我有以下数据库设计:
= ==================================== =================
User table: <br>
PK USER_ID, <br>
USER_NAME UNIQUE, <br>
...
Item table: <br>
PK ITEM_ID, <br>
FK ITEM_SELLER -> Many to One relationship with user.USER_ID, <br>
FK ITEM_BUYER -> Many to One relationship with user.USER_ID, <br>...
Bid table (bridge between user and item): <br>
PK BID_ID, <br>
FK BIDDER_ID -> Many to One relationship with user.USER_ID, <br>
FK ITEM_ID -> Many to One relationship with item.ITEM_ID, <br>...
答案 0 :(得分:1)
如果查看堆栈跟踪,您将看到Hibernate处理查询字符串时发生错误。
看着这个:
Query q = session.createQuery (
"select user" +
"from User as user, Bid as bid" +
"where user.id = bid.user.id and bid.bidAmt in(" +
"select max(b.bidAmt)" +
"from Bid as b" +
"where b.item.id = '" + itemID + "')");
您连接的字符串之间没有空格,因此您构建的查询字符串将如下所示:
select *userfrom* User as user, Bid as *bidwhere* user.id = bid.user.id and bid.bidAmt *in(select max(b.bidAmt)from* Bid as *bwhere* b.item.id ...
因此hibernate在尝试解析它时抛出异常。
希望有所帮助!
凯特。