-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Inner object fetching problem
PostPosted: Wed Nov 03, 2010 2:45 am 
Newbie

Joined: Tue Jun 17, 2008 11:01 pm
Posts: 16
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


Top
 Profile  
 
 Post subject: Re: Inner object fetching problem
PostPosted: Tue Nov 16, 2010 4:13 am 
Newbie

Joined: Tue Jun 17, 2008 11:01 pm
Posts: 16
What is the use of distinctRootEntity, is it solve this issue ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.