国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
沒(méi)有SPRING,hibernate 延遲加載

給你粘出幾個(gè)類(lèi)的代碼,你自己看看,
類(lèi)HibernateUtil.java,用這個(gè)類(lèi)來(lái)管理session和事務(wù)

Java代碼
 
  1. import org.hibernate.*;   
  2. import org.hibernate.cfg.Configuration;   
  3. import org.hibernate.exceptions.InfrastructureException;   
  4. import org.apache.commons.logging.*;   
  5.   
  6. import javax.naming.*;   
  7.   
  8. /**  
  9.  * Basic Hibernate helper class, handles SessionFactory, Session and Transaction.  
  10.  * <p>  
  11.  * Uses a static initializer for the initial SessionFactory creation  
  12.  * and holds Session and Transactions in thread local variables. All  
  13.  * exceptions are wrapped in an unchecked InfrastructureException.  
  14.  *  
  15.  * @author christian@hibernate.org  
  16.  */  
  17. public class HibernateUtil {   
  18.   
  19.     private static Log log = LogFactory.getLog(HibernateUtil.class);   
  20.   
  21.     private static Configuration configuration;   
  22.     private static SessionFactory sessionFactory;   
  23.     private static final ThreadLocal threadSession = new ThreadLocal();   
  24.     private static final ThreadLocal threadTransaction = new ThreadLocal();   
  25.     private static final ThreadLocal threadInterceptor = new ThreadLocal();   
  26.   
  27.     // Create the initial SessionFactory from the default configuration files   
  28.     static {   
  29.         try {   
  30.             configuration = new Configuration();   
  31.             sessionFactory = configuration.configure().buildSessionFactory();   
  32.             // We could also let Hibernate bind it to JNDI:   
  33.             // configuration.configure().buildSessionFactory()   
  34.         } catch (Throwable ex) {   
  35.             // We have to catch Throwable, otherwise we will miss   
  36.             // NoClassDefFoundError and other subclasses of Error   
  37.             log.error("Building SessionFactory failed.", ex);   
  38.             throw new ExceptionInInitializerError(ex);   
  39.         }   
  40.     }   
  41.   
  42.     /**  
  43.      * Returns the SessionFactory used for this static class.  
  44.      *  
  45.      * @return SessionFactory  
  46.      */  
  47.     public static SessionFactory getSessionFactory() {   
  48.         /* Instead of a static variable, use JNDI:  
  49.         SessionFactory sessions = null;  
  50.         try {  
  51.             Context ctx = new InitialContext();  
  52.             String jndiName = "java:hibernate/HibernateFactory";  
  53.             sessions = (SessionFactory)ctx.lookup(jndiName);  
  54.         } catch (NamingException ex) {  
  55.             throw new InfrastructureException(ex);  
  56.         }  
  57.         return sessions;  
  58.         */  
  59.         return sessionFactory;   
  60.     }   
  61.   
  62.     /**  
  63.      * Returns the original Hibernate configuration.  
  64.      *  
  65.      * @return Configuration  
  66.      */  
  67.     public static Configuration getConfiguration() {   
  68.         return configuration;   
  69.     }   
  70.   
  71.     /**  
  72.      * Rebuild the SessionFactory with the static Configuration.  
  73.      *  
  74.      */  
  75.      public static void rebuildSessionFactory()   
  76.         throws InfrastructureException {   
  77.         synchronized(sessionFactory) {   
  78.             try {   
  79.                 sessionFactory = getConfiguration().buildSessionFactory();   
  80.             } catch (Exception ex) {   
  81.                 throw new InfrastructureException(ex);   
  82.             }   
  83.         }   
  84.      }   
  85.   
  86.     /**  
  87.      * Rebuild the SessionFactory with the given Hibernate Configuration.  
  88.      *  
  89.      * @param cfg  
  90.      */  
  91.      public static void rebuildSessionFactory(Configuration cfg)   
  92.         throws InfrastructureException {   
  93.         synchronized(sessionFactory) {   
  94.             try {   
  95.                 sessionFactory = cfg.buildSessionFactory();   
  96.                 configuration = cfg;   
  97.             } catch (Exception ex) {   
  98.                 throw new InfrastructureException(ex);   
  99.             }   
  100.         }   
  101.      }   
  102.   
  103.     /**  
  104.      * Retrieves the current Session local to the thread.  
  105.      * <p/>  
  106.      * If no Session is open, opens a new Session for the running thread.  
  107.      *  
  108.      * @return Session  
  109.      */  
  110.     public static Session getSession()   
  111.         throws InfrastructureException {   
  112.         Session s = (Session) threadSession.get();   
  113.         try {   
  114.             if (s == null) {   
  115.                 log.debug("Opening new Session for this thread.");   
  116.                 if (getInterceptor() != null) {   
  117.                     log.debug("Using interceptor: " + getInterceptor().getClass());   
  118.                     s = getSessionFactory().openSession(getInterceptor());   
  119.                 } else {   
  120.                     s = getSessionFactory().openSession();   
  121.                 }   
  122.                 threadSession.set(s);   
  123.             }   
  124.         } catch (HibernateException ex) {   
  125.             throw new InfrastructureException(ex);   
  126.         }   
  127.         return s;   
  128.     }   
  129.   
  130.     /**  
  131.      * Closes the Session local to the thread.  
  132.      */  
  133.     public static void closeSession()   
  134.         throws InfrastructureException {   
  135.         try {   
  136.             Session s = (Session) threadSession.get();   
  137.             threadSession.set(null);   
  138.             if (s != null && s.isOpen()) {   
  139.                 log.debug("Closing Session of this thread.");   
  140.                 s.close();   
  141.             }   
  142.         } catch (HibernateException ex) {   
  143.             throw new InfrastructureException(ex);   
  144.         }   
  145.     }   
  146.   
  147.     /**  
  148.      * Start a new database transaction.  
  149.      */  
  150.     public static void beginTransaction()   
  151.         throws InfrastructureException {   
  152.         Transaction tx = (Transaction) threadTransaction.get();   
  153.         try {   
  154.             if (tx == null) {   
  155.                 log.debug("Starting new database transaction in this thread.");   
  156.                 tx = getSession().beginTransaction();   
  157.                 threadTransaction.set(tx);   
  158.             }   
  159.         } catch (HibernateException ex) {   
  160.             throw new InfrastructureException(ex);   
  161.         }   
  162.     }   
  163.   
  164.     /**  
  165.      * Commit the database transaction.  
  166.      */  
  167.     public static void commitTransaction()   
  168.         throws InfrastructureException {   
  169.         Transaction tx = (Transaction) threadTransaction.get();   
  170.         try {   
  171.             if ( tx != null && !tx.wasCommitted()   
  172.                             && !tx.wasRolledBack() ) {   
  173.                 log.debug("Committing database transaction of this thread.");   
  174.                 tx.commit();   
  175.             }   
  176.             threadTransaction.set(null);   
  177.         } catch (HibernateException ex) {   
  178.             rollbackTransaction();   
  179.             throw new InfrastructureException(ex);   
  180.         }   
  181.     }   
  182.   
  183.     /**  
  184.      * Commit the database transaction.  
  185.      */  
  186.     public static void rollbackTransaction()   
  187.         throws InfrastructureException {   
  188.         Transaction tx = (Transaction) threadTransaction.get();   
  189.         try {   
  190.             threadTransaction.set(null);   
  191.             if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {   
  192.                 log.debug("Tyring to rollback database transaction of this thread.");   
  193.                 tx.rollback();   
  194.             }   
  195.         } catch (HibernateException ex) {   
  196.             throw new InfrastructureException(ex);   
  197.         } finally {   
  198.             closeSession();   
  199.         }   
  200.     }   
  201.   
  202.     /**  
  203.      * Reconnects a Hibernate Session to the current Thread.  
  204.      *  
  205.      * @param session The Hibernate Session to be reconnected.  
  206.      */  
  207.     public static void reconnect(Session session)   
  208.         throws InfrastructureException {   
  209.         try {   
  210.             session.reconnect();   
  211.             threadSession.set(session);   
  212.         } catch (HibernateException ex) {   
  213.             throw new InfrastructureException(ex);   
  214.         }   
  215.     }   
  216.   
  217.     /**  
  218.      * Disconnect and return Session from current Thread.  
  219.      *  
  220.      * @return Session the disconnected Session  
  221.      */  
  222.     public static Session disconnectSession()   
  223.         throws InfrastructureException {   
  224.   
  225.         Session session = getSession();   
  226.         try {   
  227.             threadSession.set(null);   
  228.             if (session.isConnected() && session.isOpen())   
  229.                 session.disconnect();   
  230.         } catch (HibernateException ex) {   
  231.             throw new InfrastructureException(ex);   
  232.         }   
  233.         return session;   
  234.     }   
  235.   
  236.     /**  
  237.      * Register a Hibernate interceptor with the current thread.  
  238.      * <p>  
  239.      * Every Session opened is opened with this interceptor after  
  240.      * registration. Has no effect if the current Session of the  
  241.      * thread is already open, effective on next close()/getSession().  
  242.      */  
  243.     public static void registerInterceptor(Interceptor interceptor) {   
  244.         threadInterceptor.set(interceptor);   
  245.     }   
  246.   
  247.     private static Interceptor getInterceptor() {   
  248.         Interceptor interceptor =   
  249.             (Interceptor) threadInterceptor.get();   
  250.         return interceptor;   
  251.     }   
  252.   
  253. }  
