-->
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.  [ 1 post ] 
Author Message
 Post subject: query in many-to-many ORM problem,ask for help!
PostPosted: Tue Aug 08, 2006 1:24 am 
Newbie

Joined: Tue Aug 08, 2006 12:38 am
Posts: 1
/**
*the ORM between Category and Product is many-to -many
*@author GanChunming
*Creation time 2006-8-8 12:44:26
* @param category
* @param page:used for dipart page
* @param security:purview setting
* @return
*/
public List searchWithProduct(IPojo category, Page page, ISecurity security) {
Session session = HibernateUtil.currentSession();
Transaction tr = session.beginTransaction();
StringBuffer hql = new StringBuffer();
hql.append(" from Category as cat join fetch cat.product as cp where (1=1)");

Category cat = (Category) category;
if (cat.getId() != 0)
hql.append(" and cat.id=:cid");
if (cat.getName() != null && !cat.getName().equals(""))
hql.append(" and cp.name like :name ");
if (security == null || security.getJobber() == false)
hql.append(" and cp.restrict=:restrict");
hql.append(" order by cat.layer asc");

Query query = session.createQuery(hql.toString());
if (cat.getId() != 0)
query.setShort("cid", cat.getId());
if (cat.getName() != null && !cat.getName().equals(""))
query.setString("name", "%" + cat.getName() + "%");
if (security == null || security.getJobber() == false)
query.setShort("restrict", (short) 0);
query.setFirstResult((page.getCurrentPage() - 1) * page.getPageSize())
.setMaxResults(page.getPageSize());
List results = query.list();

System.out.println("the results' row before Treeset() is:" + results.size());

for (Iterator it = results.iterator(); it.hasNext();) {
Category cat1= (Category)it.next();

Set product=cat1.getProduct();
if(log.isDebugEnabled()){
log.debug("the number of products in this category is:"+product.size());

}

}
Set resultset = new TreeSet(results);
System.out.println("the results' row after Treeset() is:" + resultset.size());
List results1 = new ArrayList(resultset);
// List results2=new ArrayList(results1);

tr.commit();
HibernateUtil.closeSession();
return results1;
}


my problem is that:when excute this programme,I create a certain instance of Category which OID is 5 in my database ,then call this method.
the print results are:
//*************************
the results' row before Treeset() is:4
13:20:32,250 DEBUG CategoryManagementImpl:147 - the number of products in this category is:2
13:20:32,250 DEBUG CategoryManagementImpl:147 - the number of products in this category is:2
13:20:32,265 DEBUG CategoryManagementImpl:147 - the number of products in this category is:2
13:20:32,265 DEBUG CategoryManagementImpl:147 - the number of products in this category is:2
//******************************


In the database there are indeed 4 data meet the condition .

So I don't know why when debug :the number of products in this category is,its result is 2!
but not 4?!!

someone who knows how to solve this problem please help me,thanks!!


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

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.