/**
*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!!
|