<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在线| 亚洲精品乱码久久久久久中文字幕| 男女超爽视频免费播放| 成在线人永久免费视频播放| 亚洲爆乳无码专区www| 在线观看永久免费视频网站| 国产精品亚洲精品爽爽| 亚洲国产aⅴ综合网| 国产免费内射又粗又爽密桃视频 | 亚洲高清在线观看| 久久精品国产亚洲av高清漫画| 亚洲精品9999久久久久无码| 好大好硬好爽免费视频| 亚洲爆乳少妇无码激情| 亚洲国产精品一区二区第一页免| 五级黄18以上免费看| 亚洲无线码在线一区观看| 久久久久久久久久国产精品免费| 女人18毛片水最多免费观看| 黄色三级三级三级免费看| 亚洲精品国产精品乱码不卡√ | 亚洲 欧洲 视频 伦小说| 免费观看的毛片手机视频| 猫咪免费观看人成网站在线| 亚洲乱码无码永久不卡在线| 亚洲免费二区三区| 风间由美在线亚洲一区| 亚洲国产成人片在线观看| 在线看片免费不卡人成视频| 精品一区二区三区免费毛片| 亚洲成AV人片在线观看无| 人禽杂交18禁网站免费| 久久免费香蕉视频| 亚洲成A人片在线播放器| 亚洲尤码不卡AV麻豆| 成年人视频在线观看免费 | 三年片在线观看免费大全电影|