2011-12-13 56 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; 
} 

}

相關問題