Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

hibernate subquery using criteria or query

$
0
0
what i want to do is to count(*) a records with this query :

SELECT COUNT(*) FROM ( 
     SELECT COUNT(*) FROM inventory 
     GROUP BY Office_Id, Warehouse_Id, Item_Id, Expired_Date) 
AS Records


question, is there any way to do that with hibernate either using criteria or query. code below mine using criteria.

the code works fine, only i need to get a list ("int records = criteria.list().size();") in order to get total records. so is there anyway i can only get total records without listing data ?

pay attantion to line 18 and 20


public static int getInventoryLevelCounts(Session _session, 
    String _givenProperty, String _givenOperator, Object _givenValue,
    String _property, String _operator, Object _value,  
    String _orderField, String _alias) {

    Criteria criteria = _session.createCriteria(Inventory.class);
    criteria.addOrder(Order.asc(_orderField));

    if(!"".equals(_givenProperty) && !"".equals(_givenOperator) && !"".equals(_givenValue)){
        criteria.add(HibernateCriteriaPresenter.getExpression(_givenProperty,_givenOperator,_givenValue));
    }

    if(!"".equals(_property) && !"".equals(_operator) && !"".equals(_value)){
        _property = CriteriaPresenter.getAliasProperty(criteria, _alias, _property);
        criteria.add(HibernateCriteriaPresenter.getExpression(_property,_operator,_value));
    } 

    criteria.setProjection(getInventoryLevel_ProjectionListTest().add(Projections.rowCount()));

    int records = criteria.list().size();    
    return records;  
}   


   private static ProjectionList getInventoryLevel_ProjectionListTest(){
        ProjectionList projectionList = Projections.projectionList();

        projectionList.add(Projections.groupProperty("id.officeId"));
        projectionList.add(Projections.groupProperty("warehouse.warehouseId"));
        projectionList.add(Projections.groupProperty("item.itemId"));
        projectionList.add(Projections.groupProperty("expiredDate"));       
        
        return projectionList;
    }

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>