nhibernate group by

时间:2010-09-29 15:11:59

标签: nhibernate

我希望显示所有订购产品的清单。我可以使用以下sql语句显示它

select products.Id,count(products.Id), products.Name from Products
inner join orderitems on Products.Id = orderItems.ProductId
left join orders on orderItems.OrderId = orders.Id
where orders.CompanyId = 27
group by products.Id,products.Name

我有以下实体 Order,OrderItem,具有外键关系的产品。

我创建了以下投影类

public class OrderProduct
{
    public int Id { get; set; } // removed virtual
    public string Name { get; set; } // removed virtual
    public int Quantity { get; set; } // removed virtual
}

无论如何,只是希望有人可以启动我应该如何使用条件api或hql来处理此查询

更新

以rafaels为例,我已将hql简化为

 var hql = @"select p.Id, p.Name, count(p.Id) as Quantity 
            from OrderItem oi 
                inner join oi.Product p on p.Id = oi.Product.Id 
                group by p.Id, p.Name";

这将返回以下sql

select product1_.Id as col_0_0_, product1_.Name as col_1_0_, count(product1_.Id) as col_2_0_ from OrderItems orderitem0_ inner join Products product1_ on orderitem0_.ProductId=product1_.Id

非常接近,但它似乎忽略了该组,给我以下错误

列'Products.Id'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

OrderItem的

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="EStore.Domain"
                   namespace="EStore.Domain.Model">

  <class name="OrderItem" table="OrderItems">
    <id name="Id" type="Int32" column="Id" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <property name="Name" column="Name"/>
    <set name="OrderItemAddresses" table="OrderItemAddress" generic="true" cascade="all-delete-orphan"  inverse="true" >
      <key column="OrderItemId" not-null="true" />
      <one-to-many class="EStore.Domain.Model.OrderItemAddress, EStore.Domain" />
    </set>
    <many-to-one name="Product" column="ProductId"  not-null="true" class="EStore.Domain.Model.Product, EStore.Domain" />
    <many-to-one name="Order" column="OrderId"  not-null="true" class="EStore.Domain.Model.Order, EStore.Domain" />
  </class>
</hibernate-mapping>

## product ##

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="EStore.Domain"
                   namespace="EStore.Domain.Model">

  <class name="Product" table="Products">
    <id name="Id" type="Int32" column="Id" unsaved-value="0">
      <generator class="identity"/>
    </id>

    <property name="MinQuantity" column="MinQuantity" type="Int32" not-null="true" />
    <property name="MaxQuantity" column="MaxQuantity" type="Int32" not-null="true" />
    <property name="Name" column="Name" />

  </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:1)

试试这个:

var hql = @"select p.Id, p.Name, count(p.Id) as Quantity 
            from OrderItem oi 
               inner join oi.Products p where p.Id = oi.Product.Id 
               left  join oi.Order o where o.Id = oi.Order.Id 
                 with o.Company.Id = :companyId 
            group by p.Id, p.Name";


var result = session.CreateQuery(hql)
     .SetParameter("companyId", 27)
     .SetResultTransformer(Transformers.AliasToBean(typeof(OrderProduct)))
     .List<OrderProduct>();

注意:如果查询失败,请尝试and而不是with o.Company...