0

簡(jiǎn)單的filter類(lèi)如下:

Java代碼
 
  1. import org.apache.commons.logging.*;   
  2.   
  3. import javax.servlet.*;   
  4. import java.io.IOException;   
  5.   
  6. /**  
  7.  * A servlet filter that opens and closes a Hibernate Session for each request.  
  8.  * <p>  
  9.  * This filter guarantees a sane state, committing any pending database  
  10.  * transaction once all other filters (and servlets) have executed. It also  
  11.  * guarantees that the Hibernate <tt>Session</tt> of the current thread will  
  12.  * be closed before the response is send to the client.  
  13.  * <p>  
  14.  * Use this filter for the <b>session-per-request</b> pattern and if you are  
  15.  * using <i>Detached Objects</i>.  
  16.  *  
  17.  * @see HibernateUtil  
  18.  * @author Christian Bauer <christian@hibernate.org>  
  19.  */  
  20. public class HibernateFilter implements Filter {   
  21.   
  22.     private static Log log = LogFactory.getLog(HibernateFilter.class);   
  23.   
  24.     public void init(FilterConfig filterConfig) throws ServletException {   
  25.         log.info("Servlet filter init, now opening/closing a Session for each request.");   
  26.     }   
  27.   
  28.     public void doFilter(ServletRequest request,   
  29.                          ServletResponse response,   
  30.                          FilterChain chain)   
  31.             throws IOException, ServletException {   
  32.   
  33.         // There is actually no explicit "opening" of a Session, the   
  34.         // first call to HibernateUtil.beginTransaction() in control   
  35.         // logic (e.g. use case controller/event handler) will get   
  36.         // a fresh Session.   
  37.         try {   
  38.             chain.doFilter(request, response);   
  39.   
  40.             // Commit any pending database transaction.   
  41.             HibernateUtil.commitTransaction();   
  42.   
  43.         } finally {   
  44.   
  45.             // No matter what happens, close the Session.   
  46.             HibernateUtil.closeSession();   
  47.   
  48.         }   
  49.     }   
  50.   
  51.     public void destroy() {}   
  52.   
  53. }  



