package com.factory;
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;
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分页例子
共56个文件
tld:20个
jar:11个
xml:7个
需积分: 3 33 下载量 149 浏览量
2009-11-26
14:10:29
上传
评论
收藏 2.01MB RAR 举报
温馨提示
自己整理的hibernate分页例子 int num=5;//每页显示的条数 int rows=dao.getRows(); int pages=(rows-1)/num+1;//总页数
资源推荐
资源详情
资源评论
收起资源包目录
Hibernate分页.rar (56个子文件)
Hibernate分页
AA
.project 1KB
.mymetadata 279B
WebRoot
WEB-INF
struts-tiles.tld 8KB
fmt-1_0.tld 12KB
fmt.tld 19KB
.struts-config.mex 434B
sql-1_0-rt.tld 5KB
fmt-1_0-rt.tld 11KB
permittedTaglibs.tld 1KB
web.xml 942B
struts-config.xml 580B
struts-bean.tld 9KB
scriptfree.tld 2KB
c-1_0-rt.tld 11KB
c-1_0.tld 11KB
struts-nested.tld 70KB
x-1_0-rt.tld 7KB
struts-html.tld 72KB
struts-logic.tld 14KB
x.tld 12KB
validator-rules.xml 13KB
sql.tld 8KB
lib
commons-logging.jar 37KB
commons-digester.jar 164KB
commons-fileupload.jar 22KB
commons-beanutils.jar 184KB
jstl.jar 20KB
antlr.jar 350KB
struts.jar 531KB
commons-validator.jar 82KB
jakarta-oro.jar 64KB
standard.jar 384KB
classes
com
dao
DAO.class 1KB
action
EAction.class 2KB
vo
Employee.hbm.xml 1KB
Employee.class 2KB
yourcompany
struts
ApplicationResources.properties 85B
factory
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 908B
sql-1_0.tld 6KB
c.tld 16KB
fn.tld 7KB
x-1_0.tld 7KB
META-INF
MANIFEST.MF 39B
list.jsp 2KB
.myeclipse
src
com
dao
DAO.java 745B
action
EAction.java 1KB
vo
Employee.hbm.xml 1KB
Employee.java 1KB
yourcompany
struts
ApplicationResources.properties 85B
factory
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 908B
.mystrutsdata 262B
.classpath 1KB
.myhibernatedata 417B
lib
ms_sql2k_jdbc.jar 401KB
共 56 条
- 1
资源评论
叶子13
- 粉丝: 3
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功