您的位置:首页 > 编程语言 > Java开发

JavaWeb学习笔记-Hibernate-03-完善HibernateUtil

2017-12-27 22:36 465 查看
//完善HibernateUtil方法
public static void add(Object entity){
Session s = null;
Transaction tx = null;
try{
s = HiberanteUtil.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 = HiberanteUtil.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
9d50
= null;
try{
s = HiberanteUtil.getSession();
tx = s.beginTransaction();
s.delete(entity);
tx.commit();
}finally{
if(s!=null){
s.close();
}
}
}
public static Object get(Class clazz, Serializable id){
Session s = null;
try{
s = HiberanteUtil.getSession();
Object obj = s.get(clazz,id);
return obj;
}finally{
if(s != null){
s.close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: