Hibernate Practice (2)

xiaoxiao2021-03-06  37

Eclipse will automatically generate _baserootdao.java objects, which encapsulates a considerable part of the Hibernate interface, and also implementation of the session management.

Public abstract class _baserootdao {

/ / Store sessionFactory's Haxi table

Protected static map sessionFactoryMap = new hashmap ();

Protected static threadlocal threadedsessions = new threadlocal ();

/ ** * initialize hiberante connection * / public static void initialize () throws HibernateException {initialize ((String) null);} public static void initialize (String configFileName) throws HibernateException {if (null == configFileName && sessionFactoryMap.size () > 0) return; else if (! null = sessionFactoryMap.get (configFileName)) return; else {Configuration cfg = new Configuration (); if (null == configFileName) cfg.configure (); else cfg.configure (configFileName) SetSessionFactory (configFileName, cfg.buildsessionFactory ());}}

/ ** * Put sessionFactory into map * / protected static void setsessionFactory (sessionFactory sessionFactory) {setsessionFactory ((String) null, sessionFactory;}

Protected static void setsessionFactory (string configfilename, sessionFactory sessionFactory) {sessionFactoryMap.put (configfilename, sessionfactory);}

/ * ** The acquired profile SessionFactory * / private static SessionFactory getSessionFactory (String configFile) throws HibernateException {if (sessionFactoryMap.size () == 1) return (SessionFactory) sessionFactoryMap.values ​​() toArray () [0].; else {SessionFactory sessionFactory = (SessionFactory) sessionFactoryMap.get (configFile); if (null == sessionFactory) if (null == configFile) throw new RuntimeException ( "The session factory has not been initialized."); else throw new RuntimeException ( "The session factory for '" configfile "' Has NOT BEEN INITIALIZED."); Else Return sessionFactory;}}

/ ** * Create Session * / public static Session createSession (String configFile) throws HibernateException {/// Gets storage stack java.util.Stack sessionStack Session of the current thread = (java.util.Stack) threadedSessions.get () ; Session session = null; if (null == sessionstack) {// If there is no new stack sessionStack = new java.util.stack (); threadedSssions.Set (sessionstack);} if (sessionstack.size ()> 0) {// If there is, and the stack is greater than 0, take out the top element Object [] arr = (Object []) sessionStack.peek (); string cf = (string) arr [0]; if (null = = CF) {session = (session) Arr [1];} else if (null! = cf && null! = configfile) {if (cf.equals (configfile) session = (session) Arr [1];} (NULL == session) {// If the removed session is empty, regenerate session = getsessionFactory (configfile) .openSession (); arr = new object [2]; arr [0] = configfile; arr [1] = session SessionStack.push (arr);}}} else {session = getsessionFactory (configfile) .openSession (); object [] arr = new object [2]; arr = new object [2]; arr [0] = con Figfile; arr [1] = session; sessionStack.push (arr);} return session;} / ** * close the session * / public void closesession () throws hibernateException {java.util.stack sessionstack = (java.util.) ThreadedSssions.get (); if (null! = Sessionstack) {Object [] arr = (object []) sessionStack.peek (); string cf = (string) arr [0]; if (null == CF) {Session session = (session) Arr [1]; session.close (); sessionStack.pop ();} else {string configurationFile = getConfigurationFileName (); if (null! = ConfigurationFile &&

ConfigurationFile.equals (cf)) {session session = (session) Arr [1]; session.close (); sessionStack.pop ();}}}} / ** * Execute Query operation, return value is List, pay attention to query The writing of statements * / public java.util.list Find (String query) throws hibernateException {session s = null; try {s = getsession (); returnof (query, s);} finally {closesession ();}}} Last

转载请注明原文地址:https://www.9cbs.com/read-63579.html

New Post(0)