<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
    主站蜘蛛池模板: 国产h肉在线视频免费观看| 国产美女视频免费观看的网站| 99re免费在线视频| 亚洲AV成人精品网站在线播放| 西西人体免费视频| 久久精品国产亚洲AV果冻传媒| 男人进去女人爽免费视频国产| 国产精品久久久亚洲| 免费国产叼嘿视频大全网站| 亚洲成熟xxxxx电影| 久久久久av无码免费网| 亚洲熟妇无码八V在线播放| 麻豆成人精品国产免费| 日韩在线观看免费| 91麻豆国产自产在线观看亚洲| 国产真人无码作爱视频免费| 蜜芽亚洲av无码精品色午夜| 无码国产精品一区二区免费I6| 亚洲乱妇老熟女爽到高潮的片| 国产乱弄免费视频| 一级毛片大全免费播放| 久久精品国产精品亚洲艾| 可以免费看的卡一卡二| 国产亚洲高清在线精品不卡| 亚洲无人区午夜福利码高清完整版| 毛片在线播放免费观看| 亚洲香蕉在线观看| 亚洲人妻av伦理| av无码免费一区二区三区| 久久人午夜亚洲精品无码区| 国产AV无码专区亚洲AWWW| **真实毛片免费观看| 亚洲免费网站观看视频| 亚洲国产另类久久久精品黑人| 国产成人福利免费视频| 特级毛片全部免费播放| 亚洲精品在线播放视频| 亚洲国产精品成人网址天堂| 精品一区二区三区无码免费视频| 亚洲国产AV一区二区三区四区| 国产亚洲综合成人91精品 |