package com.util;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;
import com.exception.HibernateUtilException;
public class HibernateUtil {
private static SessionFactory sf;
private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
static {
Configuration cfg = new Configuration();
cfg.configure("/hibernate.cfg.xml");
cfg.buildSessionFactory();
}
private static Session getSession() throws HibernateUtilException {
Session session = threadLocal.get();
if (null != session && session.isOpen()) {
return session;
} else {
try {
threadLocal.set(sf.openSession());
} catch (HibernateException e) {
throw new HibernateUtilException("获取Session出错", e);
}
}
return session;
}
private static void closeSession() {
Session session = threadLocal.get();
if (null != session && session.isOpen()) {
session.close();
}
}
private static void beginTran() throws HibernateUtilException {
Session session = threadLocal.get();
if (null != session && session.isOpen()) {
try {
session.beginTransaction();
} catch (HibernateException e) {
throw new HibernateUtilException("尝试启动事务失败,可能未获取到Session对象", e);
}
} else {
getSession();// 获取session
beginTran();// 再次开启事务
}
}
private static void commitTran() throws HibernateUtilException {
Session session = threadLocal.get();
if (null != session && session.isOpen()) {
try {
session.getTransaction().commit();
} catch (HibernateException e) {
throw new HibernateUtilException("尝试提交事务失败,可能未获取到Session对象", e);
}
}
}
private static void rollbackTran() throws HibernateUtilException {
Session session = threadLocal.get();
if (null != session && session.isOpen()) {
try {
session.getTransaction().rollback();
} catch (HibernateException e) {
throw new HibernateUtilException("尝试回滚事务失败,可能未获取到Session对象", e);
}
}
}
public static Object addObj(Object obj) {
Object obj2=null;
try {
beginTran();// 启动事务
obj2=(Object)threadLocal.get().save(obj);
commitTran();
} catch (HibernateUtilException e) {
try {
rollbackTran();
} catch (HibernateUtilException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return obj2;
}
public static Object getOneObjBySQL(String entity,String column,String value){
Object obj=null;
String hql= "from "+entity +" obj where obj."+column+" = :v";
//String hql= "from "+entity +" where obj."+column+" = :v";
try {
beginTran();//启动一个事务
Query query= threadLocal.get().createQuery(hql);//创建查询对象
query.setParameter("v", value);
obj=query.uniqueResult();//获得单独对象
commitTran();//提交事务
} catch (HibernateUtilException e) {
e.printStackTrace();
}
return obj;
}//public static Object getOneObjBySQL
public static void updateObj(Object obj) {
Session session = threadLocal.get();
try {
beginTran();// 启动事务
session.update(obj);
commitTran();
} catch (HibernateUtilException e) {
try {
rollbackTran();
} catch (HibernateUtilException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
public static void deleteObj(Class clz,Long id) {
Session session = threadLocal.get();
try {
beginTran();// 启动事务
session.delete(get(clz,id));
commitTran();
} catch (HibernateUtilException e) {
try {
rollbackTran();
} catch (HibernateUtilException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
public static Object get(Class clz, Long id) {
Object obj=null;
Session session = threadLocal.get();
try {
obj=session.get(clz, id);
} catch (HibernateException e) {
e.printStackTrace();
}
return obj;
}
public List search(Class clz,Object condition){
Session session = threadLocal.get();
List results=null;
try {
results=session.createCriteria(clz).add(Example.create(condition)).list();
} catch (RuntimeException re) {
re.printStackTrace();
}
return results;
}
}
封装了一个Hibernate增删改查的工具类
4星 · 超过85%的资源 需积分: 9 7 浏览量
2010-01-07
22:15:55
上传
评论 3
收藏 3KB RAR 举报
ououy009
- 粉丝: 3
- 资源: 10
最新资源
- TFS(淘宝分布式存储引擎代码
- 海尔H42E10 6i981BTA DH1PY600000 MBOOT usb.bin 程序.zip
- 网盘的功能实现之数据库搭建笔记
- 软件工程与软件测试阶段作业三.doc
- 软件工程中的人工智能辅助设计研究.pptx
- 海尔智能电视刷机数据 H42E07 机编DH1QR000H07 务必确认机编一致 强制刷机 整机USB升级主程序
- 4444444444444444444
- 基于PHP实现响应式创业园区管委会网站.zip
- 安卓手机用作电脑网络摄像头的工具文件
- 海尔智能电视刷机数据 H32E10 机编DH1QX000805 务必确认机编一致 强制刷机 整机USB升级主程序
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