package com.accp.hbm;
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综合练习
共74个文件
jar:28个
java:16个
class:16个
需积分: 0 4 下载量 161 浏览量
2008-09-09
16:07:22
上传
评论
收藏 7.51MB RAR 举报
温馨提示
如果你觉得你的hibernate 学的不好,那你能看懂这段代码吗?请测试
资源详情
资源评论
资源推荐
收起资源包目录
hibernate-5.rar (74个子文件)
hibernate-5
src
com
accp
pojo
Dept.java 949B
Users.hbm.xml 1KB
Dept.hbm.xml 867B
Users.java 1KB
dao
IusersDao.java 147B
Userdao.java 1KB
exception
hibernateException.java 519B
hbm
HibernateSessionFactory.java 3KB
service
IuserService.java 239B
UserService.java 542B
common
Icommon.java 1KB
Common.java 2KB
test
CommonTest.java 2KB
servlet
UserSevlet.java 754B
acco
vo
UsersVo.java 1KB
DeptVo.java 534B
ccom
accp
proxy
ProxyFactory.java 539B
TransactionProxy.java 1KB
hibernate.cfg.xml 968B
.classpath 3KB
.mymetadata 306B
.myeclipse
.project 1KB
hnbfinal.mer 2KB
WebRoot
META-INF
MANIFEST.MF 39B
index.jsp 834B
WEB-INF
classes
com
accp
pojo
Users.hbm.xml 1KB
Dept.hbm.xml 867B
Dept.class 1KB
Users.class 2KB
dao
IusersDao.class 157B
Userdao.class 2KB
exception
hibernateException.class 748B
hbm
HibernateSessionFactory.class 2KB
service
IuserService.class 184B
UserService.class 1020B
common
Icommon.class 620B
Common.class 3KB
test
CommonTest.class 2KB
servlet
UserSevlet.class 1KB
acco
vo
UsersVo.class 2KB
DeptVo.class 876B
ccom
accp
proxy
ProxyFactory.class 864B
TransactionProxy.class 2KB
hibernate.cfg.xml 968B
lib
jta.jar 8KB
jaas.jar 99KB
swarmcache-1.0rc2.jar 26KB
jboss-common.jar 499KB
connector.jar 16KB
dom4j-1.6.1.jar 257KB
concurrent-1.3.2.jar 145KB
commons-logging-1.0.4.jar 33KB
xerces-2.6.2.jar 966KB
jaxen-1.1-beta-7.jar 194KB
hibernate3.jar 1.63MB
log4j-1.2.11.jar 305KB
ehcache-1.1.jar 41KB
jboss-cache.jar 320KB
commons-collections-2.1.1.jar 169KB
jboss-jmx.jar 505KB
jacc-1_0-fr.jar 20KB
asm-attrs.jar 16KB
jboss-system.jar 193KB
c3p0-0.9.0.jar 421KB
antlr-2.7.6rc1.jar 425KB
xml-apis.jar 117KB
jgroups-2.2.8.jar 1.36MB
asm.jar 25KB
proxool-0.8.3.jar 406KB
jdbc2_0-stdext.jar 6KB
cglib-2.1.3.jar 244KB
oscache-2.1.jar 97KB
web.xml 802B
.myhibernatedata 482B
共 74 条
- 1
yun920920
- 粉丝: 12
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0