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

  • 相关阅读:
    微服务负载均衡技术
    dubbo 协议注册中心
    dubbo 元数据中心
    @Autowired 写在构造方法上
    onchange事件 is not defined
    转:JNDI的本质及作用
    The valid characters are defined in RFC 7230 and RFC 3986报错处理
    FOR XML PATH 简单介绍
    ROW_NUMBER() OVER函数的基本用法
    java和js中JSONObject,JSONArray,Map,String之间转换
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3929661.html
Copyright © 2011-2022 走看看