package com.zjl.hi;
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 threadLocal = new ThreadLocal();
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
dwr框架+struts+Hibrenate实现下拉菜单二级联动
共30个文件
xml:8个
class:5个
java:5个
5星 · 超过95%的资源 需积分: 10 20 下载量 185 浏览量
2008-11-22
22:09:46
上传
评论
收藏 661KB RAR 举报
温馨提示
dwr框架+struts+Hibrenate实现下拉菜单二级联动,有注释,应用方便
资源详情
资源评论
资源推荐
收起资源包目录
LDTest.rar (30个子文件)
LDTest
.project 1KB
.settings
org.eclipse.jdt.core.prefs 334B
.mymetadata 291B
WebRoot
WEB-INF
dwr.xml 344B
web.xml 751B
lib
sqljdbc.jar 227KB
dwr.jar 478KB
classes
log4j.properties 633B
com
zjl
dao
BaseHibernateDAO.class 1KB
hi
HibernateSessionFactory.class 2KB
service
Test.class 2KB
entity
Position.class 1KB
PositionType.class 910B
PositionType.hbm.xml 687B
Position.hbm.xml 796B
hibernate.cfg.xml 963B
index.jsp 2KB
META-INF
MANIFEST.MF 39B
.myeclipse
src
log4j.properties 633B
com
zjl
dao
BaseHibernateDAO.java 975B
hi
HibernateSessionFactory.java 3KB
service
Test.java 2KB
entity
Position.java 879B
PositionType.java 697B
PositionType.hbm.xml 687B
Position.hbm.xml 796B
hibernate.cfg.xml 963B
SQLQuery1.sql 991B
.classpath 692B
.myhibernatedata 475B
共 30 条
- 1
junlizhang
- 粉丝: 3
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1