Hibernate API encapsulation and use in the business layer

Source: Internet
Author: User
Tags getmessage rollback
  To create, initialize, and invoke global sessionfactory instances, we use the Hibernateutil class to encapsulate the Hibernate API, reduce the writing of repetitive code, and make the program look more concise     Package ...     import ...     public class Hibernateutil {    private static L OG log = logfactory.getlog (Hibernateutil.class);     private static configuration configuration;     private static Sessionfactory sessionfactory;       Static {       try {          configuration = new configuration ();          sessiongfactory = Configuration. (). Configure (). Buildsessionfactory ();       catch (Throwable ex) {          Log.error (Ex.getmessage ());          throw new Exceptionininitializeerror (ex);      }    }     &NBSp public static Sessionfactory Getsessionfactory () {       return sessionfactory;  & nbsp; }       public static Configuration GetConfiguration () {        return configuration;    }       public static void Rebuildsessionfactory () {    & nbsp;  Synchronized (sessiongfactory) {          try{              sessionffactory=getconfiguration (). Buildsessionfactory ();           } catch (Exception ex) {              log.error (Ex.getmessage ());              Throw Datastoreexception.datastoreerror (ex);           }       }    }       public static void Rebuildsessionfactory ( Configuration cfg) {       Synchronized (sessiongfactory) {           try{             sessionffactory= cfg.buildsessionfactory ();              configuration = cfg;           } catch (Exception ex) {              log.error (Ex.getmessage ());              Throw Datastoreexception.datastoreerror (ex);           }            }       public static void Close () {       try{           sessionfactory.close ();         catch (Exception ex) {           Log.error (Ex.getmessage ());       }    }      //The following is the most commonly used session in the program to open, close, and rollback     public static Session getsession () throws datastoreexception{       try {           return sessionfactory.opensession ();        }catch (Exception ex) {            log.error (Ex.getmessage ());            throw Datastoreexception.datastoreerror (ex);       }     }       public static void CloseSession (Session session) throws Datastoreexception {        try{            session.close ();         catch (Exception ex) {            log.error (Ex.getmessage ());            throw Datastoreexception.datastoreerror (ex);         }     }       public static void RollbackTransaction (Transaction Transaction) throws Datastoreexception {          try{           if (transaction! = NULL)               Transaction.rollback ();          } catch (Exception ex) {              log.Error (Ex.getmessage ());              Throw Datastoreexception.datastoreerror (ex);         }     }}     Encapsulated use example:   & Nbsp; session Session = Hibernateutil.getsession ();     Transaction tx = NULL;     try {      tx = Session.begintransaction ();         //hibernate Database Operations       Query query = session.createquery ...     & nbsp;   Tx.commit ();       } catch (Exception ex) {       Hibernateutil.rollbacktransaction (ex);        throw Datastoreexception.datastoreerror (ex);     } finally {       hibernateutil.closesession (session) ;    }  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.