package com.soft.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 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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Hibernate+Servlet+jsp实现分页
共49个文件
jar:19个
java:7个
class:7个
4星 · 超过85%的资源 需积分: 9 68 下载量 9 浏览量
2014-09-03
23:52:35
上传
评论 3
收藏 6.85MB ZIP 举报
温馨提示
这个是我写的一个用到Hibernate+Servlet+Jsp来实现分页的代码,有助于大家学习分页功能的实现。
资源推荐
资源详情
资源评论
收起资源包目录
Hibernate+Servlet+Jsp实现分页.zip (49个子文件)
Hibernate_0502
hibernate.reveng.xml 925B
.myhibernatedata 593B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 330B
src
com
soft
dao
EmpDao.java 197B
impl
EmpDaoImpl.java 1KB
action
QueryServlet.java 2KB
service
EmpService.java 204B
impl
EmpServiceImpl.java 452B
entity
Emp.java 2KB
Emp.hbm.xml 1KB
util
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 944B
.project 2KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 2KB
classes
com
soft
dao
EmpDao.class 238B
impl
EmpDaoImpl.class 2KB
action
QueryServlet.class 2KB
service
EmpService.class 250B
impl
EmpServiceImpl.class 794B
entity
Emp.hbm.xml 1KB
Emp.class 2KB
util
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 944B
lib
log4j-1.2.14.jar 359KB
commons-collections-3.1.jar 546KB
hibernate-annotations.jar 273KB
dom4j-1.6.1.jar 307KB
ejb3-persistence.jar 49KB
standard.jar 384KB
cglib-2.2.jar 272KB
ojdbc14.jar 1.47MB
jstl.jar 20KB
javassist-3.9.0.GA.jar 583KB
hibernate-commons-annotations.jar 65KB
slf4j-api-1.5.8.jar 23KB
ehcache-1.2.3.jar 203KB
slf4j-log4j12-1.5.8.jar 9KB
jta-1.1.jar 13KB
hibernate-validator.jar 61KB
hibernate-entitymanager.jar 116KB
antlr-2.7.6.jar 433KB
hibernate3.jar 2.31MB
index.jsp 3KB
.mymetadata 315B
.classpath 2KB
.myeclipse
共 49 条
- 1
资源评论
- xl_you2014-09-15还好、帮我复习了……
- wolfkingkg01292014-09-17很简单的示例,清晰了解hibernate servlet jsp三者是如何结合的,分页基本功能也很典型,对初学者很有帮助
- duchengguo2016-02-22很有用,很好
- qq_287888592016-08-30说实话不怎么样
xpflikeyou
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功