/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package factory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
*
* @author wlshi
*/
public class HibernateSessionFactory {
//读取配置文件
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(){//无参构造方法
}
/**
* 获得Session方法
*
*/
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;
}
public static void rebuildSessionFactory(){
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exception e){
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
*关闭Hibernate的Session
*
*/
public static void closeSession()throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if(session!=null){
session.close();
}
}
/**
* 给Configuration赋值的方法
*
* Session Factory在接下来的调用将被重建
*/
public static void setConfigFile(String configFile){
HibernateSessionFactory.configFile=configFile;
sessionFactory=null;
}
/**
* 获得Session Factory的方法
*
*/
public static org.hibernate.SessionFactory getSessionFactory(){
return sessionFactory;
}
/**
* 获得Hibernate Configuration的方法
*
*/
public static Configuration getConfigration(){
return configuration;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
hibernate_one_to_one映射简单入门例子
共19个文件
xml:9个
java:4个
properties:3个
2星 需积分: 3 9 下载量 120 浏览量
2011-09-29
10:38:32
上传
评论
收藏 18KB RAR 举报
温馨提示
hibernate_one_to_one映射简单入门例子,使用netbeans开发
资源推荐
资源详情
资源评论
收起资源包目录
hibernate_one_to_one.rar (19个子文件)
hibernate_one_to_one
src
conf
MANIFEST.MF 25B
java
hibernate.cfg.xml 902B
factory
HibernateSessionFactory.java 3KB
test
Test.java 1KB
vo
Dept_ExplainVo.hbm.xml 807B
DeptVo.hbm.xml 646B
Dept_ExplainVo.java 1KB
DeptVo.java 1KB
web
WEB-INF
sun-web.xml 521B
web.xml 490B
index.jsp 455B
test
bank.sql 1KB
build.xml 3KB
nbproject
genfiles.properties 455B
project.properties 2KB
private
private.properties 3KB
ant-deploy.xml 3KB
build-impl.xml 44KB
project.xml 1KB
共 19 条
- 1
资源评论
- worldcomputermaster2012-08-22我用的myeclipse,没有成功打开 ,不知道上传者用的什么。我刚入门。
至尊无上大师
- 粉丝: 0
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功