Java

          Java

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            3 Posts :: 12 Stories :: 7 Comments :: 0 Trackbacks

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




          posted on 2008-11-06 08:07 keer 閱讀(2732) 評論(0)  編輯  收藏 所屬分類: hibernate

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 林芝县| 宁蒗| 若羌县| 杭州市| 嘉兴市| 苍南县| 铜梁县| 湘乡市| 大竹县| 鞍山市| 金乡县| 公主岭市| 广昌县| 玛沁县| 铜山县| 隆回县| 弥渡县| 拜城县| 鹿泉市| 梁山县| 建德市| 饶阳县| 漯河市| 澄江县| 色达县| 介休市| 孟连| 双流县| 双桥区| 常德市| 阿荣旗| 山东| 聂荣县| 晴隆县| 寿光市| 沙河市| 九龙县| 河间市| 荣昌县| 禄劝| 新竹县|