package hibernate;
import org.hibernate.HibernateException;
import org.hibernate.classic.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;
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
hibernate实现分页
共74个文件
jar:26个
xml:11个
java:9个
1星 需积分: 9 309 下载量 126 浏览量
2007-05-24
17:45:24
上传
评论
收藏 5.1MB RAR 举报
温馨提示
基于struts框架!
资源推荐
资源详情
资源评论
收起资源包目录
ChiJiu.rar (74个子文件)
ChiJiu
.project 382B
WEB-INF
struts-tiles.tld 8KB
validation.xml 1KB
tiles-defs.xml 1KB
web.xml 2KB
struts-config.xml 4KB
struts-bean.tld 9KB
struts-nested.tld 63KB
src
DB
DatabaseConnectionBean.java 658B
DBConnection.java 1KB
dao
Test.java 3KB
InsertDao.java 949B
hibernate
HibernateSessionFactory.java 3KB
log4j.properties 1KB
java
resources
application.properties 1KB
vo
Student.java 1KB
com
action
showRecordAction.java 944B
changePageAction.java 2KB
actionForm
pageForm.java 398B
hibernate.cfg.xml 1KB
hibernateXML
student.hbm.xml 588B
build.xml 4KB
struts-html.tld 65KB
struts-template.tld 2KB
struts-logic.tld 14KB
validator-rules.xml 41KB
lib
ant-1.6.3.jar 1009KB
commons-logging.jar 31KB
cglib-2.1.jar 275KB
log4j-1.2.9.jar 344KB
commons-digester.jar 107KB
mysql-connector-java-3.1.8-bin.jar 400KB
commons-fileupload.jar 22KB
antlr-2.7.5H3.jar 423KB
dom4j-1.6.jar 306KB
ant-antlr-1.6.3.jar 6KB
ant-junit-1.6.3.jar 72KB
commons-beanutils.jar 116KB
jta.jar 9KB
hibernate3.jar 1.57MB
commons-lang.jar 62KB
jdbc2_0-stdext.jar 7KB
struts.jar 486KB
ant-swing-1.6.3.jar 7KB
commons-validator.jar 46KB
ehcache-1.1.jar 46KB
struts-legacy.jar 10KB
asm.jar 26KB
jakarta-oro.jar 64KB
commons-collections.jar 161KB
asm-attrs.jar 16KB
ant-launcher-1.6.3.jar 9KB
classes
DB
DBConnection.class 2KB
DatabaseConnectionBean.class 1KB
dao
Test.class 4KB
InsertDao.class 2KB
hibernate
HibernateSessionFactory.class 2KB
log4j.properties 1KB
java
resources
application.properties 1KB
vo
Student.class 1KB
com
action
changePageAction.class 2KB
showRecordAction.class 2KB
actionForm
pageForm.class 576B
hibernate.cfg.xml 1KB
hibernateXML
student.hbm.xml 588B
build.xml 4KB
bin
META-INF
MANIFEST.MF 55B
pages
side.jsp 313B
show.jsp 1KB
main.jsp 313B
Welcome.jsp 538B
frame.jsp 678B
src
index123.jsp 276B
.classpath 2KB
共 74 条
- 1
资源评论
- wgcking-2312012-12-19谢谢,很基础的资料。
excignjord
- 粉丝: 6
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功