1. 程式人生 > >hibernate中executeFind()方法和execute()方法區別

hibernate中executeFind()方法和execute()方法區別

返回型別不同:executeFind()方法返回集合,execute()方法返回物件

轉載地址

executeFind方法

@Override
    public List<TCpContact> getCpContactByPage(final Map<String,Object> params,
            final int pageNo, final int pageSize) {
        List<TCpContact> list=getHibernateTemplate().executeFind(new HibernateCallback<Object>() {          
            @Override
public Object doInHibernate(Session session) throws HibernateException,SQLException { Query query =session.createQuery(hql(params)); List<TCpContact> result=query.setFirstResult(((pageNo-1)* pageSize)).setMaxResults(pageSize).list(); return
result; } }); return list; }

execute方法

public TblUser login(final String name, final String pass) throws Exception {
   return (TblUser)super.getHibernateTemplate().execute(new HibernateCallback(){
    public Object doInHibernate(Session s) throws HibernateException, SQLException {
     Query q=s.createQuery("from TblUser as u where u.uname=:name and u.upass=:pass"
); q.setString("name", name); q.setString("pass", pass); return q.list().get(0); } }); }