package org.mytech.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 = "/org/mytech/hibernate/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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
action返回list,页面显示下拉列表.rar (49个子文件)
action返回list,页面显示下拉列表
test1.sql 5KB
test1
.mystrutsdata 257B
.myhibernatedata 427B
src
org
mytech
ProInfo.hbm.xml 673B
Login.java 1KB
ProInfo.java 793B
Login.hbm.xml 802B
hibernate
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 936B
DAO
DAO.java 1KB
struts
ApplicationResources.properties 204B
action
LoginAction.java 3KB
Show_proNameAction.java 1KB
.project 1KB
WebRoot
form
login.jsp 582B
show_proName.jsp 816B
META-INF
MANIFEST.MF 39B
WEB-INF
struts-nested.tld 70KB
validator-rules.xml 13KB
struts-bean.tld 9KB
struts-html.tld 72KB
struts-logic.tld 14KB
struts-tiles.tld 8KB
web.xml 1KB
.struts-config.mex 1KB
classes
org
mytech
ProInfo.hbm.xml 673B
Login.hbm.xml 802B
hibernate
hibernate.cfg.xml 936B
HibernateSessionFactory.class 2KB
ProInfo.class 890B
DAO
DAO.class 2KB
Login.class 1KB
struts
ApplicationResources.properties 204B
action
LoginAction.class 3KB
Show_proNameAction.class 987B
struts-config.xml 1KB
lib
commons-beanutils.jar 184KB
mssqlserver.jar 63KB
jakarta-oro.jar 64KB
commons-validator.jar 82KB
msbase.jar 278KB
commons-fileupload.jar 22KB
msutil.jar 58KB
commons-logging.jar 37KB
struts.jar 531KB
antlr.jar 350KB
commons-digester.jar 164KB
.mymetadata 288B
.classpath 1KB
.myeclipse
共 49 条
- 1
资源评论
- XUANWODONGDONG2013-07-07找不到想要的点子,不过还是谢谢楼主
- XMonsterH2013-05-22可能是好东西,但我只是想要动态select的例子
szw19841217
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功