i have an app where i need to fire a jpql query with sum() and Func(year,…) functions, which means only 2 fields are to be fetched and stored into a collection and then this collection is to be returned to the managed bean. now my question is that how do i use this collection to retrieve each value. below is the session bean and managed bean code:
public Collection getScripQtyYearWise(Integer scripID)
{
try
{
Collection coll=em.createQuery("select sum(t.tradeExecutedQuantity), FUNC('YEAR',t.tradeDateTime) from TradeStock t where t.scripID.scripID = :scripID group by FUNC('YEAR',t.tradeDateTime) ").setParameter("scripID", scripID).getResultList();
return coll;
}catch(Exception e){return null;}
}
eg of data returned: sum(qty) year
210 2011
198 2012
i need to extract each of the values in each record returned in followinf managed bean:
objejb=(StockCommodityEJBStateless) new InitialContext().lookup("StockCommodityTest");
Collection coll=objejb.getScripQtyYearWise(scripID1); // how to use this collection?
collis aCollectionofObject[], so you can parse it like this:If you want a List instead of Object[] you can change your select like this:
Read more here!