多个实体类进行分页查询

1>这种查询只能查询3个及3个以下的实体类。

public PaginationSupport fingEntity(int type, int pageSize,
			int startIndex) {
String jpql = " from AA a,BB b,CC c";
jpql += " where a.id=b.id and b.e_id= c.id";
		
PaginationSupport ps = this.selectPage(jpql, pageSize, startIndex);
return ps;	
}

@Override
public List getList() {
// TODO Auto-generated method stub
String jpql = " select a.e_name,lp FROM AA a,PP lp WHERE a.id = lp.e_id  ";
//System.out.println("jpql:"+jpql);
List list = this.select(jpql);	

return list;
}

@Override
public PaginationSupport selectPage(String jpql, int pageSize,
			int startIndex) {
		int totalCount = selectCount(jpql);
		if (totalCount < 1)
			return new PaginationSupport(new ArrayList(0), 0);
		Query query = entityManager.createQuery(jpql);
		List items = query.setFirstResult(startIndex).setMaxResults(pageSize).getResultList();
		PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
		return ps;
	}


2> 查询实体涉及4个及4个以上,可以采用以下方法:


@Override
public PaginationSupport findEnterpriseList(int is_submit,String real_name, int pageSize,int currentPage) {
String jpql ="SELECT enter.id,enter.e_name,u.real_name, u.telephone_number, u.mailbox ,";
jpql+= " FROM (SELECT * FROM EE) enter  " ;		
jpql+= " LEFT JOIN (SELECT * FROM UU ) u ON enter.id = u.e_id ";		   
jpql+= " LEFT JOIN (SELECT * FROM MM) manu ON enter.id = manu.e_id";
jpql+= " LEFT JOIN (SELECT * FROM NS ) ns ON enter.id = ns.id ";		
jpql+= " WHERE enter.is_submit = ? " ;
if(!"".equals(real_name)){
jpql+=" AND u.real_name LIKE '%"+real_name+"%' ";
}
jpql+= "ORDER BY enter.id DESC ";		
		
Object[] objects = {is_submit};
try{
Map<String,Class> clazz=new HashMap<String,Class>();
PaginationSupport ps = this.selectPageSql(jpql, clazz, pageSize, currentPage,objects);	
return ps;
}catch(Exception e){
	e.printStackTrace();
}
return null;
}



daoImpl:

@Override
	public PaginationSupport selectPageSql(String jpql,
			Map<String, Class> entity, int pageSize, int startIndex,
			Object... objects) {
			int count=0;
		
		Session session = (Session)entityManager.getEntityManagerFactory().createEntityManager().getDelegate();
		SQLQuery query=session.createSQLQuery(jpql);
		for(String classname:entity.keySet()){
			query.addEntity(classname, entity.get(classname));
		}
		for (int i = 0; i < objects.length; i++) {
			query.setParameter(i, objects[i]);
		}
		count= query.list().size();
		List items = query.setFirstResult(startIndex).setMaxResults(pageSize).list();
		PaginationSupport ps = new PaginationSupport(items, count, pageSize, startIndex);
		return ps;
	}




推荐下开的淘宝小店筱夜宫主( http://xiaoyegongzhu.taobao.com/)DR.HU 胡博士美妆~坐在电脑旁的汉子姑娘注意脸部护理呀,做了程序媛,也要美丽啊。。。小店有优惠码:YE4450,报优惠码,抵两元券,(⊙o⊙)…

你可能感兴趣的:(java,sql,Hibernate,daoimpl,hpql)