Translate query to NHibernate

Posted by Rob Walker on Stack Overflow See other posts from Stack Overflow or by Rob Walker
Published on 2010-04-22T02:31:43Z Indexed on 2010/04/22 2:33 UTC
Read the original article Hit count: 711

Filed under:
|

I am trying to learn NHibernate, and am having difficulty translating a SQL query into one using the criteria API.

The data model has tables: Part (Id, Name, ...), Order (Id, PartId, Qty), Shipment (Id, PartId, Qty)

For all the parts I want to find the total quantity ordered and the total quantity shipped. In SQL I have:

select shipment.part_id, sum(shipment.quantity), sum(order.quantity)  
  from shipment cross join order
  on order.part_id = shipment.part_id
  group by shipment.part_id

Alternatively:

select id, 
   (select sum(quantity) from shipment where part_id = part.id), 
   (select sum(quantity) from order where part_id = part.id)
  from part

But the latter query takes over twice as long to execute.

Any suggestions on how to create these queries in (fluent) NHibernate? I have all the tables mapped and loading/saving/etc the entities works fine.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about sql