2011-12-13 43 views

回答

1

带上这个HibernateUtil类。它加载没有标准名称的XML和属性文件。希望它有用。

public class HibernateUtil2 { 
    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
    try { 
     URL r1 = HibernateUtil2.class.getResource("/hibernate2.cfg.xml"); 
     Configuration c = new Configuration().configure(r1); 

     try { 
      InputStream is = HibernateUtil2.class.getResourceAsStream("/hibernate2.properties"); 
      Properties props = new Properties(); 
      props.load(is); 
      c.addProperties(props); 
     } catch (Exception e) { 
      LOG.error("Error al leer las propiedades", e); 
     } 

     return c.buildSessionFactory(); 
    } catch (Throwable ex) { 
     // Make sure you log the exception, as it might be swallowed 
     LOG.error("Error al construir SessionFactory", ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

}

相关问题