qty|price
1 | 4
4 | 5
6 | 4
2 | 3
1 | 2
是數據庫上的條目。我想獲得的結果:(集團通過價格數量)java hibernate彙總錯誤
qty|price
7 | 4
4 | 5
2 | 3
1 | 2
我成功當我這樣做使用sqlwave此查詢到我的本地DATABSE
SELECT price, sum(quantity) FROM table
但是當我嘗試這在Java /休眠..並嘗試列出,我沒有得到sum()聚合的結果。我試圖聚合..但它給了我所有的數量,而不是價格的總和。我如何限制,所以我得到的具體價格數量的總和??
這裏是我的方法:
protected void listInventory(String productID)
{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Query query = session.createQuery("SELECT price, sum(quantity) FROM table WHERE product_code=:productID GROUP BY price");
query.setParameter("product_code",productID);
List iv = query.list();
for(Iterator iterator = iv.iterator();iterator.hasNext();){
Products pb = (Products)iterator.next();
System.out.println("Price: "+pb.getPrice());
System.out.println("Quantity: " + pb.getQuantity());
}
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();serverWriter.flush();
}
}
請發表您的具體HQL/JPQL查詢,你怎麼讀它,使用Hibernate的生成SQL一起。 – chrylis
@chrylis更新。我知道pb.getQuantity()只會得到該特定條目的數量..所以我想知道如何可以將該價格的總計數量放置在那裏。 – user2556304