Hi andresgr or anyone else?,
I swapped some emails with you about 2 months ago how to do "select count distincts" in hibernate 3.2 using Criteria and ProjectionLists. You gave me the following sample code to try:
Code:
stackedProductsCriteria.setProjection(Projections.projectionList().add(
Projections.distinct(Projections.projectionList()
.add(Projections.property("deliveryOrder"))
.add(Projections.property("towerNo"))
.add(Projections.property("palletLayerNo")))
.add(Projections.rowCount(), "noOfPallets") )
);
This doesn't work as rowCount() always returns the total number of rows in the table, ie it doesn't do it on the number of distinct rows and I can't seem to get it to do it on the distinct rows ProjectionList.
Here is the ordinary SQL which works that I want to specify in Hibernate:
Code:
select pallet.description, count(distinct
orderproduct.deliveryorder_id, stackedproduct.tower_no, stackedproduct.pallet_layer_no)
from pallet, deliveryorder, stackedproduct, orderproduct
where pallet.id = deliveryorder.pallet_id and
orderproduct.deliveryorder_id = deliveryorder.id and
stackedproduct.orderproduct_id = orderproduct.id
group by pallet.description ;
Here is the Hibernate-generated SQL that doesn't work:
Code:
select distinct orderprodu1_.deliveryorder_id as y0_, this_.tower_no as y1_, this_.pallet_layer_no as y2_, count(*) as y3_, pallet3_.description as y4_, pallet3_.description as y5_ from stackedproduct this_ inner join orderproduct orderprodu1_ on this_.orderproduct_id=orderprodu1_.id inner join deliveryorder deliveryor2_ on orderprodu1_.deliveryorder_id=deliveryor2_.id inner join pallet pallet3_ on deliveryor2_.pallet_id=pallet3_.id group by pallet3_.description
as you can see, it puts in "count(*) instead of "select count(distinct ....)".
I can't use Projections.count(String) either as even if I give the distinct Projection an alias, eg"distinct_rows", the count("distinct_rows") fails because it can't find "distinct_rows" anywhere.
This is really annoying, grrrrr. any ideas?
Thanks