2

I have a simple one-to-many relationship between table Order and table Line.

I want a query that reports each Order entity and the sum of the Line.Value field.

I can do this in HQL:

select order, sum(line.Value) as LineValue 
from Order as order   
  join order.Lines as line
group by order

So far, so good. When I run this I get a List of object arrays where [0] is the Order and [1] is the sum of the line values.

How do I do the same thing with the criteria API?

The closest I can get is something like:

session.CreateCriteria(typeof(Order))
  .CreateAlias("Lines", "l")
  .SetProjection(Projections.ProjectionList()
     .Add(Projections.GroupProperty("Id"))
     .Add(Projections.GroupProperty("Customer"))
     /* ... ditto for each Order field ... */
     .Add(Projections.Sum("l.Value"))

I have to manually add each property off Order that I want to return. How do I specify that I want to group by all the Order fields?

Rob Walker
  • 46,588
  • 15
  • 99
  • 136

0 Answers0