hibernate doInHibernate 用法

1:executeFind

public List findRowList(final String queryString, final int rows) {
            return getHibernateTemplate().executeFind(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Query q = session.createQuery(queryString);
                    q.setFirstResult(0);
                    q.setMaxResults(rows);
                    List list = q.list();
                    if (list == null) {
                        list = Collections.EMPTY_LIST;
                    }
                    return list;
                }
            });
    }

 

2:   protected Integer queryCount(final QueryCondition condition) throws ScriptException {
        try {
           return (Integer)getHibernateTemplate().execute(new HibernateCallback() {
               public Object doInHibernate(Session session) throws HibernateException {
                   Query q =createQuery(session,condition);
                   List list = q.list();
                   if (list == null) {
                       return new Integer(0);
                   }else{
                       return (Integer)list.get(0);
                   }

               }
           });
       } catch (Exception e) {

           throw new ScriptException(e);
       }
    }

 

3:try {
            return (DMTask) getHibernateTemplate().execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                 DMTask t = null;
                    Query q = session.createQuery(query)
                       .setFirstResult(0)
                       .setMaxResults(1);
                    List result = q.list();
                    if (result != null && result.size() >0) {
                     t = (DMTask) result.get(0);
                    }
                    return t;
                }
            });
        } catch (DataAccessException e) {
            throw new DAOException(e);
        }  

 

 

 

你可能感兴趣的:(Hibernate)