package com.ajax.hibernate;
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;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
AJAX和JSF和EJB整合
共20个文件
xml:5个
class:3个
java:3个
需积分: 9 56 下载量 4 浏览量
2007-07-24
01:13:59
上传
评论
收藏 3.78MB RAR 举报
温馨提示
老师做的,比较详细,我加了注释,有2张PPT介绍,数据库我拷贝进去了,解压后直接使用,不懂的发我E-mail: zwl2007@163.com
资源推荐
资源详情
资源评论
收起资源包目录
JSF和EJB详细整合.rar (20个子文件)
JSF和EJB详细整合
JSF_EJB
.myhibernatedata 436B
src
com
sodi
hi
FaqCapital.java 1KB
FaqCapital.hbm.xml 781B
ajax
servlet
FaqServlet.java 2KB
hibernate
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 931B
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 636B
classes
com
sodi
hi
FaqCapital.hbm.xml 781B
FaqCapital.class 1KB
ajax
servlet
FaqServlet.class 2KB
hibernate
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 931B
lib
login.jsp 3KB
.mymetadata 294B
.classpath 501B
lib
sqljdbc.jar 247KB
.myeclipse
CVS.msi 3.82MB
JSF_EJB.BAK 915KB
共 20 条
- 1
资源评论
q9j7y1
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功