HibernateUtil

 1 package com.ssh.util;
 2 
 3 import org.hibernate.SessionFactory;
 4 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 5 import org.hibernate.cfg.Configuration;
 6 import org.hibernate.service.ServiceRegistry;
 7 import org.hibernate.tool.hbm2ddl.SchemaExport;
 8 
 9 public class HibernateUtil {
10     private static final SessionFactory sessionFactory = buildSessionFactory();
11 
12     private static SessionFactory buildSessionFactory() {
13         try {
14             
15             //Hibernate 4.x 时代
16             Configuration cfg = new Configuration();
17             cfg.configure();
18             
19 //            SchemaExport se = new SchemaExport(cfg);
20 //            se.create(true, true);
21 
22             ServiceRegistry sr = 
23                 new StandardServiceRegistryBuilder()
24                     .applySettings(cfg.getProperties())
25                     .build();
26 
27             SessionFactory factory = cfg.buildSessionFactory(sr);
28             
29             return factory;
30             // Create the SessionFactory from hibernate.cfg.xml
31            // return new Configuration().configure().buildSessionFactory();
32         }
33         catch (Throwable ex) {
34             // Make sure you log the exception, as it might be swallowed
35             System.err.println("Initial SessionFactory creation failed." + ex);
36             throw new ExceptionInInitializerError(ex);
37         }
38     }
39 
40     public static SessionFactory getSessionFactory() {
41         return sessionFactory;
42     }
43 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。