Hi all I am facing a problem while using hibernate criteria query
My object structure as follows
StockBatch { Long id; String batchNumber; Date batchdate; Collection<Stock> stock; Item item; }
Stock { Long id; double quantity; Unit atUnit; String stockStatus; }
Unit{
Long id, String name,(e.g :Chenni) String location }
Item {
Long id, String name, (e.g :N79 Mobile) String code; Double unitWeight }
I tried to find quantity of N79 mobiles(item) in Chenni unit(atUnit). But i got the stock of N79 mobiles in Chenni,Kolkata,Mumbai units
I used Hibernate Criteria Query as below
public Collection<StockBatch> getStock(Item item,Unit atUnit)throws Exception{
Collection<StockBatch> stockBatckList = new ArrayList<StockBatch>();
Criteria criteria = getSession().createCriteria(StockBatch.class);
criteria.add(Expression.eq("item.id", item.getId())); // item id of N79
criteria.createCriteria("stock", "stockAlias"); criteria.add(Expression.eq("stockAlias.atUnit.id", unit.getId()); // unit id of Chenni
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); stockBatckList = criteria.list();
return stockBatckList; }
I was queried for quantity like below
Collection<StockBatch> stockBatchList = getStock(item,atUnit);
double StockQuantity =0.0;
for(StockBatch stockBatch : stockBatchList){
for(Stock stock : stockBatch.getStock()){
StockQuantity = StockQuantity+stock.getQuantity(); } }
How it is possible to avoid the unwanted data ? is it possible by using any JOIN ?
Thanks in advance
|