package com.dzy.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 final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ssh框架实现登陆注册和数据的增删查改
共76个文件
jar:28个
xml:10个
class:10个
5星 · 超过95%的资源 需积分: 9 43 下载量 129 浏览量
2015-01-15
20:19:53
上传
评论 1
收藏 9.23MB RAR 举报
温馨提示
适用于初学者,代码结构便于理解,适合学习struts,hibernate和jsp的开发人员
资源推荐
资源详情
资源评论
收起资源包目录
admin.rar (76个子文件)
admin
.project 2KB
.mystrutsdata 240B
.mymetadata 288B
src
struts.xml 1KB
hibernate.cfg.xml 890B
com
dzy
service
Userservice.java 298B
Emservice.java 458B
action
Modifyaction.java 937B
Showaction.java 1KB
model
Emmodel.java 1KB
Usermodel.java 943B
Emmodel.hbm.xml 754B
Usermodel.hbm.xml 585B
dao
UserDAO.java 1KB
EmDAO.java 2KB
util
Hibernateutil.java 745B
HibernateSessionFactory.java 3KB
WebRoot
show.jsp 2KB
WEB-INF
classes
struts.xml 1KB
hibernate.cfg.xml 890B
com
dzy
service
Emservice.class 1KB
Userservice.class 740B
action
Modifyaction.class 2KB
Showaction.class 2KB
model
Emmodel.hbm.xml 754B
Usermodel.hbm.xml 585B
Emmodel.class 2KB
Usermodel.class 2KB
dao
UserDAO.class 2KB
EmDAO.class 2KB
util
Hibernateutil.class 1KB
HibernateSessionFactory.class 3KB
lib
servlet-api.jar 173KB
commons-lang3-3.1.jar 308KB
hibernate-commons-annotations.jar 65KB
javassist-3.9.0.GA.jar 583KB
cglib-2.2.jar 272KB
dom4j-1.6.1.jar 307KB
log4j-1.2.14.jar 359KB
slf4j-log4j12-1.5.8.jar 9KB
antlr-2.7.6.jar 433KB
jsp-api.jar 87KB
commons-collections-3.1.jar 546KB
freemarker-2.3.19.jar 909KB
hibernate-c3p0-4.2.2.Final.jar 42KB
commons-io-2.0.1.jar 156KB
c3p0-0.9.1.2.jar 596KB
commons-fileupload-1.2.2.jar 58KB
ehcache-1.2.3.jar 203KB
slf4j-api-1.5.8.jar 23KB
hibernate3.jar 2.31MB
struts2-core-2.3.14.jar 778KB
ejb3-persistence.jar 49KB
hibernate-annotations.jar 273KB
xwork-core-2.3.14.jar 622KB
mysql-connector-java-5.1.18-bin.jar 771KB
hibernate-entitymanager.jar 116KB
jta-1.1.jar 13KB
ognl-3.0.6.jar 223KB
hibernate-validator.jar 61KB
web.xml 691B
error.jsp 926B
index.jsp 1KB
zhuce.jsp 2KB
add.jsp 2KB
update.jsp 2KB
META-INF
MANIFEST.MF 36B
.myeclipse
.myhibernatedata 555B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.common.component 453B
.jsdtscope 500B
.classpath 2KB
共 76 条
- 1
资源评论
- qq_368247122017-12-18挺好的 时候复习和新手
- hjy80232015-06-10挺好的代码,它详细描述了如何实现增删改查
yanqiwen
- 粉丝: 2
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 王姿.html
- 51单片机学习(1)-软件keil下载
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功