<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-88  評論-77  文章-48  trackbacks-0

    package com.zte.hibernate.dao;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;

    import com.zte.hibernate.domain.Dept;
    import com.zte.hibernate.domain.Emp;

    /**
    ?* Configures and provides access to Hibernate sessions, tied to the
    ?* current thread of execution.? Follows the Thread Local Session
    ?* pattern, see {@linkhttp://hibernate.org/42.html}.
    ?*/
    public class HibernateSessionFactory {

    ??? /**
    ???? * Location of hibernate.cfg.xml file.
    ???? * NOTICE: Location should be on the classpath as Hibernate uses
    ???? * #resourceAsStream style lookup for its configuration file. That
    ???? * is place the config file in a Java package - the default location
    ???? * is the default Java package.<br><br>
    ???? * Examples: <br>
    ???? * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
    ???? * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
    ???? */
    ??? private static String CONFIG_FILE_LOCATION = "/com/zte/hibernate/dao/hibernate.cfg.xml";

    ??? /** Holds a single instance of Session */
    ?private static final ThreadLocal threadLocal = new ThreadLocal();

    ??? /** The single instance of hibernate configuration */
    ??? private static final Configuration cfg = new Configuration();

    ??? /** The single instance of hibernate SessionFactory */
    ??? private static org.hibernate.SessionFactory sessionFactory;

    ??? /**
    ???? * Returns the ThreadLocal Session instance.? Lazy initialize
    ???? * the <code>SessionFactory</code> if needed.
    ???? *
    ???? *? @return Session
    ???? *? @throws HibernateException
    ???? */
    ??? public static Session currentSession() throws HibernateException {
    ??????? Session session = (Session) threadLocal.get();
    ??????? Properties p = new Properties();
    ??????? InputStream in;
    ??try {
    ???in = new FileInputStream("src/hibernate.properties");
    ???p.load(in);
    ??} catch (FileNotFoundException e1) {
    ???// TODO Auto-generated catch block
    ???e1.printStackTrace();
    ??} catch (IOException e) {
    ???// TODO Auto-generated catch block
    ???e.printStackTrace();
    ??}
    ???????
    ??if (session == null || !session.isOpen()) {
    ???if (sessionFactory == null) {
    ????try {
    ?????cfg.setProperties(p);
    ?????cfg.addClass(Dept.class);
    ?????cfg.addClass(Emp.class);
    ?????//cfg.configure(CONFIG_FILE_LOCATION);
    ?????sessionFactory = cfg.buildSessionFactory();
    ????} catch (Exception e) {
    ?????System.err
    ???????.println("%%%% Error Creating SessionFactory %%%%");
    ?????e.printStackTrace();
    ????}
    ???}
    ???session = (sessionFactory != null) ? sessionFactory.openSession()
    ?????: null;
    ???threadLocal.set(session);
    ??}

    ??????? return session;
    ??? }

    ??? /**
    ???? *? Close the single hibernate session instance.
    ???? *
    ???? *? @throws HibernateException
    ???? */
    ??? public static void closeSession() throws HibernateException {
    ??????? Session session = (Session) threadLocal.get();
    ??????? threadLocal.set(null);

    ??????? if (session != null) {
    ??????????? session.close();
    ??????? }
    ??? }

    ??? /**
    ???? * Default constructor.
    ???? */
    ??? private HibernateSessionFactory() {
    ??? }

    }

    posted on 2006-05-18 16:50 崛起的程序員 閱讀(679) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 鲁丝片一区二区三区免费| 亚洲免费视频一区二区三区| 免费不卡在线观看AV| 亚洲国产精品国自产拍AV| 最好免费观看高清在线| 亚洲精品无码久久久久sm| 永久在线观看免费视频 | 国产日产亚洲系列最新| 国产精品偷伦视频免费观看了| 久久亚洲国产精品123区| 久久免费香蕉视频| 亚洲AV人无码综合在线观看| 久久国产乱子免费精品| 亚洲人成人77777在线播放| 免费无码又爽又刺激高潮| 精品久久久久久亚洲中文字幕| 亚洲精品无码专区2| 国产精品高清免费网站| 久久亚洲精品成人| jjizz全部免费看片| 亚洲色少妇熟女11p| 亚洲AV无码专区日韩| 免费在线观影网站| 91亚洲国产成人久久精品网址| 在线不卡免费视频| www永久免费视频| 久久青青草原亚洲av无码app | 91香蕉国产线观看免费全集| 亚洲精品在线电影| 成人看的午夜免费毛片| 免费高清A级毛片在线播放| 亚洲国产无套无码av电影| 97热久久免费频精品99| 国产综合激情在线亚洲第一页| 亚洲中文字幕日产乱码高清app| 在线看无码的免费网站| 免费一级做a爰片久久毛片潮| 久久亚洲国产成人精品性色| 日韩人妻无码免费视频一区二区三区 | 三年片在线观看免费观看大全动漫| 亚洲精品一二三区|