作为简单的一个单件模式,
getSessionFactory()函数返回null是一个不合理的决定,可以返回一个没有初始化的SessionFactory对象,但是返回null就和这种模式的语意有冲突了。
 
以下是用myeclipse5自动生成的代码:
 
package model.sessionfatory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
   
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
 private static final ThreadLocal threadLocal = new ThreadLocal();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;
    private HibernateSessionFactory() {
    }
 
 
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
  if (session == null || !session.isOpen()) {
   if (sessionFactory == null) {
    rebuildSessionFactory();
   }
   session = (sessionFactory != null) ? sessionFactory.openSession()
     : null;
   threadLocal.set(session);
  }
        return session;
    }
 
 public static void rebuildSessionFactory() {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err
     .println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
 }
 
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);
        if (session != null) {
            session.close();
        }
    }
 
 public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
 }
 
 public static void setConfigFile(String configFile) {
  HibernateSessionFactory.configFile = configFile;
  sessionFactory = null;
 }
 
 public static Configuration getConfiguration() {
  return configuration;
 }
}
 
里边的这个函数:
 public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
 }
 
是否应该修改为:
 public static org.hibernate.SessionFactory getSessionFactory() {
  rebuildSessionFactory();
  return sessionFactory;
 }
 
这样调用代码:
  dao.setSessionFactory(HibernateSessionFactory.getSessionFactory());
会报错误:
java.lang.IllegalArgumentException: sessionFactory is required
 at org.springframework.orm.hibernate3.HibernateAccessor.afterPropertiesSet(HibernateAccessor.java:318)
 at org.springframework.orm.hibernate3.HibernateTemplate.<init>(HibernateTemplate.java:147)
 at org.springframework.orm.hibernate3.support.HibernateDaoSupport.createHibernateTemplate(HibernateDaoSupport.java:83)
 at org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(HibernateDaoSupport.java:70)
 at test.TestOrgTypeDAO.testFindById(TestOrgTypeDAO.java:66)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at junit.framework.TestCase.runTest(TestCase.java:154)
 at junit.framework.TestCase.runBare(TestCase.java:127)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at junit.framework.TestSuite.runTest(TestSuite.java:208)
 at junit.framework.TestSuite.run(TestSuite.java:203)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
 
而这样调用却不会错误:
  HibernateSessionFactory.rebuildSessionFactory();
  dao.setSessionFactory(HibernateSessionFactory.getSessionFactory());

使用myeclipse生成的HibernateSessionFactory的合理性的更多相关文章

  1. MyEclipse生成注册码

    今天正在使用的MyEclipse出现了使用过期,在网上发现一个可以生成注册码的程序,现在分享给各位. /** * myEclipse生成注册码 * 点击顶部:MyEclipse --> subs ...

  2. [转]myeclipse 生成JAR包并引入第三方包

    myeclipse 生成JAR包并引入第三方包 我用的是myeclipse8.0 首先用myeclipse生成JAR 一.生成JAR包 1.点选项目右键—>Export 2.Java—>J ...

  3. Eclipse/Myeclipse生成serialVersionUID方法

    serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 如果你修改代码重新部署后出现序列化错误,可以考虑给相应的类增加serialVersio ...

  4. MyEclipse 生成APK文件

    MyEclipse 生成APK文件 Android Tools>Export  Signed  Application Packages. http://jingyan.baidu.com/ar ...

  5. eclipse&myeclipse 生成jar包后,spring无法扫描到bean定义

    问题:eclipse&myeclipse 生成jar包后,spring无法扫描到bean定义 在使用getbean或者扫包时注入bean失败,但在IDE里是可以正常运行的? 原因:导出jar未 ...

  6. (转)使用myeclipse生成实体类和hibernate映射文件

    转至:http://blog.sina.com.cn/s/blog_9658bdb40100uiod.html 1.下载并安装myeclipse,如果已经安装,则忽略该步骤; 2.打开myeclips ...

  7. 通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

    1.   前言 很多人都在使用myEclipse,很多公司也都使用hibernate框架,老版本的hibernate中,由于没有annotation,我们需要写两个文件来维护表与对象的关系,写一个类, ...

  8. 使用MyEclipse生成实体类和Hibernate映射文件

    1.打开MyEclipse DataBase Explorer. 如上图,点击图中2便可切换到MyEclipse DataBase Explorer,若没有该图标,则点击图中1,便可选择MyEclip ...

  9. 1、MyEclipse插件配置以及通过MyEclipse生成表对应的JPA代码

     去除MyEclipse插件的方式是打开:WindowàCustomize Perspective窗口进行插件配置: 取出下图中不常用的插件勾,最终点击OK. 3.点击OK之后显示的效果图如下: ...

随机推荐

  1. IOS开发UI篇-NavigationController的基本使用

    一.简介 一个iOS的app很少只由一个控制器组成,除非这个app极其简单 当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小v ...

  2. LeeCode-Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 /** * Definition for a ...

  3. postgresql基本语句

    preface,熟悉pgsql sql Language article disorder; 1,pgsql数据库控制台Cli(command line interface) help mannual ...

  4. (step6.1.3)hdu 1875(畅通工程再续——最小生成树)

    题目大意:本题是中文题,可以直接在OJ上看 解题思路:最小生成树 1)本题的关键在于把二维的点转化成一维的点 for (i = 0; i < n; ++i) { scanf("%d%d ...

  5. hdu2208之搜索

    唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. JavaWeb——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  7. Letter of application, e-mail version

    Subject line: (logical to recipient!) Application for sales representative for mid-Atlantic area Apr ...

  8. Linux下重要日志文件及查看方式

    http://os.51cto.com/art/201108/282184_all.htm   1.Linux下重要日志文件介绍 /var/log/boot.log 该文件记录了系统在引导过程中发生的 ...

  9. JQ 事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. [原创] Assistant editor 取消拖拽 binding 的 UI 元素

    1. press up-right button "show the utilities" 2. press "show the Connections inspecto ...