package config;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
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 = "/config/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static AnnotationConfiguration configuration = new AnnotationConfiguration();
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(Table per class hierarchy).rar (63个子文件)
hibernate
.project 719B
bin
postgresql-8.2-506.jdbc4.zip 446KB
com
persist
tablePerConcreteClass
TBook.class 343B
TItem.class 1KB
TDvd.class 335B
TItem.hbm.xml 1KB
AbstractTDvd.class 614B
AbstractTBook.class 623B
TBookDAO.class 1KB
BaseHibernateDAO.class 494B
IBaseHibernateDAO.class 176B
factory
HibernateSessionFactory.class 2KB
config
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 965B
postgresql.jar 93KB
.settings
org.eclipse.jdt.ui.prefs 99B
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
src
com
persist
BaseHibernateDAO.java 365B
tablePerConcreteClass
AbstractTBook.java 377B
AbstractTDvd.java 365B
TDvd.java 220B
TItem.hbm.xml 1KB
TBookDAO.java 997B
TBook.java 280B
TItem.java 690B
IBaseHibernateDAO.java 222B
factory
HibernateSessionFactory.java 3KB
config
hibernate.cfg.xml 965B
HibernateSessionFactory.java 3KB
hibernate.reveng.xml 511B
.classpath 557B
.myhibernatedata 532B
lib
postgresql-8.2-506.jdbc4.zip 446KB
postgresql.jar 93KB
hibernateAnno
.project 723B
bin
com
persist
tablePerConcreteClass
TBook.class 470B
TItem.class 2KB
TDvd.class 466B
AbstractTDvd.class 688B
AbstractTBook.class 697B
TBookDAO.class 1KB
BaseHibernateDAO.class 481B
IBaseHibernateDAO.class 176B
config
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 1KB
.settings
org.eclipse.jdt.ui.prefs 99B
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
src
com
persist
BaseHibernateDAO.java 317B
tablePerConcreteClass
AbstractTBook.java 568B
AbstractTDvd.java 509B
TDvd.java 573B
TBookDAO.java 998B
TBook.java 403B
TItem.java 1KB
IBaseHibernateDAO.java 222B
config
hibernate.cfg.xml 1KB
HibernateSessionFactory.java 3KB
.classpath 572B
.myhibernatedata 480B
lib
postgresql-8.2-506.jdbc4.zip 446KB
postgresql.jar 93KB
共 63 条
- 1
资源评论
- crystalai2013-12-25很早期的下载了 还是很有用的
jszhaochengming
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 调显著性代码.do
- 66297787762147sf22346mod.apk
- tubiao.cpython-311.pyc
- 文一-2405072034.awb
- 海能达TD360写频软件
- iscreenlocker5.2.2-release.apk
- tensorflow-gpu-2.2.3-cp37-cp37m-win-amd64.whl
- 基础运维技能(下)md格式笔记
- tensorflow-rocm-2.13.0.570-cp39-cp39-manylinux2014-x86-64.whl
- tensorflow-rocm-2.13.0.570-cp38-cp38-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功