使用spring的HibernateTemplate 过程中发现有很多方法和直接使用hibernate 查询的方法不同
1、比如String hql = "select s from ShopInfo s where s.userId=:userid"; 这个hql语句 使用hibernate查询
Query query = this.getDaoDelegate().createQuery( hql ).setParameter("userid", userid);  这样得到一个query了 可以通过setParameter方法写对参数赋值,可是通过HibernateTemplate  如何进行查询啊
2、使用HibernateTemplate 的executeFind 和execute等方法的时候是返回一个HibernateCallback对象 要实现doInHibernate这个方法 这样查询的时候就都使用了一个匿名函数
public List getAll(){
  return getHibernateTemplate().executeFind(new HibernateCallback() {
     public Object doInHibernate(Session s) {
        return s.createQuery("from Student").list();
      }
    });
 }
感觉这样很麻烦 有什么好的方法解决么 还请朋友多多指教 在线等

posted on 2007-12-10 09:54 LifeNote 阅读(5365) 评论(3)  编辑  收藏 所属分类: JavaHibernateSpring
Comments
  • # re: getHibernateTemplate() 学习的疑问
    ph
    Posted @ 2007-12-10 10:55
    //-------------------------------------------------------------------------
    // Convenience finder methods for HQL strings
    //-------------------------------------------------------------------------

    /**
    * Execute an HQL query.
    * @param queryString a query expressed in Hibernate's query language
    * @return a {@link List} containing the results of the query execution
    * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
    * @see org.hibernate.Session#createQuery
    */
    List find(String queryString) throws DataAccessException;

    /**
    * Execute an HQL query, binding one value to a "?" parameter in the
    * query string.
    * @param queryString a query expressed in Hibernate's query language
    * @param value the value of the parameter
    * @return a {@link List} containing the results of the query execution
    * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
    * @see org.hibernate.Session#createQuery
    */
    List find(String queryString, Object value) throws DataAccessException;

    /**
    * Execute an HQL query, binding a number of values to "?" parameters
    * in the query string.
    * @param queryString a query expressed in Hibernate's query language
    * @param values the values of the parameters
    * @return a {@link List} containing the results of the query execution
    * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
    * @see org.hibernate.Session#createQuery
    */
    List find(String queryString, Object[] values) throws DataAccessException;

    /**
    * Execute an HQL query, binding one value to a ":" named parameter
    * in the query string.
    * @param queryString a query expressed in Hibernate's query language
    * @param paramName the name of the parameter
    * @param value the value of the parameter
    * @return a {@link List} containing the results of the query execution
    * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
    * @see org.hibernate.Session#getNamedQuery(String)
    */
    List findByNamedParam(String queryString, String paramName, Object value)
    throws DataAccessException;

    /**
    * Execute an HQL query, binding a number of values to ":" named
    * parameters in the query string.
    * @param queryString a query expressed in Hibernate's query language
    * @param paramNames the names of the parameters
    * @param values the values of the parameters
    * @return a {@link List} containing the results of the query execution
    * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
    * @see org.hibernate.Session#getNamedQuery(String)
    */
    List findByNamedParam(String queryString, String[] paramNames, Object[] values)
    throws DataAccessException;  回复  更多评论   
  • # re: getHibernateTemplate() 学习的疑问
    ph
    Posted @ 2007-12-10 11:05
    简言之,用“?”的参数可以用find(hql, values), 用:userid可以使用findByNamedParam(hql, paramNames, values)  回复  更多评论   
  • # re: getHibernateTemplate() 学习的疑问
    LifeNote
    Posted @ 2007-12-10 17:07
    多谢指点 还是多看下api的好啊 呵呵   回复  更多评论   

只有注册用户登录后才能发表评论。


网站导航: