在执行SQL语句的时候通过一个回调可以获得SESSION
return (Cus) getHibernateTemplate().execute(new HibernateCallback() {
 public Object doInHibernate(org.hibernate.Session session)
 throws org.hibernate.HibernateException,
 java.sql.SQLException {
 Query query = session
 .createQuery("from Cus cus where cus.id=:id");
 query.setInteger("id", id);
 List list = query.list();
 Cus cus=(Cus) list.get(0);
 return cus;
 }
 });

解决方案 »

  1.   


    public class BaseDAOimpl extends HibernateDaoSupport{

    public Session openSession() {
    Session session=super.getSession();

            return session;
            
        }
    public static void main(String []args){
    new BaseDAOimpl().openSession();

    }
    }这样不行吗??
      

  2.   

    那种回调的方式是把异常处理交给SPRING管理,还一种方式,不过要保证符合SPRING的DataAccessException的异常体系
    Session session=getSession(getSessionFactory(),false);
    try{
    ...
    }catch(HibernateException ex){
    throw convertHibernateAccessException(ex);
    }