`
cesar2008
  • 浏览: 41164 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

使用myeclipse生成的HibernateSessionFactory的合理性

 
阅读更多
作为简单的一个单件模式,
getSessionFactory()函数返回null是一个不合理的决定,可以返回一个没有初始化的SessionFactory对象,但是返回null就和这种模式的语意有冲突了。

以下是用myeclipse5自动生成的代码:

package model.sessionfatory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution.  Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
    /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses 
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.  
     */
    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() {
    }

/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    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;
    }
/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err
     .println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
}
/**
     *  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();
        }
    }
/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
}
/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
  HibernateSessionFactory.configFile = configFile;
  sessionFactory = null;
}
/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
  return configuration;
}
}

里边的这个函数:
/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
}

是否应该修改为:
/**
     *  return session factory
     *
     */
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());
分享到:
评论
3 楼 温柔一刀 2007-04-23  
这代码看着真累,用代码编辑器编辑下吧

既然用了SPRING ,就不需要生成HibernateSessionFactory了
2 楼 温柔一刀 2007-04-23  
这代码看着真累,用代码编辑器编辑下吧

既然用了SPRING ,就不需要生成HibernateSessionFactory了
1 楼 codeutil 2007-04-23  

spring 配置 SessionFactory不是用 HibernateSessionFactory 的!!!

相关推荐

    myeclipse自动生成hibernate映射文件

    本文将详细介绍如何在MyEclipse集成开发环境中利用内置功能自动生成Hibernate的映射文件,从而提高开发效率。 Hibernate是一款流行的Java ORM框架,它允许开发者用面向对象的方式来操作数据库,而无需编写大量的SQL...

    MyEclipse_Hibernate_快速入门中文版.rar

    - **生成SQL**:使用HQL(Hibernate Query Language)或Criteria API编写查询,MyEclipse能自动生成对应的SQL语句。 - **数据浏览与操作**:通过Session对象,实现增删改查操作,MyEclipse提供图形化界面辅助操作...

    MyEclipse Hibernate快速入门中文版

    【MyEclipse Hibernate快速入门中文版】是一份旨在帮助初学者快速掌握MyEclipse集成环境下的Hibernate框架使用的教程。Hibernate是一个强大的Java持久化框架,它简化了数据库与Java对象之间的交互,使得开发者能够...

    SSH整合 myeclipse10

    例如,可以使用“New -&gt; Struts2 Action Class”创建Struts2的Action,使用“New -&gt; Hibernate Utility”生成Hibernate的配置和映射文件。这些向导可以帮助快速初始化项目,减少手动编写代码的工作量。 总的来说,...

    hibernate上级学习资料

    MyEclipse等IDE通常提供辅助功能,自动生成Hibernate所需的配置文件和基础类,如SessionFactory的创建类。 四、反向工程与映射文件 在数据库视图下,可以使用反向工程功能,根据已有数据库结构生成Java实体类和对应...

    jsf+spring+hibernate.pdf

    注意,如果使用MyEclipse自动生成的功能,可能出现asm-2.3.1.jar的冲突,建议使用指定版本的lib以避免此类问题。 接下来,配置web.xml文件。这是Java EE应用的核心配置文件,它定义了应用程序的上下文、监听器、...

    spring_struts_hibernate实例开发

    - **DAO层改造**: 使用Spring的反向工程功能生成Spring Hibernate DAO层代码。 - **事务管理器**: 配置Spring的事务管理器来统一管理事务。 - **事务代理**: 为DAO层添加Spring的事务代理,确保事务能够被正确地管理...

    Struts2_Spring_Hibernate集成

    - 通过MyEclipse的反向工程功能生成实体类和对应的Hibernate映射文件。 - 配置映射文件的细节,例如IDGenerator选择“native”。 ##### 9. 创建视图层 - 设计视图层逻辑,如使用JSP或Freemarker模板引擎展示数据。...

    Struts2.1、Spring3.0、Hibernate3.3框架整合详细图解

    在整合时,我们选择Hibernate的版本并配置其核心库和Annotation支持,根据需求决定是否使用XML映射文件。Hibernate配置文件通常与Spring配置文件集成,以由Spring管理SessionFactory。通过反向工程,可以自动生成与...

    Spring+Struts+hibernate 环境搭建

    使用Hibernate反转工程工具,自动生成实体类(Pojo)和映射文件(hbm.xml),简化数据库操作的代码编写。 7. **DAO接口和实现**: 创建DAO接口,命名通常为Pojo名称加DAO后缀。接口中定义数据库操作方法,然后...

    Struts+Spring+Hibernate整合笔记

    在持久化类生成时,可以选择是否生成DAO和Mapping文件,以及是否使用Spring DAO模式。Spring DAO模式中,HibernateTemplate对象代替了直接的Session操作,简化了数据库访问,同时也将事务管理集成到Spring中。 总的...

    Xfire配置Web Service+Spring+Hibernate详细流程

    - 配置实体类的主键生成策略,如自动递增、UUID等,确保数据的唯一性。 10. **集成配置** - 要实现Xfire与Spring的集成,主要通过修改配置文件实现。有两种方式: - 直接在Spring配置文件中集成,仅需`web.xml`...

    v512工作室_刘伟_Hibernate与Struts2和Spring组合开发

    通过MyEclipse或其他IDE提供的向导功能,自动生成POJO类及其对应的映射文件。这些映射文件定义了实体类与数据库表之间的映射关系,是Hibernate执行CRUD操作的基础。 ##### 5. 设计DAO层 - **DAO接口设计**:定义...

    javaSSHtrutsSpringHibernate大框架整合详细图解.docx

    在Hibernate反向工程中,选择表,设置生成目录和配置,自动生成对应的Java类和映射文件`hbm.xml`。 整合SSH框架的主要目的是实现各层之间的松耦合,提供更高效的工作流程。Struts处理用户交互,Spring管理对象和...

    完整的论坛项目

    3. **Hibernate框架**:掌握实体类的创建,配置映射文件,使用SessionFactory和Session进行数据操作,了解HQL(Hibernate Query Language)和Criteria查询。 4. **MyBatis框架**:如果项目中使用了MyBatis,那么会...

Global site tag (gtag.js) - Google Analytics