package com.cjx.hibernate;
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多对多关系项目
共54个文件
jar:29个
xml:7个
class:6个
5星 · 超过95%的资源 需积分: 9 40 下载量 188 浏览量
2009-10-06
04:25:25
上传
评论 1
收藏 8.94MB RAR 举报
温馨提示
hibernate 多对多 映射 翻转 hibernate多对多关系项目 自己做的hibernate项目 上传上来和大家分享
资源推荐
资源详情
资源评论
收起资源包目录
many2many.rar (54个子文件)
many2many
.project 1KB
.mymetadata 300B
WebRoot
WEB-INF
web.xml 375B
lib
connector.jar 16KB
oscache-2.1.jar 97KB
jboss-cache.jar 320KB
log4j-1.2.11.jar 305KB
c3p0-0.9.0.jar 421KB
antlr-2.7.6rc1.jar 425KB
jta.jar 8KB
hibernate3.jar 1.63MB
jaxen-1.1-beta-7.jar 194KB
commons-logging-1.0.4.jar 33KB
concurrent-1.3.2.jar 145KB
xml-apis.jar 117KB
jdbc2_0-stdext.jar 6KB
commons-collections-2.1.1.jar 169KB
jboss-common.jar 499KB
jaas.jar 99KB
ehcache-1.1.jar 41KB
swarmcache-1.0rc2.jar 26KB
dom4j-1.6.1.jar 257KB
proxool-0.8.3.jar 406KB
asm.jar 25KB
jacc-1_0-fr.jar 20KB
jgroups-2.2.8.jar 1.36MB
classes12.jar 1.53MB
xerces-2.6.2.jar 966KB
jboss-system.jar 193KB
cglib-2.1.3.jar 244KB
jboss-jmx.jar 505KB
asm-attrs.jar 16KB
classes
com
cjx
hibernate
HibernateSessionFactory.class 2KB
test
Test.class 569B
roledao
RoleDao.class 960B
po
Trole.class 1KB
Tuser.class 2KB
Trole.hbm.xml 1015B
Tuser.hbm.xml 1KB
userdao
UserDao.class 2KB
hibernate.cfg.xml 981B
index.jsp 834B
META-INF
MANIFEST.MF 39B
.myeclipse
src
com
cjx
hibernate
HibernateSessionFactory.java 3KB
test
Test.java 985B
roledao
RoleDao.java 493B
po
Tuser.java 1KB
Trole.hbm.xml 1015B
Tuser.hbm.xml 1KB
Trole.java 903B
userdao
UserDao.java 2KB
hibernate.cfg.xml 981B
.classpath 2KB
.myhibernatedata 488B
共 54 条
- 1
资源评论
- 胡萝卜_20142012-09-12非常详细,细致!
- liushuzhi_1232012-08-01按照他说的配置成功,学习了
- MAX2012-11-20写的很好,配置很成功~
yongcock
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功