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);
            }
        }   
    }

  • 相关阅读:
    font-weight:bolder与设置数值的区别
    纯CSS3打造圆形菜单
    CSS Specificity
    控制页面内跳转
    解决Python操作MySQL中文乱码的问题
    字体图标font-awesome
    linux下安装使用MySQL 以及 python mysqldb 遇到的问题
    CocosCreator游戏开发(二)SocketIO简易教程
    CocosCreator游戏开发---菜鸟学习之路(一)资料整理
    2017已经接近尾声,然而我却什么都没干成
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3929661.html
Copyright © 2011-2022 走看看