引入spring可能要改的東西比較多,JE知識(shí)庫(kù)里應(yīng)該有不少spring的教程。

因?yàn)椴恢滥闶欠裼袑?xiě)Hibernate工廠類(lèi),以下僅作參考
明白什么回事后很容易改

新建一個(gè)全局的過(guò)濾器

Java代碼
 
  1. package com.yourcomp;   
  2. import com.yourcomp.HibernateSessionFactory;   
  3. import javax.servlet.Filter;   
  4. //..各種import   
  5. public class HbnSessionFilter implements Filter{   
  6. public void doFilter(ServletRequest request, ServletResponse response,   
  7.  FilterChain chain) {   
  8.    try {   
  9.      chain.doFilter(request, response);     
  10.      HibernateSessionFactory.closeSession();     
  11.    }   
  12.    catch (Exception e) {   
  13.      e.printStackTrace();   
  14.    }   
  15. }   
  16. public void init(FilterConfig filterConfig) throws ServletException {}   
  17. public void destroy() {}   
  18. }  


web.xml中配置(望文生義,就不解釋了……)
Xml代碼
 
  1. <filter>  
  2.     <filter-name>hbnSessionFilter</filter-name>  
  3.     <filter-class>com.yourcomp.HbnSessionFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>hbnSessionFilter</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8. </filter-mapping>  

2009年3月17日 13:29

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
一個(gè)簡(jiǎn)單的Hibernate工具類(lèi)HibernateUtil
Spring源代碼解析(八):Spring驅(qū)動(dòng)Hibernate的實(shí)現(xiàn)
將Hibernate與Struts結(jié)合
hibernate 要點(diǎn)
Hibernate 工具類(lèi)
一個(gè)用myeclipse開(kāi)發(fā)hibernate的入門(mén)例子 - 我愛(ài)JAVA - BlogJ...
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服