作为简单的一个单件模式,
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. scheme代码高亮

    (defun iedit-symbol-in-defun () "Enter `iedit-mode' to rename the symbol in current function, o ...

  2. 从QWidget继承的圆形进度条QRoundProgressBar,只有370行代码(别忘了有现成的QProgressDialog)

    是学习绘制控件的绝佳例子: http://blog.csdn.net/liang19890820/article/details/51957568 https://sourceforge.net/pr ...

  3. ownCloud Virtual Machines(bitnami.com)

    ownCloud Virtual Machines(bitnami.com)https://bitnami.com/stack/owncloud/virtual-machine

  4. Socket也有专门的Unicode版本

    https://www.chilkatsoft.com/refdoc/wcppCkSocketWRef.html https://www.chilkatsoft.com/refdoc/vcCkSock ...

  5. 使用 asp.net Web API 2的坑

    使用工具: Googl  浏览器+PostMan 插件 写了个  控制器 添加了个Action,结果呢?GET 方式请求没问题. POST一直,在服务器端获取不了参数...找了官方的文档 .各种雨里雾 ...

  6. You don't seem to have 'make' or 'gmake' in your PATH

    标题尚的错误,不是原因,因为which make的时候是有显示/usr/bin/make的,echo $PATH的时候也是有make的路径的. 编译Qt  ./configure的时候出现标题上错误, ...

  7. Android EditText setOnClickListener事件 只有获取焦点才能响应 采用setOnTouchListener解决

    最近在学习Android开发,在编写程序的过程中,发现EditText setOnClickListener事件响应中,只有获取焦点的时候才会响应, 如当焦点在别的控件上时,只能先点击获取焦点,第二次 ...

  8. MATLAB三维曲面

    今天终于测试了,发下来第一张试卷中只会做一小题.我蒙了!!! 所以呢,我现在再做一下,总结总结! 作函数 f(x)=2(x1-1)4+2x22 的三维图. 这道题要用到的知识点有函数meshgrid. ...

  9. 剑指offer-面试题5.从尾到头打印链表

    题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. 刚看到这道题的小伙伴可能就会想,这还不简单,将链表反转输出. 但是这种情况破坏了链表的结构. 如果面试官要求不破坏链表结构呢,这时候我们 ...

  10. Easy Number Challenge(暴力,求因子个数)

    Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...