package com.snow.sessionfactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* 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 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 annotation JPA 一对多,多对多
共51个文件
jar:24个
java:6个
class:6个
4星 · 超过85%的资源 需积分: 27 94 下载量 77 浏览量
2009-11-04
17:10:13
上传
评论 2
收藏 6.69MB ZIP 举报
温馨提示
使用学生(student)、老师(teacher)、班级(class)反映出Hibernate annotation JPA 的一对多、多对多、多对一的关联。或许对您有用
资源推荐
资源详情
资源评论
收起资源包目录
hibernateAn.zip (51个子文件)
hibernateAn
.project 1KB
.settings
.jsdtscope 500B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 306B
WebRoot
WEB-INF
web.xml 371B
lib
log4j-1.2.11.jar 342KB
hibernate-commons-annotations.jar 65KB
hibernate-annotations.jar 259KB
jta.jar 9KB
hibernate3.jar 2.15MB
javassist.jar 460KB
jaxen-1.1-beta-7.jar 222KB
commons-logging-1.0.4.jar 37KB
ehcache-1.2.3.jar 203KB
xml-apis.jar 121KB
jdbc2_0-stdext.jar 7KB
commons-collections-2.1.1.jar 171KB
ejb3-persistence.jar 49KB
jaas.jar 102KB
hibernate-entitymanager.jar 114KB
dom4j-1.6.1.jar 307KB
mysql6.0.jar 622KB
asm.jar 26KB
hibernate-validator.jar 60KB
xerces-2.6.2.jar 987KB
jboss-archive-browsing.jar 404KB
cglib-2.1.3.jar 276KB
antlr-2.7.6.jar 433KB
asm-attrs.jar 16KB
classes
log4j.properties 297B
com
snow
test
Test.class 1KB
sessionfactory
HibernateSessionFactory.class 2KB
entity
Teacher.class 2KB
Classes.class 2KB
Student.class 2KB
pojo
BaseDao.class 2KB
hibernate.cfg.xml 952B
index.jsp 834B
META-INF
MANIFEST.MF 36B
.myeclipse
src
log4j.properties 297B
com
snow
test
Test.java 911B
sessionfactory
HibernateSessionFactory.java 3KB
entity
Classes.java 1KB
Teacher.java 2KB
Student.java 2KB
pojo
BaseDao.java 1KB
hibernate.cfg.xml 952B
.classpath 2KB
.myhibernatedata 552B
共 51 条
- 1
hhb_snow1
- 粉丝: 4
- 资源: 35
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页