Hibernate辅助类(官方推荐)


Hibernate辅助类(官方推荐)

  1. public class HibernateUtil {   
  2.     private static final SessionFactory sessionFactory = buildSessionFactory();   
  3.     private static SessionFactory buildSessionFactory() {   
  4.         try {   
  5.             // Create the SessionFactory from hibernate.cfg.xml   
  6.             return new Configuration().configure().buildSessionFactory();   
  7.         } catch (Throwable ex) {  
  8.             // Make sure you log the exception, as it might be swallowed   
  9.             System.err.println("Initial SessionFactory creation failed." + ex);   
  10.             throw new ExceptionInInitializerError(ex);  
  11.         }   
  12.     }   
  13.     public static SessionFactory getSessionFactory() {   
  14.         return sessionFactory;   
  15.     }  
  16. }  

相关内容