您的位置:首页 > 其它

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还可以通过指定参数来传递:

下面看:
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息