HibernateUtils.java
上传用户:kimgenplus
上传日期:2016-06-05
资源大小:20877k
文件大小:1k
源码类别:

OA系统

开发平台:

Java

  1. package com.bjsxt.oa.model;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. public class HibernateUtils {
  6. private static SessionFactory factory;
  7. private HibernateUtils() {}
  8. static {
  9. Configuration cfg = new Configuration().configure();
  10. factory = cfg.buildSessionFactory();
  11. }
  12. public static SessionFactory getSessionFactory() {
  13. return factory;
  14. }
  15. public static Session getSession() {
  16. factory.getCurrentSession();
  17. return factory.openSession();
  18. }
  19. public static void closeSession(Session session) {
  20. if (session != null) {
  21. if (session.isOpen()) {
  22. session.close();
  23. }
  24. }
  25. }
  26. }