简体   繁体   English

HQL查询-通过联接关系提取数据

[英]HQL query - pulling data through a join relationship

I am having trouble with a relatively simple HQL query being called from a jsp page through a javaBean. 我在通过javaBean从jsp页面调用相对简单的HQL查询时遇到麻烦。 I am just setting things up for testing at the moment to make sure I know how to do everything before I get all crazy with my pages. 我现在只是准备进行测试,以确保在我对页面疯狂之前,我知道该怎么做。

The issue I am having (beyond not knowing exactly what I'm doing of course) is the function getHighBidder is not working properly. 我遇到的问题(当然不完全知道我在做什么)是函数getHighBidder无法正常工作。 As stated below, Bid is a join between User and Item. 如下所述,投标是用户和项目之间的联接。 I want to have an itemID and use it to look up the maximum bidAmt from the Bid table and return the UserID or User object associated with the bid. 我想拥有一个itemID,并使用它来从Bid表中查找最大的bidAmt,并返回与出价相关联的UserID或User对象。 However, when I run the function in my current setup, it returns null and I always end up with my error "Failed to find high bidder for Item". 但是,当我在当前设置中运行该函数时,该函数返回null,并且总是以错误“无法为商品找到高出价者”结束。

I've tried both queries (one is commented out) in the getHighBidder function in the HQL query editor in NetBeans and it successfully returns the User instance that matches the highest bid for the specified item. 我已经在NetBeans的HQL查询编辑器中的getHighBidder函数中尝试了两个查询(其中一个已被注释掉),它成功返回了与指定项目的最高出价相匹配的User实例。 This is why I'm so confused why I'm getting a null values when I actually test the query in jsp. 这就是为什么我如此困惑,为什么我实际上在jsp中测试查询时得到空值的原因。

I've been dealing with this for a couple days now and can't seem to get anywhere. 我已经处理了几天,似乎无法解决任何问题。 Any help, hints, pointers, or advice on good learning tools or references would be extremely appreciated. 任何关于良好学习工具或参考的帮助,提示,指示或建议,将不胜感激。 I feel like once I get this problem solved, I'll be able to created almost all of the functions I need for my project. 我觉得一旦解决了这个问题,我就可以创建项目所需的几乎所有功能。

---EDIT--- Here is the error netBeans spits at me: ---编辑---这是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)

Here is part of the JSP page I am using to call the bean: 这是我用来调用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());

%>

Here is the bean that is being called. 这是被调用的bean。 Note that getItemByID works just fine with my current setup, returning the Item object for use in the jsp page. 请注意,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;
    }

...

}

I have the following database design: 我有以下数据库设计:
===================================================== ================================================== ===

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>...

If you look at the stack trace you will see the error is occurring when Hibernate is processing your query string. 如果查看堆栈跟踪,您将看到Hibernate处理查询字符串时发生错误。

Looking at this: 看这个:

        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 + "')");

You have no whitespace between the strings you are concatenating so the query string you are constructing would look like: 您要连接的字符串之间没有空格,因此要构造的查询字符串如下所示:

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 ...

and hence hibernate is throwing an exception when it tries to parse it. 因此,休眠尝试解析它时会抛出异常。

Hope that helps! 希望有帮助!

Kate. 凯特。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM