首页 > 代码库 > Hibernate基础工具类

Hibernate基础工具类

 1 import org.hibernate.Session;
 2 import org.hibernate.SessionFactory;
 3 import org.hibernate.cfg.Configuration;
 4 
 5 public class Hibernate_Util {
 6 
 7     private static SessionFactory factory;
 8 
 9     static {
10         Configuration configure = new Configuration().configure();
11         factory = configure.buildSessionFactory();
12         Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
13 
14             @Override
15             public void run() {
16                 System.out.println("资源关闭了");
17                 factory.close();
18             }
19         }));
20     }
21 
22     public static Session openSession() {
23         Session session = factory.openSession();
24         return session;
25     }
26 
27     public static Session getCurrentSession() {
28         Session session = factory.getCurrentSession();
29         return session;
30     }
31 }

 

Hibernate基础工具类