hibernate的Configuration类和SessionFactory接口
2015-11-06 15:55
351 查看
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还可以通过指定参数来传递:
下面看:
2,SessionFactory接口
SessionFactory接口负责初始化Hibernate。它充当数据存储元的代理,并负责创建Session对象。通过Configuration实例构建SessionFactory对象。
sessionFactory = configuration.buildSessionFactory();SessionFactory是线程安全的,可以被多个线程调用。在我们实际开发中,我们可以在初始化的部分构造一个SessionFactory即可。多数情况下,一个应用只初始化一个SessionFactory,为不同的线程提供Session。
下面是myeclipse添加Hibernate框架时,自动创建的HibernateSessionFactory.java文件。文件中有如下方法:
![](https://img-blog.csdn.net/20151106155338442?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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;
}
}
通过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 Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
- 深入理解Hibernate中的flush机制
- 简单的手工hibernate程序示例
- 解析使用jdbc,hibernate处理clob/blob字段的详解
- 浅析java程序中hibernate的应用总结
- java面试常见问题之Hibernate总结
- 关于Hibernate的一些学习心得总结
- Hibernate管理Session和批量操作分析
- 使用Hibernate