select id,order_id,book_id,unit_price,
sum(quantity) as quantity
from order_details
group by book_id
以上SQL
的目的是想把book_id相同的查询结果合并,将他们的quantity相加
我想要转化成用NHIBERNATE来查询,应该如何做
可否用以下HQL来查询?
string hql = "select id,order.id,book.id,unitPrice,sum(quantity) as quantity
from OrderDetail
group by book.id"
Query query = session.createQuery(hql);
List<OrderDetail> OrderDetails = (List<OrderDetail>)query.list();
for (OrderDetail orderDetail : OrderDetails) {
System.out.println(orderDetail.getBook().getId());
System.out.println(orderDetail.getQuantity());
System.out.println("---------------------");
}我想用foreach来遍历
但是这样会报错
[Ljava.lang.Object; cannot be cast to com.witbridge.ebook.entity.OrderDetail
请各位帮忙
sum(quantity) as quantity
from order_details
group by book_id
以上SQL
的目的是想把book_id相同的查询结果合并,将他们的quantity相加
我想要转化成用NHIBERNATE来查询,应该如何做
可否用以下HQL来查询?
string hql = "select id,order.id,book.id,unitPrice,sum(quantity) as quantity
from OrderDetail
group by book.id"
Query query = session.createQuery(hql);
List<OrderDetail> OrderDetails = (List<OrderDetail>)query.list();
for (OrderDetail orderDetail : OrderDetails) {
System.out.println(orderDetail.getBook().getId());
System.out.println(orderDetail.getQuantity());
System.out.println("---------------------");
}我想用foreach来遍历
但是这样会报错
[Ljava.lang.Object; cannot be cast to com.witbridge.ebook.entity.OrderDetail
请各位帮忙
这个顺序
把book_id相同的查询结果合并,将他们的quantity相加
并且让查询的结果为OrderDetail的类型呢
select id,order_id,book_id,unit_price,
sum(quantity) as quantity
from order_details
group by book_id因为你用了组函数,group by后面还应该包括id,order_id,unit_price.
请问有什么办法让NHIBERNATE执行这条SQL并且返回OrderDetail对象吗?
select id,order_id,book_id,unit_price,
sum(quantity) as quantity
from order_details
group by book_id
修改:select new com.witbridge.ebook.entity.OrderDetail(OD.id,OD.order_id,OD.book_id,OD.unit_price,sum(OD.quantity) as quantity)
from OrderDetail OD group by OD.book_idtry it...
public OrderDetail(Integer quantity, Double unitPrice, Book book) {
super();
this.quantity = quantity;
this.unitPrice = unitPrice;
this.book = book;
}我在实体类中建立了如上的构造函数String hql2 = " select new OrderDetail(sum(od.quantity) as quantity,od.unitPrice,book)
from OrderDetail as od inner join od.book book";
Query query = session.createQuery(hql2);执行以上是报错
Unable to locate appropriate constructor on class [com.witbridge.ebook.entity.OrderDetail]但是我不能将sum(od.quantity) as quantity 写成 sum(od.quantity) as od.quantity
求高手指点正确写法