zoukankan      html  css  js  c++  java
  • HibernateUtil.java

    package com.keer.hibernate;

    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;

    public class HibernateUtil {
        
        private static final SessionFactory sessionFactory;
        
        static {
            try {
                Configuration cfg = new Configuration().configure();
                sessionFactory = cfg.buildSessionFactory();
            } catch(Throwable e) {
                System.err.println("Initial SessionFactory creation failed" + e );
                throw new ExceptionInInitializerError(e);
            }
        }
        
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
        
        public static Session getSession() {
            return sessionFactory.openSession();
        }
        
        public static void closeSession(Session session) throws HibernateException {
            if(session != null) {
                if(session.isOpen()) {
                    session.close();
                }
            }
        }
        
        public static void rollback( Transaction tran ) {
            try {
                if(tran != null) {
                    tran.rollback();
                } 
            } catch (HibernateException he) {
                System.out.println("Rollback faild." + he);
            }
        }   
    }

  • 相关阅读:
    摘:SQL Server数据类型的25种
    二维码简介和容错率的问题
    PHP QR Code
    Git 更新操作
    [转载]ecmall语言包程序
    linux 从百度网盘下载文件的方法
    Linux定时备份数据到百度云盘
    nginx整合php+lua+oracle环境搭建
    php 36进制与10进制转换
    “互联网+”取代O2O将成为2016最大风口
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3929661.html
Copyright © 2011-2022 走看看