hibernate distinct 分页

获得条数:由于初次在hibernate中用distinct 先写个简单的sql试试。ok的。

     public int getSendCount(final String userid) {

Stringsql="selectcount(distinctt.ctid)fromt_sendtwheret.userid='"

+userid+"'";

intresult=0;

try{

result=Integer.parseInt(String.valueOf(getHibernateTemplate()

.getSessionFactory().getCurrentSession()

.createSQLQuery(sql).uniqueResult()));//这块我再想session是否关闭的问题

}catch(RuntimeExceptionre){

result=0;

}

returnresult;

    }

获得list分页:

 public List<?> findSendList(final String userid, final int startPoint,

finalintrecordPerPage){

Listinstances=(List)getHibernateTemplate().executeFind(

newHibernateCallback(){

/*

*重载方法:@see

*org.springframework.orm.hibernate3.HibernateCallback

*#doInHibernate(org.hibernate.Session)

*方法描述:(这里用一句话描述这个方法的作用)适用条件:(这里描述这个方法适用条件–可选)

*

*@paramsession

*

*@return

*

*@throwsHibernateException

*/

publicObjectdoInHibernate(Sessionsession)

throwsHibernateException{

Criteriacriteria=session.createCriteria(TSend.class);

if(StringUtils.isNotEmpty(userid)){

criteria.add(Restrictions.eq("userid",userid));

}

ProjectionListprojectionList=Projections

.projectionList();

projectionList.add(Projections.property("contentid"));

projectionList.add(Projections.property("ctid"));

criteria.setProjection(Projections

.distinct(projectionList));

criteria.setFirstResult(startPoint);

criteria.setMaxResults(recordPerPage);

Listlist=criteria.list();

Listresult=newArrayList();

if(list!=null&&list.size()>0){

Iteratorit=list.iterator();

while(it.hasNext()){

Object[]obj=(Object[])it.next();

TSendtSend=newTSend();

tSend.setContentid(obj[0]+"");

tSend.setCtid(obj[1]+"");

if("1".equals(tSend.getCtid())){

CriteriacriteriaInfolist=session

.createCriteria(TTest.class);

criteriaInfolist.add(Restrictions.eq(

"infoid",tSend.getContentid()));

criteriaInfolist.add(Restrictions.eq(

"ctid",tSend.getCtid()));

TTesttTest=(TTest)criteriaInfolist

.uniqueResult();

tSend.settTest(tTest);

}

result.add(tSend);

}

}

returnresult;

}

});

if(instances==null){

instances=newArrayList();

}

returninstances;

}

相关推荐