文档详情

hibernate增删改查的标准范例.docx

发布:2017-05-22约4.51千字共5页下载文档
文本预览下载声明
package .bookhome.daoimpl; ? ?? import java.util.List; ? ?? import org.hibernate.Session; ? import org.hibernate.Transaction; ? ?? import .bookhome.dao.AdminDao; ? import .bookhome.dao.BaseHibernateDao; ? import .bookhome.domain.Admin; ? import .bookhome.utils.HibernateSessionFactory; ? ?? public class AdminDaoImpl extends BaseHibernateDao implements AdminDao { ? ?? ? ? public void deleteObject(Admin entity) { ? ? ? ? ? Transaction tx = null; ? ? ? ? ? try { ? ? ? ? ? ? ? Session session = getSession(); ? ? ? ? ? ? ? tx = session.beginTransaction(); ? ? ? ? ? ? ? session.delete(entity); ? ? ? ? ? ? ? mit(); ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? tx.rollback(); ? ? ? ? ? ? ? throw new RuntimeException(删除所有错误+e); ? ? ? ? ? } finally { ? ? ? ? ? ? ? HibernateSessionFactory.closeSession(); ? ? ? ? ? } ? ? ? } ? ?? ? ? public void deleteObjectById(Integer id) { ? ? ? ? ? Transaction tx = null; ? ? ? ? ? try { ? ? ? ? ? ? ? Session session = getSession(); ? ? ? ? ? ? ? tx = session.beginTransaction(); ? ? ? ? ? ? ? session.save(id); ? ? ? ? ? ? ? mit(); ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? tx.rollback(); ? ? ? ? ? ? ? throw new RuntimeException(根据id错误+e); ? ? ? ? ? } finally { ? ? ? ? ? ? ? HibernateSessionFactory.closeSession(); ? ? ? ? ? } ? ? ? } ? ?? ? ? public List getAllObjects(Class entityClass) { ? ? ? ? ? try { ? ? ? ? ? ? ? return getSession().createQuery(from Admin).list(); ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? throw new RuntimeException(查找错误+e); ? ? ? ? ? } finally { ? ? ? ? ? ? ? HibernateSessionFactory.closeSession(); ? ? ? ? ? } ? ? ? } ? ?? ? ? public Admin getObjectById(Class className, Integer id) { ? ? ? ? ? try { ? ? ? ? ? ? ? return (Admin) getSession().get(className, id); ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? throw new RuntimeException(根据id查找错误+e); ? ? ? ? ? } finally { ? ? ? ? ? ? ? HibernateSessionFactory.closeSession(); ? ? ? ? ? } ? ? ? } ? ?? ? ? public List getObjects(Class clazz, int from, int size, Stri
显示全部
相似文档