HibernateSessionFactory

package org.util;

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;

}

}

时间: 2024-10-06 09:48:33

HibernateSessionFactory的相关文章

HibernateSessionFactory的用法

1-使用系统自带的HibernateSessionFactory类,该类是优秀的factory类.可以利用该类里的方法来完成我们一些需求 2-查询一条记录,查询多条记录 新建HibernateSessionFactory类.Myeclipse软件中,window-->openperpective-->myeclipsehibernate选择该方式显示窗口,就可以在相应的目录下新建HibernateSessionFactory类. 建立了HibernateSessionFactory类后就可以使

HibernateSessionFactory演示样例

package common; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory { private static Configuration cfg; private stati

Hibernate4.0之HibernateSessionFactory源码详解

import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; @SuppressWarnings("deprecation") pu

hibernate3和4中 HibernateSessionFactory中不同之处 The method applySettings(Map) from the type ServiceRegistryBuilder is deprecated - The type ServiceRegistryBuilder is deprecated

hibernate3和4中 HibernateSessionFactory中不同之处 //serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 新的写法 serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProper

HibernateSessionFactory建立-使用ThreadLocal

立即加载还是延迟加载必须要连接数据库的,而在Java中连接数据库是依赖java.sql.Connection,在hibernate中session就是Connection的一层高级封装,一个session对应了一个Connection,要实现延迟加载必须有session才行.而且要进行延迟加载还必须保证是同一个session才行,用另外一个session去延迟加载前一个session的代理对象是不行的.大家都知道Connection是使用过后必须要进行关闭的,那么我们如何保证一次http请求过程

HibernateSessionFactory示例

package common; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory { private static Configuration cfg; private stati

Java EE之Hibernate的HibernateSessionFactory

昨天,一下午都被一个bug缠身,最后逐层排查,发现是MyEclipse 2014自动生成的HibernateSessionFactory有问题.后观察网友提供的自动生成的HibernateSessionFactory也不尽一样,难道,这就有些奇怪了,由于课程项目时间紧张,这学期时间过得太紧巴,此bug的原因暂不讨论,以下先贴上正确的HibernateSessionFactory的设计,再贴上错误的HibernateSessionFactory(且很奇怪的是,我做Hibernate的映射demo的

hibernate 之 HQL语句总结【转】

1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String hql = "from Users"; Query query = session.createQuery(hql); List<Users> users = query.list(); for(Users user : users){ System.out.println(user.getName() + " : " + user.ge

Hibernate 的 session.load()使用方法

protected Person getOne(int id){ Session session = HibernateSessionFactory.getSession(); Person person = new Person(); try{ session.load(person, id); }catch(Exception e){ e.printStackTrace(); }finally{ session.close(); } return person; }