<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 崛起的程序員 閱讀(686) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 伊人久久大香线蕉亚洲| 亚洲精品在线免费观看视频| 亚洲人成在线中文字幕| 中文字幕日本人妻久久久免费| 亚洲人成色7777在线观看不卡| 鲁死你资源站亚洲av| 在线观看亚洲免费| 亚洲av永久无码一区二区三区| 日本特黄特色免费大片| 特级毛片A级毛片免费播放| 亚洲成a人片在线观看日本麻豆 | 免费A级毛片无码A∨免费| 久久精品视频亚洲| 99爱在线精品视频免费观看9| 最近最好最新2019中文字幕免费| 亚洲av无码专区国产乱码在线观看| a级毛片免费全部播放| 亚洲av永久无码精品国产精品| 在线观看肉片AV网站免费| 亚洲午夜久久久精品影院| 美女视频黄免费亚洲| 亚洲精品伦理熟女国产一区二区 | 久久久久亚洲AV无码麻豆| 8888四色奇米在线观看免费看| 亚洲精品成人图区| 破了亲妺妺的处免费视频国产| 日韩一区二区三区免费播放| 亚洲国产精品无码专区影院| 国产成人yy免费视频| 亚洲AV无码AV吞精久久| 国产亚洲精品资源在线26u| 亚洲w码欧洲s码免费| 亚洲av无码片vr一区二区三区| 亚洲男人的天堂一区二区| 久久青草精品38国产免费| 四虎亚洲精品高清在线观看| xvideos亚洲永久网址| 日韩内射激情视频在线播放免费| 亚洲色成人网站WWW永久四虎| 亚洲人成无码www久久久| 色老头永久免费网站|