Java

          Java

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            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 閱讀(2727) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 聂拉木县| 久治县| 通道| 孝昌县| 义马市| 固镇县| 修水县| 洛南县| 遂溪县| 聂荣县| 商都县| 镇平县| 大英县| 射洪县| 岑溪市| 冕宁县| 五峰| 城固县| 都匀市| 大荔县| 石棉县| 宁明县| 汝南县| 新津县| 宁乡县| 舒城县| 北安市| 当涂县| 皮山县| 河源市| 舞钢市| 寿阳县| 邯郸市| 天峨县| 云林县| 清流县| 贡嘎县| 塔河县| 仁布县| 察雅县| 南木林县|