2

我已经声明了一个List<Object[]>返回对象数组的列表,其中每个数组代表结果中的一行。值索引基于您的 select 语句。

productList = 
(List<Object[]>) session.createSQLQuery("
SELECT User.username, User.email, Orders.p_id, Orders.o_id, Product.listed_price " +                                                                                         
"FROM Orders " +                                                                     
"INNER JOIN User ON User.u_id = Orders.u_id " +                                                                  
"INNER JOIN Product ON Product.p_id = Orders.p_id " +                                                                
"WHERE Product.p_id = '"+p_id +"' " +                                                            
"ORDER BY User.username").list();

我有 3 张表User, Product 和 Orders

USER:
-----+-----------+------------+---------+----------+-----------+
 u_id | username  | password   | contact | email   | city      |
------+-----------+------------+---------+---------+-----------+

PRODUCT:
+------+----------+--------------+------+--------------+
| p_id | category | listed_price | qty  | description  |
+------+----------+--------------+------+--------------+

ORDERS:
+------+--------+------+------+-----------+
| o_id | date   | u_id | p_Id | order_qty |
+------+--------+------+------+-----------+

我想按以下顺序将 User 表与 Product 以及 o_id(来自 ORDER)一起内联:

+-----------+-------+------+------+--------------+
| username  | email | p_id | o_id | listed_price |
+-----------+-------+------+------+--------------+

在我的 ActionClass/View 类中,我声明public List<Object[]> productList;从上面的控制器类访问对象列表。

public List<Object[]> productList;

public String listAllProduct(){
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        productList = orderDaoFactory.listProduct(Integer.parseInt( request.getParameter("p_id"))); 
         System.out.println("\t"+productList.get(0).toString());
        return SUCCESS;
}

在我的 JSP 页面中,我使用这个列表(对象数组)productList 作为迭代器标记来迭代所有订购该产品的客户。

<s:iterator value="productList">

        <tr>
            <td><h4><s:property value="username"/></h4></td>    
            <td><h4><s:property value="email"/></h4></td>
            <td><h4><s:property value="p_id"/></h4></td>
            <td><h4><s:property value="o_id"/></h4></td>
            <td><h4><s:property value="listed_price"/></h4></td>            
        </tr>

</s:iterator>

挑战/问题:我的问题是即使在调试后没有任何错误也无法成功获得输出。空心现在想使用 O/R 映射将 Objects JOIN 与其他对象关联起来。请建议我哪里错了。你的建议很有价值。

4

2 回答 2

0
productList = 
(List<Object[]>) session.createSQLQuery("
SELECT User, Orders, Product" +                                                                                         
"FROM User,Orders,Product " +                                                                     
"INNER JOIN User ON User.u_id = Orders.u_id " +                                                                  
"INNER JOIN Product ON Product.p_id = Orders.p_id " +                                                                
"WHERE Product.p_id = '"+p_id +"' " +                                                            
"ORDER BY User.username").list();
Then you will get three object. In first list you will get Object[0]=Users,Object[1]=Orders,Object[2]=Product. Now iterate it. 

版后

productList = 
(List<Object[]>) session.createQuery("
SELECT User, Orders, Product" +                                                                                         
"FROM User,Orders,Product " +                                                                     
"INNER JOIN User ON User.u_id = Orders.u_id " +                                                                  
"INNER JOIN Product ON Product.p_id = Orders.p_id " +                                                                
"WHERE Product.p_id = '"+p_id +"' " +                                                            
"ORDER BY User.username").list();

is 语法有什么问题?

于 2013-02-26T13:13:17.233 回答
0

如果要直接查询对象,请使用 HQL 而不是 SQL。session.createQuery(String)与以下查询一起使用:

SELECT u.username, u.email, o.p_id, o.o_id, p.listed_price FROM Orders as o
INNER JOIN User as u WITH u.u_id = o.u_id
INNER JOIN Product as p WITH p.p_id = o.p_id
WHERE p.p_id = ???
ORDER BY u.username
于 2013-02-26T13:47:52.037 回答