删除工作经历的DAO方法
public void deleteEmpWork(int WorkId) {
String hql = "Delete From Emp_Work Where ID=?";
Query q = null;
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setInteger(0, WorkId);
q.executeUpdate();
this.session.close();
}获取工作经历列表的方法
public ArrayList<Emp_Work> getEmpWorkByCondition(String empId) {
ArrayList<Emp_Work> work_list = null;
Query q = null;
String hql = "From Emp_Work Where empId=?";
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setString(0, empId);
System.out.println("333");
work_list = (ArrayList<Emp_Work>) q.list();
System.out.println("444");
session.close();
return work_list;
}

解决方案 »

  1.   

    你每个DAO方法都有这样的代码吗?这可不是个好习惯哟。
    Configuration config = new Configuration().configure(); 
    SessionFactory sf = config.buildSessionFactory(); 
      

  2.   

    我对处理session的打开和关闭一直没一个明确的思想因为有时候需要几个不同的DAO对象一起使用所以session需要在方法结束时候关闭掉但是再用的时候为了不是关闭的状态于是就每次都打开  每次都关闭了……请问正确的设计该是怎么做的呢
      

  3.   

    可以用工具生成的这个类。也可以直接写一个单例工厂。来获得sessionimport org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;/**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;    private HibernateSessionFactory() {
        }

    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        
        
        public static Session getAnotherSession() throws HibernateException{
         if (sessionFactory == null) {
    rebuildSessionFactory();
    }
         return sessionFactory.openSession();
           
        }
        
        
        
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        } /**
         *  Rebuild hibernate session factory
         *
         */
    public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    } /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
                session.close();
            }
        } /**
         *  return session factory
         *
         */
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    }
    public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
    } /**
         *  return hibernate configuration
         *
         */
    public static Configuration getConfiguration() {
    return configuration;
    }
    }
      

  4.   

    真取的做法是每个业务需求一个session . 
    比如:做 添加时一个session . 删除时候一个session 。因为session是带缓存的所以不应该将一个session 来完成不同的操作
      

  5.   

    参考HibernateUtil的使用。
    http://dev.21tx.com/2005/09/20/15730.html好像跑题了,呵呵..
      

  6.   

    query.list()方法就失效了 
    可以能是由于你创建了太多 SessionFactory导致的 建议先改一下 获得Sesssion 的方式后再调试。
    就是我 5楼提供的方法。
      

  7.   

    DAO方法中换成
    this.session = SessionFactory.getSession();
    再试试?
    那个类我直接拿来用就是可以的吧?