码迷,mamicode.com
首页 > Web开发 > 详细

HibernateUtil

时间:2014-12-24 20:11:00      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

HibernateUtil

标签:

原文地址:http://www.cnblogs.com/hudie/p/4183227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!