2012-09-05 80 views
0

考慮代碼HibernateCallback實施

public List<Product> listProducts(){ 

    HibernateCallback callBack=new HibernateCallback(){ 

    public Object doInHibernate(Session session){ 
     Query query=session.createQuery("from Product"); 
     return query.list(); 
    } 
    }; 
    return (List<Product>)hibernateTemplate.execute(callBack); 
} 

這有什麼錯與上面的代碼?在蝕太陽神它顯示以下錯誤:

The type new HibernateCallback(){} must implement the inherited abstract method HibernateCallback.doInHibernate(Session) 

doInHibernate()被實現,那麼爲什麼它示出了上述錯誤?

+0

確保'Session'是'org.hibernate.Session',而不是別的(例如'org.hibernate.classic.Session')。 – axtavt

+0

@axtavt:是的,我錯誤地做了這個。以上的改變已經解決了這個問題。你能不能請你發表評論作爲回答,以便我可以接受? –

+0

以答案形式發佈。 – axtavt

回答

3

確保Sessionorg.hibernate.Session,而不是別的(例如org.hibernate.classic.Session)。

這對org.hibernate.Sessionorg.hibernate.classic.Session特別棘手 - 由於後者接口擴展了前者,因此這類商業錯誤不會引起其他問題,也不容易被注意到。