package org.jiao.entity;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
J2EE-Struts2-Spring-Hibernate整合的例子:添加
共73个文件
jar:39个
xml:9个
class:7个
需积分: 9 19 下载量 168 浏览量
2010-07-22
03:28:28
上传
评论
收藏 19.35MB RAR 举报
温馨提示
这是一个J2EE-Struts2-Spring-Hibernate整合的例子:添加,采用现比较流行的一些技术(struts+hibernate+spring)
资源推荐
资源详情
资源评论
收起资源包目录
J2EE-Struts2-Spring-Hibernate整合的例子:添加.rar (73个子文件)
Test2
.springBeans 320B
.myhibernatedata 471B
src
struts.xml 502B
org
jiao
dao
StudentDAO.java 138B
impl
StudentDAOImpl.java 562B
biz
StudentBiz.java 138B
impl
StudentBizImpl.java 484B
web
struts
action
AddStudentAction.java 1KB
entity
Student.java 648B
Student.hbm.xml 620B
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 914B
applicationContext.xml 1KB
.project 2KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 908B
classes
struts.xml 502B
org
jiao
dao
impl
StudentDAOImpl.class 959B
StudentDAO.class 169B
biz
StudentBiz.class 169B
impl
StudentBizImpl.class 804B
web
struts
action
AddStudentAction.class 2KB
entity
Student.hbm.xml 620B
Student.class 1KB
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 914B
applicationContext.xml 1KB
lib
log4j-1.2.14.jar 359KB
hibernate3-src.jar 1.51MB
commons-logging-1.0.4.jar 37KB
dom4j-1.6.1.jar 307KB
spring-src.jar 3.01MB
spring.jar 2.58MB
commons-beanutils.jar 160KB
saaj.jar 19KB
jta.jar 9KB
commons-collections.jar 558KB
jakarta-oro.jar 62KB
junit.jar 118KB
commons-validator.jar 74KB
hibernate-tools.jar 352KB
commons-fileupload.jar 19KB
xwork-2.0.4.jar 445KB
struts2-spring-plugin-2.0.11.jar 9KB
ojdbc14.jar 1.12MB
jaxrpc.jar 32KB
jtidy-r8-20060801.jar 243KB
struts-src.jar 908KB
ognl-2.6.11.jar 164KB
struts-core-1.3.5.jar 312KB
jstl.jar 20KB
commons-logging.jar 33KB
ehcache-1.2.4.jar 207KB
cglib-nodep-2.1_3.jar 317KB
mysql-connector-java-5.1.5-bin.jar 661KB
freemarker.jar 771KB
wsdl4j-1.5.1.jar 124KB
commons-discovery-0.2.jar 70KB
struts.jar 474KB
freemarker-2.3.8.jar 784KB
commons-digester.jar 147KB
hsqldb.jar 610KB
jaxen-1.1-beta-7.jar 222KB
antlr-2.7.6.jar 433KB
struts2-core-2.0.11.jar 2.22MB
hibernate3.jar 2.15MB
index.jsp 904B
addStudent.jsp 1KB
error.jsp 844B
success.jsp 836B
.mymetadata 288B
.classpath 4KB
.myeclipse
数据库.sql 290B
共 73 条
- 1
资源评论
「已注销」
- 粉丝: 19
- 资源: 125
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用 Python 和 Pygame 开发《星露谷物语》 游戏+源码(毕业设计&课程设计&项目开发)
- VOC4-123-1-321-32-1
- 【课程设计全套资料】一个基于JAVA-JSP企业合同管理系统
- bootstrap的css和js文件
- lda代码-checkpoint-checkpoint.ipynb
- 基于java局域网飞鸽传书软件设计与实现(源代码+论文).zip
- 基于python开发的魂斗罗游戏+源码(毕业设计&课程设计&项目开发)
- 停车场管理系统系统采用JAVAEE,利用SQLSERVER2005,B/S架构
- VOC数据集1-111111
- 基于python开发的乒乓球和滑雪游戏+源码(毕业设计&课程设计&项目开发)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功