package org.hibernate;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
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); //解析hibernate.cfg.xml映射文件。
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() { //不能new
}
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();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
//增加对象
public static void add(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateSessionFactory.getSession();
tx=s.beginTransaction();
s.save(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
//更新对象
public static void update(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateSessionFactory.getSession();
tx=s.beginTransaction();
s.update(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
//删除对象
public static void delete(Object entity){
Session s=null;
Transaction tx=null;
try{
s=HibernateSessionFactory.getSession();
tx=s.beginTransaction();
s.delete(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
//通过id得到对象
public static Object get(Class clazz,Serializable id){
Session s=null;
try{
s=HibernateSessionFactory.getSession();
Object obj=s.get(clazz, id);
return obj;
}finally{
if(s!=null){
s.close();
}
}
}
}
hibernate例子(多对一)
3星 · 超过75%的资源 需积分: 9 185 浏览量
2008-11-25
16:17:34
上传
评论
收藏 444KB RAR 举报
wangjun_pfc
- 粉丝: 252
- 资源: 124
最新资源
- 基于混沌集成决策树的电能质量复合扰动识别(注释完全,可直接运行)(文档加Matlab源码)
- 基于JAVA的打飞机游戏设计(程序).zip
- 随机优化智能配电网的双时间尺度随机优化调度(注释完全,可直接运行)(文档加Matlab源码)
- 多目标粒子群选址定容-main为主函数-含储能出力(注释完全,可直接运行)(文档加Matlab源码)
- fonepaw苹果恢复
- 基于主从博弈的主动配电网阻塞管理(注释完全,可直接运行)(文档加Matlab源码)
- 房地产公司信息化管理奖惩作业指引模版.doc
- 园区三方主体(注释完全,可直接运行)(文档加Matlab源码)
- 基于php+mysql+html+css超市管理系统答辩PPT.pptx
- 房地产公司信息化管理程序模版.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