hibernate的Configuration类和SessionFactory接口
1,Configuration类的主要作用是解析Hibernate的配置文件和映射文件中的信息,即负责管理Hibernate的配置信息。Hibernate在运行时需要获取一些底层实现的基本信息,如数据库驱动类,数据路URL,数据库登录名,数据库登录密码等。
通过Configuration对象的buildSessionFactory()方法可创建SessionFactory对象之后,由于配置信息已经由Hibernate维护并绑定在返回的SessionFactory中,此时Configuration已没有价值。
<pre name="code" class="html"><pre name="code" class="java">Configuration configuration = new Configuration();
configuration.configure("/hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
我们看到:要创建一个Configuration,可以使用
Configuration config = new Configuration().configure(); 这里,Configuration是入口,通过它来获得配置文件。
同时Configuration还可以通过指定参数来传递:
下面看:
File file = new File("c:\\Hibernate.xml");
Configuration config = new Configuration().config(file);
2,SessionFactory接口
SessionFactory接口负责初始化Hibernate。它充当数据存储元的代理,并负责创建Session对象。通过Configuration实例构建SessionFactory对象。
sessionFactory = configuration.buildSessionFactory();
SessionFactory是线程安全的,可以被多个线程调用。在我们实际开发中,我们可以在初始化的部分构造一个SessionFactory即可 。多数情况下,一个应用只初始化一个SessionFactory,为不同的线程提供Session。
下面是myeclipse添加Hibernate框架时,自动创建的HibernateSessionFactory.java文件。文件中有如下方法:
package SessionFactory;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<Session> threadLocal = new ThreadLocal<Session>();private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory;private static String configFile = CONFIG_FILE_LOCATION;static {try {configuration.configure(configFile);sessionFactory = configuration.buildSessionFactory();} catch (Exception e) {System.err.println("%%%% Error Creating SessionFactory %%%%");e.printStackTrace();}}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;}}
hibernate的Configuration类和SessionFactory接口相关推荐
- hibernate的Configuration和配置文件
一.hibernate的入口Configuration 在Hibernate中,Configuration是hibernate的入口.在实例化一个Configuration的时候,Hibernate会 ...
- Hibernate学习(二):heibernate核心接口
Hibernate是一种对JDBC做了轻量级封装的对象---关系映射工具,所谓轻量级封装,是指Hibernate并没有完全封装JDBC,Java应用即可以通过Hibernate API访问数据库,还可 ...
- Hibernate框架--学习笔记(上):hibernate项目的搭建和常用接口方法、对象的使用
一.什么是Hibernate框架: 1.Hibernate是一个操作数据库的框架,实现了对JDBC的封装: 2.Hibernate是一个ORM(对象关系映射)框架,我们在写程序时 ,用的是面向对象的方 ...
- hibernate之工具类
package util;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate ...
- Hibernate之工具类HibernateUtil
原创文章,转载请注明:Hibernate之工具类HibernateUtil By Lucio.Yang 1.最简单的工具类,实现SessionFactory的单例共享,session的管理 pack ...
- How to use Hibernate - XML Configuration
使用Hibernate进行开发时,有两种方式,一种是使用XML配置,这是比较传统的方式.另一种是使用注解,这是JPA标准所支持的.今天介绍得是使用XML配置的方式使用Hibernate.具体使用方式如 ...
- 如何用MyBatis-Generator自动创建代码(映射生成实体类、DAO接口和Mapping映射文件)
如何用MyBatis自动生成实体类.DAO接口和Mapping映射文件 引言: 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBa ...
- hibernate高级工具类(含分页)
最近笔者在研究一套系统(jeeCMS)的源码,看到他对于hibernate的封装,顿时生出一种惊为天人的感觉,特与诸君分享. 这里面用到了两个基础知识,笔者在这里列一下,对于hibernate不熟悉读 ...
- hibernate mysql 自动建表_配置hibernate根据实体类自动建表功能
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码update upda ...
最新文章
- 要看懂MATLAB的Help需要积累的英文词汇!
- 基于单目视觉的智能车辆视觉导航系统设计
- golang mac 环境变量_macOS 配置 golang 运行环境
- 在2017年从Maven工件生成P2存储库
- 前端 学习笔记day47 其他标签
- transform,translate,transition 的区别
- ArcGIS API for JavaScript——给图层添加标注
- C#利用CDOSYS组件发邮件的一些小结
- C# 委托与事件(delegate)
- python基础---文件处理
- logstash nginx error access 日志处理
- 【图像边缘检测】基于matlab最小二乘法椭圆边缘检测【含Matlab源码146期】
- 如何将PDF转化成Word格式
- ChatGPT4.0中国怎么使用
- HAL库配置STM32F1系列定时器驱动步进电机(三)
- vcruntime140d.dll丢失的解决方法_vcruntime140d.dll修复工具下载
- SAP增强总结-第四代增强(BTE实例详解)
- 公众号显示IP归属地,有多少人会现出原形?
- 合肥计算机专业大学排名23,计算机专业大学排名.doc
- MFC简单自学图形绘制1
热门文章
- C++ Primer 5th笔记(chap 18 大型程序工具)多重继承下的类作用域
- python 语言教程(2)基础语法
- java元婴期(26)----java进阶(mybatis(5)---spring和mybatis整合(重点)逆向工程(会用))
- Kubernetes 中创建 Pod 时集群中到底发生了些什么?
- 设计模式--命令(Command)模式
- node + express + mongodb 手动配置
- 134. 加油站(贪心算法)
- 可信平台模块(TPM)概念介绍
- project1两周收获总结
- Windows保护模式学习笔记(十四)—— 阶段测试